-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
util.cljs
56 lines (50 loc) · 1.54 KB
/
util.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) 2016-2020 Andrey Antukh <niwi@niwi.nz>
(ns rumext.util)
(defn obj->map
"Convert shallowly an js object to cljs map."
[obj]
(let [keys (.keys js/Object obj)
len (alength keys)]
(loop [i 0
r (transient {})]
(if (< i len)
(let [key (aget keys i)]
(recur (unchecked-inc i)
(assoc! r (keyword key) (unchecked-get obj key))))
(persistent! r)))))
(defn map->obj
[o]
(let [m #js {}]
(run! (fn [[k v]] (unchecked-set m (name k) v)) o)
m))
(defn wrap-props
[props]
(cond
(object? props) (obj->map props)
(map? props) props
(nil? props) {}
:else (throw (ex-info "Unexpected props" {:props props}))))
(defn props-equals?
[eq? new-props old-props]
(let [old-keys (.keys js/Object old-props)
new-keys (.keys js/Object new-props)
old-keys-len (alength old-keys)
new-keys-len (alength new-keys)]
(if (identical? old-keys-len new-keys-len)
(loop [idx (int 0)]
(if (< idx new-keys-len)
(let [key (aget new-keys idx)
new-val (unchecked-get new-props key)
old-val (unchecked-get old-props key)]
(if ^boolean (eq? new-val old-val)
(recur (inc idx))
false))
true))
false)))
(defn symbol-for
[v]
(.for js/Symbol v))