-
Notifications
You must be signed in to change notification settings - Fork 0
/
operations.clj
167 lines (139 loc) · 5.71 KB
/
operations.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
(ns info.setmy.string.operations
"String operations/functionality."
(:gen-class)
(:require [clojure.string :as str]
[clojure.data.json :as json]
[clj-yaml.core :as yaml]
[info.setmy.collection.operations :as collection-ops]))
(def comma-string ",")
(def empty-string "")
(defn split-and-trim
"Return LazySeq"
[text & [delimiter]]
(let [separator (or delimiter comma-string)]
(->> (str/split text (re-pattern separator))
(map str/trim))))
(defn trim-list
[strings-list]
(map str/trim strings-list))
(defn to-boolean
[text & [default-value]]
(let [default-value (or default-value false)]
(if (nil? text)
default-value,
(let [lower-text (str/lower-case text)]
(cond
(or (= lower-text "false") (= lower-text "no")) false
(or (= lower-text "true") (= lower-text "yes")) true
:else (throw (ex-info "Invalid boolean value" {})))))))
(defn to-short
[text & [default-value]]
(let [default-value (or default-value 0)]
(if (nil? text)
default-value
(try
(Short/parseShort text)
(catch NumberFormatException _ default-value)))))
(defn to-int
[text & [default-value]]
(let [default-value (or default-value 0)]
(if (nil? text)
default-value
(try
(Integer/parseInt text)
(catch NumberFormatException _ default-value)))))
(defn to-long
[text & [default-value]]
(let [default-value (or default-value 0)]
(if (nil? text)
default-value
(try
(Long/parseLong text)
(catch NumberFormatException _ default-value)))))
(defn to-float
[text & [default-value]]
(let [default-value (or default-value 0.0)]
(if (nil? text)
default-value
(try
(Float/parseFloat text)
(catch NumberFormatException _ default-value)))))
(defn to-double
[text & [default-value]]
(let [default-value (or default-value 0.0)]
(if (nil? text)
default-value
(try
(Double/parseDouble text)
(catch NumberFormatException _ default-value)))))
(defn json-to-object
[text & [default-value]]
(let [default-value (or default-value {})]
(if (nil? text)
default-value
(json/read-str text))))
(defn yaml-to-object
[text & [default-value]]
(let [default-value (or default-value {})]
(if (nil? text)
default-value
(yaml/parse-string text))))
(defn find-named-placeholders
[text & [as-clean]]
(let [as-clean (if (nil? as-clean) true as-clean)
pattern (re-pattern "\\$\\{(.*?)\\}")
placeholders (re-seq pattern text)
unique-placeholders (distinct placeholders)]
(if as-clean
; placeholder is in form: [["${def}" "def"] ["${jkl}" "jkl"] ["${prs}" "prs"]]
(map second unique-placeholders)
(map first unique-placeholders))))
(defn replace-named-placeholder
[text place-holder-name replacement]
(let [replacement-string (if (nil? replacement) "" replacement)
placeholder (str "${" place-holder-name "}")]
(-> text
(str/replace (re-pattern (str (java.util.regex.Pattern/quote placeholder))) replacement-string))))
(defn combined-list
"Creates a new list by multiplying elements from two input lists, and joining them with the specified join_text separator.
**Parameters**
* **list1** (list) The first input list.
* **list2** (list) The second input list.
* **join_text** (str, optional) The separator used to join elements from the input lists. Defaults to an empty string.
**Returns** A new list where elements from the two input lists are multiplied and joined with the join_text separator.
"
[list1 list2 & [join-text]]
(let [join-text (or join-text "")]
(->> (collection-ops/product-as-pairs list1 list2)
(filter
(fn [pair] (if (or (nil? (first pair)) (nil? (second pair))) false true)))
(map
(fn [pair] (str (first pair) join-text (second pair)))))))
(defn combined-by-function-list
"Combines two lists of string into a new list using a join_text separator
and optionally filters the resulting elements based on a given function.
**Parameters**
* **list1** (list) The first list of string.
* **list2** (list) The second list of string.
* **join_text** (str, optional) The separator text used to join elements from list1 and list2. Default is an empty string.
* func (function, optional) A filtering function that takes a combined string as input and returns True or False.
If provided, only elements for which func returns True will be included in the result. Default is None.
**Returns** A list of combined and optionally filtered string.
"
[list1 list2 & [join-text func]]
(let [join-text (or join-text empty-string)
;func (or func (fn [x] true))
result (atom [])]
(doseq [item1 list1
item2 list2]
(let [sum-item (str item1 join-text item2)]
(when (and (not (nil? func)) (func sum-item)) ;(when (func sum-item)
(swap! result conj sum-item))))
;(println "combined-by-function-list:" @result)
@result))
(defn nil-to-default
[text & [default-text]]
(let [default-text (or default-text empty-string)]
(if (nil? text)
default-text
text)))