Last active
August 29, 2015 14:10
-
-
Save jasonneylon/328a2b2e904290eee6fd to your computer and use it in GitHub Desktop.
Macro to optimise maps targeting ClojureScript
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
(defmacro defquestions | |
"Macro to reduce the size of the generated javascript by omitting unused keys (and their values) in the questions. | |
It works by looping over the questions and only retaining the keys that are required on the client side. | |
To ensure it is applied on the client size code require the macro and then apply it using #+cljs. | |
For the server side just use the #+clj marker and a normal def." | |
[name questions] | |
{:pre [(symbol? name) (and (coll? questions) (every? map? questions))]} | |
(let [filtered-questions (mapv #(select-keys % [:key :tags :schema :ask-if]) questions)] | |
`(def ~name ~filtered-questions))) | |
# An example usage of the macro: | |
(#+cljs defquestions #+clj def questions | |
[{:key :date-of-birth | |
:type :date | |
:tags [#{}] | |
:group groups/about-you | |
:schema (shared/map-has {:date-of-birth (shared/date-of-birth 16)}) | |
:label "What is your date of birth?" | |
:help-text "The following information is required by your new supplier for security. Your answers will ensure that your new supplier can verify your identity, for example when enquiring over the telephone.”}]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment