Skip to content

district0x/district-cljs-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

district-cljs-utils

Build Status

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.

Installation

Add [district0x/district-cljs-utils "1.0.4"] into your project.clj
Include [district.cljs-utils] in your CLJS file

API Overview

district.cljs-utils

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}}

Ensures a collection.

(first (cljs-utils/collify 1))
;; => 1

(first (cljs-utils/collify [1]))
;; => 1

Returns true if not nil

(cljs-utils/not-nil? 1)
;; => true

Descending sort

(cljs-utils/sort-desc [2 1 3])
;; => [3 2 1]

Descending sort by key-fn

(cljs-utils/sort-by-desc :a [{:a 2} {:a 1} {:a 3}])
;; => ({:a 3} {:a 2} {:a 1})

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}

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}

Generates random alphanumeric string with n characters.

(cljs-utils/rand-str 10)
;; => "LpvMscagIw"

(cljs-utils/rand-str 10 {:lowercase-only? true})
;; => "frtkeahqus"

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}}

Converts JS object (instance of a class) into Clojure map

(:protocol (cljs-utils/js-obj->clj (aget js/window "location")))
;; => http:

Stringifies keyword. In contrary to name, preserves namespace as well

(cljs-utils/kw->str :some.long/name)
;; => "some.long/name"

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"}}

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}}

Development

lein deps
# To run tests and rerun on changes
lein doo chrome tests

About

Set of functions helpful for working with Clojurescript

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published