/
util.clj
73 lines (57 loc) · 2.1 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
(ns clj-cctray.util
"Utility functions."
(:require [clojure.string :refer [join lower-case split replace trim]])
(:refer-clojure :exclude [join replace]))
(defn in?
"Returns true if the given element is in the given sequence."
[seq elm]
(some #(= elm %) seq))
(defn- threading-join [coll separator]
(join separator coll))
(defn keywordize-camel
"Converts the given camel cased string into a clojure style keyword.
For example: \"CamelCase\" => :camel-case"
[camel-str]
(if camel-str
(->
(name camel-str)
(split #"(?<=[a-z])(?=[A-Z])")
(threading-join "-")
(lower-case)
(keyword))))
(defn keywordize-camel-keys
"Converts all the keys in the given map into keywords."
[m]
(into {} (map (fn [[k v]] [(keywordize-camel k) v]) m)))
(defn sentenceize
"Sentenceizes the given string which means camel, snake, kebab and dot cased strings are turned into normal sentences
with spaces. This function will not split the following:
1. Multiple uppercased letters in a row
2. Digits separated by dots
3. x or * separated by dots
This is in an attempt to keep acronyms/initialisms and version numbers from getting split.
For example:
\"CamelCased_SNAKE-kebab.dot_JSON-1.2.x\" => \"Camel Cased SNAKE kebab dot JSON 1.2.x\""
[str]
(if-not (nil? str)
(-> str
(replace #"[-_]+", " ")
(replace #"\.(?![\dx*])|(?<![\dx*])\.", " ")
(replace #"([a-z])([A-Z])", "$1 $2")
trim)))
(defn normalise-string
"Normalises the given string which means it is lower cased and sentenceized.
For example:
\"CamelCased_SNAKE-kebab.DoT\" => \"camel cased snake kebab dot\""
[str]
(if-not (nil? str)
(lower-case (sentenceize str))))
(defn normalise-key
"Normalises the given key in the given map which means it is lower cased and sentenceized.
For example:
\"CamelCased_SNAKE-kebab.DoT\" => \"camel cased snake kebab dot\""
[key map]
(if-let [value (get map key)]
(merge map {(keyword (str "unnormalised-" (name key))) value
key (normalise-string value)})
map))