Created
December 10, 2016 22:45
-
-
Save rauhs/098a720669c75e53eb00dc3e6ed58e49 to your computer and use it in GitHub Desktop.
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
(defn obj->enums | |
"Takes a js object and returns all keys that map to a string or a number (thus constant)" | |
[obj] | |
(sort | |
(mapv first | |
(js->clj | |
(goog.object/filter | |
obj | |
(fn [v k] | |
((some-fn number? string?) v))))))) | |
#_(obj->enums goog.events.KeyCodes) | |
(defn gen-def-cont-str | |
[gns] | |
(with-out-str | |
(doseq [k (obj->enums (js/eval (str gns)))] | |
(let [sym (symbol k)] | |
(binding [*print-meta* true] | |
(prn `(def ~(with-meta sym {:const true}) ~(symbol (str gns "." sym))))))))) | |
#_ (gen-def-cont-str 'goog.events.KeyCodes) | |
(defn gns->cljsns | |
[base gsym] | |
(let [gstr (str gsym) | |
[_ & r] (js->clj (.split gstr ".")) | |
lastns (goog.string.toSelectorCase (last r)) | |
lastns (cond-> lastns (clojure.string/starts-with? lastns "-") (subs 1)) | |
path (clojure.string/join \. (concat [base] (butlast r) [lastns]))] | |
path)) | |
#_(gns->cljsns "gconst" 'goog.events.KeyCodes) | |
(defn gen-gconst-ns | |
[gsym] | |
(let [out-ns (gns->cljsns "gconst" gsym)] | |
(str | |
"(ns " out-ns "\n (:require [" gsym "]))\n\n" | |
(gen-def-cont-str gsym)))) | |
#_(gen-gconst-ns 'goog.events.KeyCodes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Problem:
In cljs you can't
switch-case
on goog enums:Usually people end up using
condp
for this, but a case would be the better/cleaner solution.Synopsis:
Generate your namespaced keywords with
gen-gcons-ns
and put the output in the according cljs file. Example usage in the#_
comment.Why?
Because then you can do
(case pressedkey (DOWN DASH F1) (doit))
which you can't unless the vars are declares as^:const
.Example: