-
Notifications
You must be signed in to change notification settings - Fork 73
/
utils.cljs
59 lines (53 loc) · 1.88 KB
/
utils.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
57
58
59
(ns dommy.utils)
(defn dissoc-in
"Dissociate this keyseq from m, removing any empty maps created as a result
(including at the top-level)."
[m [k & ks]]
(when m
(if-let [res (and ks (dissoc-in (m k) ks))]
(assoc m k res)
(let [res (dissoc m k)]
(when-not (empty? res)
res)))))
(defn ->Array [array-like]
(.call js/Array.prototype.slice array-like))
(defn as-str
"Coerces strings and keywords to strings, while preserving namespace of
namespaced keywords"
[s]
(if (keyword? s)
(str (some-> (namespace s) (str "/")) (name s))
s))
(defn ^boolean class-match?
"Does `class-name` string have class starting at index idx.
only will be used when Element::classList doesn't exist"
[class-name class idx]
(and
;; start
(or (zero? idx) (identical? \space (.charAt class-name (dec idx))))
;; stop
(let [total-len (.-length class-name)
stop (+ idx (.-length class))]
(when (<= stop total-len)
(or (identical? stop total-len)
(identical? \space (.charAt class-name stop)))))))
(defn ^number class-index
"Finds the index of class in a space-delimited class-name
only will be used when Element::classList doesn't exist"
[class-name class]
(loop [start-from 0]
(let [i (.indexOf class-name class start-from)]
(when (>= i 0)
(if (class-match? class-name class i)
i
(recur (+ i (.-length class))))))))
(defn remove-class-str [init-class-name class]
(loop [class-name init-class-name]
(let [class-len (.-length class-name)]
(if-let [i (class-index class-name class)]
(recur (let [end (+ i (.-length class))]
(str (if (< end class-len)
(str (.substring class-name 0 i)
(.substr class-name (inc end)))
(.substring class-name 0 (dec i))))))
class-name))))