Set of helper functions for working with vanilla Clojurescript, requiring no other 3rd party libraries. This is not meant to be replacement of similar libraries like medley, rather should be complementary.
Add [district0x/district-cljs-utils "1.0.4"]
into your project.clj
Include [district.cljs-utils]
in your CLJS file
merge-in [& args]
Deep merge of multiple maps
(cljs-utils/merge-in {:a {:b 1}} {:a {:c 2}} {:a {:d 3}})
;; => {:a {:b 1 :c 2 :d 3}}
collify [x]
Ensures a collection.
(first (cljs-utils/collify 1))
;; => 1
(first (cljs-utils/collify [1]))
;; => 1
not-nil? [x]
Returns true if not nil
(cljs-utils/not-nil? 1)
;; => true
sort-desc [coll]
Descending sort
(cljs-utils/sort-desc [2 1 3])
;; => [3 2 1]
sort-by-desc [key-fn coll]
Descending sort by key-fn
(cljs-utils/sort-by-desc :a [{:a 2} {:a 1} {:a 3}])
;; => ({:a 3} {:a 2} {:a 1})
map-kv-at-keys [f keyseq m]
Maps a function over the key/value pairs of an associate collection, but only on given keys.
(cljs-utils/map-kv-at-keys #(vec [(dec %1) (inc %2)]) [8 7] {9 1, 8 2, 7 3})
;; => {9 1, 7 3, 6 4}
map-vals-at-keys [f keyseq m]
Maps a function over the values pairs of an associate collection, but only on given keys.
(cljs-utils/map-vals-at-keys inc [:b :c] {:a 1 :b 2 :c 3})
;; => {:a 1 :b 3 :c 4}
rand-str [n & [{:keys [:lowercase-only? :exclude-numbers?]}]]
Generates random alphanumeric string with n
characters.
(cljs-utils/rand-str 10)
;; => "LpvMscagIw"
(cljs-utils/rand-str 10 {:lowercase-only? true})
;; => "frtkeahqus"
safe-assoc-in [m ks v]
Invariant version of assoc-in. Returns unchanged map if ks
path is empty.
(cljs-utils/safe-assoc-in {:a 1} [:b :c] 2)
;; => {:a 1}
(cljs-utils/safe-assoc-in {:b {}} [:b :c] 2)
;; => {:b {:c 2}}
js-obj->clj [obj]
Converts JS object (instance of a class) into Clojure map
(:protocol (cljs-utils/js-obj->clj (aget js/window "location")))
;; => http:
kw->str [kw]
Stringifies keyword. In contrary to name
, preserves namespace as well
(cljs-utils/kw->str :some.long/name)
;; => "some.long/name"
transform-keys [t coll]
Recursively transforms all map keys in coll with t.
(cljs-utils/transform-keys inc {1 "a" 2 "a" 3 {4 "a"}})
;; => {2 "a" 3 "a" 4 {5 "a"}}
transform-vals [t coll]
Recursively transforms all map values in coll with t.
(cljs-utils/transform-vals (fn [v]
(if (number? v)
(inc v)
v))
{:a 1 :b 2 :c {:z 3}})
;; => {:a 2 :b 3 :c {:z 4}}
lein deps
# To run tests and rerun on changes
lein doo chrome tests
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close