Skip to content

Instantly share code, notes, and snippets.

@jasonneylon
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonneylon/328a2b2e904290eee6fd to your computer and use it in GitHub Desktop.
Save jasonneylon/328a2b2e904290eee6fd to your computer and use it in GitHub Desktop.
Macro to optimise maps targeting ClojureScript
(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