Last active
January 4, 2018 17:00
-
-
Save krisleech/15fe9575f3f60d87d938def27010c4de to your computer and use it in GitHub Desktop.
Convert whitelist of boolean values to yes/no text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; converts boolean (true/false) to text (yes/no) | |
(defn- boolean-to-text [bool] | |
{:pre [(boolean? bool)] } | |
(if (true? bool) "Yes" "No")) | |
;; returns true if coll contains given value | |
(defn- seq-contains? [coll target] (some #(= target %) coll)) | |
;; converts key values from true/false to yes/no in map | |
(defn- coerce-boolean-values-for-map [bool-keys record] | |
(into {} (for [[k v] record] [k (if (seq-contains? bool-keys k) (boolean-to-text v) v)]))) | |
;; converts key values from boolean to text for collection of maps | |
(defn coerce-boolean-values-for-coll [coll bool-keys] | |
{:pre [(every? map? coll)]} | |
(map (partial coerce-boolean-values-for-map bool-keys) coll)) | |
(defn data [ { :bar true :foo false :baz "Hi" } ]) | |
(coerce-boolean-values-for-coll data [:bar :foo]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment