/
do_not_use.cljc
173 lines (150 loc) · 6.02 KB
/
do_not_use.cljc
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
(ns com.fulcrologic.fulcro.algorithms.do-not-use
"Some misc. utility functions. These are primarily meant for internal use, and are subject to
relocation and removal in the future.
You have been warned. Changes to this ns (or its complete removal)
will not be considered breaking changes to the library, and no mention of said changes
will even appear in the changelog."
(:require
[taoensso.timbre :as log]
[edn-query-language.core :as eql]
#?@(:cljs [[goog.object :as gobj]
[goog.crypt :as crypt]
[goog.crypt.base64 :as b64]])
[clojure.spec.alpha :as s]
[clojure.string :as str])
#?(:clj (:import
[clojure.lang Atom]
[java.util Base64]
[java.nio.charset StandardCharsets])))
(defn atom? [a] #?(:cljs (satisfies? IAtom a) :clj (instance? Atom a)))
(defn join-entry [expr]
(let [[k v] (if (seq? expr)
(ffirst expr)
(first expr))]
[(if (list? k) (first k) k) v]))
(defn join? [x]
#?(:cljs {:tag boolean})
(let [x (if (seq? x) (first x) x)]
(map? x)))
(defn recursion?
#?(:cljs {:tag boolean})
[x]
(or #?(:clj (= '... x)
:cljs (symbol-identical? '... x))
(number? x)))
(defn union?
#?(:cljs {:tag boolean})
[expr]
(let [expr (cond-> expr (seq? expr) first)]
(and (map? expr)
(map? (-> expr first second)))))
(defn join-key [expr]
(cond
(map? expr) (let [k (ffirst expr)]
(if (list? k)
(first k)
(ffirst expr)))
(seq? expr) (join-key (first expr))
:else expr))
(defn join-value [join]
(second (join-entry join)))
(defn mutation-join? [expr]
(and (join? expr) (symbol? (join-key expr))))
(defn now
"Returns current time in ms."
[]
#?(:clj (java.util.Date.)
:cljs (js/Date.)))
(defn deep-merge
"Merges nested maps without overwriting existing keys."
[& xs]
(if (every? map? xs)
(apply merge-with deep-merge xs)
(last xs)))
(defn conform! [spec x]
(let [rt (s/conform spec x)]
(when (s/invalid? rt)
(throw (ex-info (s/explain-str spec x)
(s/explain-data spec x))))
rt))
(defn destructured-keys
"Calculates the keys that are being extracted in a legal map destructuring expression.
- `m`: A map containing legal CLJ destructurings, like `{:keys [a] x :x ::keys [y]}`
Returns a set of all keywords that are destructured in the map.
Example:
```
(destructured-keys {:a/keys [v] sym :other-key}) => #{:a/v :other-key}
```
"
[m]
(let [regular-destructurings (reduce
(fn [acc k]
(if (and (keyword? k) (= "keys" (name k)))
(let [simple-syms (get m k)
included-ns (namespace k)
source-keys (into #{}
(map (fn [s]
(cond
included-ns (keyword included-ns (name s))
(and (keyword? s) (namespace s)) s
(namespace s) (keyword (namespace s) (name s))
:else (keyword s))))
simple-syms)]
(into acc source-keys))
acc))
#{}
(keys m))
symbol-destructrings (reduce
(fn [acc k]
(if (symbol? k)
(conj acc (get m k))
acc))
#{}
(keys m))]
(into regular-destructurings symbol-destructrings)))
#?(:cljs
(defn char-code
"Convert char to int"
[c]
(cond
(number? c) c
(and (string? c) (== (.-length c) 1)) (.charCodeAt c 0)
:else (throw (js/Error. "Argument to char must be a character or number")))))
(def ^:dynamic *fast-base64*
"Bind to true in CLJS to use a faster base64 encode/decode, but one that will not work correctly on data that has alternate character encodings."
false)
(defn base64-encode
"Encode a string to UTF-8 and encode the result to base 64"
[str]
#?(:clj (.encodeToString (Base64/getEncoder) (.getBytes str "UTF-8"))
:cljs (if *fast-base64*
(b64/encodeString str)
(let [bytes (crypt/stringToUtf8ByteArray (clj->js str))] ;; First convert our JavaScript string from UCS-2/UTF-16 to UTF-8 bytes
(b64/encodeString (str/join "" (map char bytes))))))) ;; base64 encode that byte array to a string
(defn base64-decode
[str]
#?(:clj (String. (.decode (Base64/getDecoder) ^String str) (StandardCharsets/UTF_8))
:cljs (if *fast-base64*
(b64/decodeString str)
(let [bytes (map char-code (vec (b64/decodeString str)))] ;; b64/decodeString produces essentially a byte array
(crypt/utf8ByteArrayToString (clj->js bytes)))))) ;; Convert the byte array to a valid JavaScript string (either UCS-2 or UTF-16)
(defn ast->query
"Workaround for bug in EQL 0.0.9 and earlier"
[ast]
(as-> (eql/ast->expr ast true) <>
(if (vector? <>)
<>
[<>])))
(defn dev-check-query
"Runtime check that the (typically root) query looks valid
(though the Spec it uses might be both more strict / more lax than the code
so do not take it absolutely.
"
[query component-name-fn]
(when (and #?(:clj true :cljs goog.DEBUG)
query
(false? (s/valid? ::eql/query query)))
(log/error (str "The composed root query is not valid EQL. The app may crash. See `(comp/get-query "
(some-> query meta :component component-name-fn) ")`")
query))
query)