-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.clj
303 lines (261 loc) · 7.04 KB
/
util.clj
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
(ns diction.util
(:require [cheshire.core :as cheshire]
[cheshire.generate :refer [add-encoder encode-str remove-encoder]]
[clj-time.core :as t]
[clojure.string :as str]
[clojure.walk :as walk])
(:import (java.util Random UUID)))
(def json-encode-to-string-classes
[java.util.regex.Pattern
org.joda.time.DateTime])
(def add-encoders-result (reduce #(conj %
(add-encoder %2
(fn [c jsonGenerator]
(.writeString jsonGenerator (str c)))))
nil
json-encode-to-string-classes))
(def joda-class (class (t/now)))
(defn polite-assoc
"Politely associates in map `m` with key `k` the value `v` iff
both the key `k` and `v` are non-nil AND there is no existing value
in the map `m` located at key `k`."
[m k v]
(try
(if (and (not (nil? k)) (not (nil? v)))
(if (nil? (get m k))
(assoc m k v)
m)
m)
(catch Exception _
m)))
(defn polite-assoc-in
"Politely associates in map `m` with nested keys `ks` the value `v` iff
both the keys `ks` and `v` are non-nil AND there is no existing value
in the map `m` located at the keys `ks`."
[m ks v]
(try
(if (and (not (empty? ks)) (not (nil? v)))
(if (nil? (get-in m ks))
(assoc-in m ks v)
m)
m)
(catch Exception _
m)))
(defn strict-int?
"Determines if `x` is a strict `int`."
[x]
(instance? Integer x))
(defn strict-long?
"Determines if `x` is strict `long`."
[x]
(instance? Long x))
(defn strict-float?
"Determines if `x` is a strict `float`."
[x]
(instance? Float x))
(defn strict-double?
"Determines if `x` is a strict `double`."
[x]
(instance? Double x))
(defn joda?
"Determine if `x` is a Joda DateTime instance."
[x]
(when x
(= joda-class (class x))))
(defn vls?
"Determines if `x` is a vector, set, or list."
[x]
(or (sequential? x) (set? x)))
(defn stack-assoc
"Associates key `k` and value `v` to map `m`."
[m k v]
(assoc m k (if-let [cv (get m k)]
(if (vls? cv)
(conj cv v)
[cv v])
[v])))
(defn ->str
"Convert `x` to string."
[x]
(when x
(if (keyword? x)
(-> x str (subs 1))
(str x))))
(defn ->kw
"Convert `xs to keyword."
[x]
(if (keyword? x)
x
(-> x
str
keyword)))
(def rndm (Random.))
(defn ->json
"Convert map `m` to JSON string."
[m]
(cheshire/generate-string m {:pretty true}))
(defn ->edn
"Convert JSON string `s` to map."
[s]
(when s
(cheshire/parse-string s true)))
(defn apply-f-to-keys
"Recursively transforms all map keys from keywords to strings."
{:added "1.1"}
[f m]
(walk/postwalk (fn [x] (if (map? x) (into {} (map f x)) x)) m))
(defn ->snake
"Convert `s` to snake case."
[s]
(-> s
->str
str/lower-case
(str/replace "-" "_")))
(defn ->skewer
"Convert `s` to skewer case."
[s]
(-> s
->str
str/trim
str/lower-case
(str/replace " " "-")
(str/replace "_" "-")))
(defn ->skewer-kw
"Converts `x` to skewer keyword."
[x]
(-> x
->skewer
keyword))
(defn walk-apply-key-f
"Apply key function `kv` to key-value pair `kv`. Used to walk map."
[kf kv]
(let [[k v] kv]
[(kf k) v]))
(def snakify-keys (partial walk-apply-key-f ->snake))
(def skewer-keys (partial walk-apply-key-f ->skewer-kw))
(def snake-keys (partial apply-f-to-keys snakify-keys))
(def skewer-keys (partial apply-f-to-keys skewer-keys))
(def default-label-replacements {"Id" "ID"
"Ids" "IDs"
"Ssn" "SSN"
"Num" "No."
"Cd" "Code"
"Svc" "Service"
"Pct" "Percent"
"Roi" "ROI"
"Mo" "Month"
"Yr" "Year"
"Wk" "Week"
"Loe" "LOE"
"No" "No."
"Ein" "EIN"
"Required Un" "Required (Unqualified)"
"Optional Un" "Optional (Unqualified)"})
(def label-replacements (atom default-label-replacements))
(defn label-replacements!
"Sets the label replacements with `lrs` map.
`lrs` should have the string key with only the first letter capitalized
(`Id` no `id`), and the value the prettified word or abbreviation."
[lrs]
(reset! label-replacements lrs))
(defn replace-with
"Returns value in replacement map `rs` if the key `v` exists
in `rs`. Otherwise, returns `v`."
[rs v]
(get rs v v))
(defn replace-labels
"Replace label `v` with replacement, otherwise pass through `v`."
[v]
(replace-with @label-replacements v))
(defn normalize-words
"Normalize each word in `s`."
[s]
(->> s
->str
str/lower-case
(#(str/split % #" "))
(map str/capitalize)
(map replace-labels)
(str/join " ")))
(defn generate-random-uuid
"Generates random UUID string."
[]
(str (UUID/randomUUID)))
(defn labelize
"Converts `x` to a label."
[x]
(-> x
->str
(str/replace "-" " ")
(str/replace "_" " ")
normalize-words
str/trim))
(defn labelize-element
"Converts element name `x` to a label."
[x]
(let [sx (->str x)
splt (str/split sx #"\/")
lbls (map #(-> %
(str/replace "-" " ")
(str/replace "_" " ")
normalize-words
str/trim)
splt)]
(str/join " / " lbls)))
(defn safe-nth
"Safe nth against collection `c` and index `ndx`. Returns `nil` if exception."
[c ndx]
(try
(nth c ndx)
(catch Exception _)))
(defn random-int
"Generates random int."
[]
(.nextInt rndm))
(defn random-long
"Generates random long."
[]
(.nextLong rndm))
(defn random-float
"Generates random float."
[]
(float (* (if (odd? (System/currentTimeMillis)) -1 1)
(float (rand))
(float (dec Float/MAX_VALUE)))))
(defn random-double
"Generates random double."
[]
(* (if (odd? (System/currentTimeMillis)) -1 1)
(rand)
(dec Double/MAX_VALUE)))
(defn coin-toss?
"Equal chance for true or false."
[]
(odd? (System/currentTimeMillis)))
(defn force-in-range
"Force numeric value `v` in range with `min` and `max`."
[min max v]
(try
(if min
(if (< v min)
min
(if (> v max)
max
v))
(if max
(if (> v max)
max
v)
v))
(catch Exception _
v)))
(defn polite-conj
"Politely conjoins element `x` to list `v` iff `x` is non-nil."
[v x & xs]
(if (empty? xs)
(if (nil? x)
v
(conj (or v []) x))
(reduce #(polite-conj % %2)
v
(cons x xs))))