-
Notifications
You must be signed in to change notification settings - Fork 13
/
parse.cljc
223 lines (195 loc) · 9.3 KB
/
parse.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
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
; Copyright (c) Alan Thompson. All rights reserved.
; The use and distribution terms for this software are covered by the Eclipse Public
; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can be found in the
; file epl-v10.html at the root of this distribution. By using this software in any
; fashion, you are agreeing to be bound by the terms of this license.
; You must not remove this notice, or any other, from this software.
(ns tupelo.parse
"Utils for parsing string values. Provides a thin Clojure wrapper around java native
parsing functions such as java.lang.Float/parseFloat. Unlike the original java
functions, these native-Clojure functions can be used as higher-order functions in maps,
function arguments, etc. Each function also provides an optional default-value which
will be returned if there is an exception during parsing."
(:refer-clojure :exclude [parse-long parse-double])
(:require
[clojure.string :as str]
[clojure.tools.reader.edn :as edn]
[clojure.walk :as walk]
[schema.core :as s]
[tupelo.core :as t]
[tupelo.schema :as tsk]
))
; #todo: write doc page
; #todo: convert args from [str-val & opts] -> [str-val & {:as opts} ]
(defn edn-parsible
"Traverses a data structure to ensure it can be serialized with `pr-str` and read via
clojure.tools.reader.edn/read-string. All non-parsible content is replaced
with `::non-parsible-object`. "
[data]
(let [edn-parse-roundtrip (fn [item]
(t/with-exception-default ::edn-non-parsible
(let [item-str (pr-str item)
item-edn (edn/read-string item-str)]
(when-not (= item item-edn)
(throw (ex-info "edn-non-parsible" {:data data})))
item)))
data-unlazy (t/unlazy data) ; coerce to map/vector/string wherever possible
data-parsible (walk/postwalk edn-parse-roundtrip data-unlazy)]
data-parsible))
#?(:clj
(do
(defn parse-byte
"
(parse-byte str-val )
(parse-byte str-val :default default-val )
A thin wrapper around java.lang.Byte/parseByte. Parses the string str-val into a byte.
If the optional default-val is specified, it will be returned in the event of an
exception."
[str-val & opts]
{:pre [(string? str-val)]}
(let [opts-map (apply hash-map opts)
default-val (get opts-map :default ::none)]
(if (= default-val ::none)
(Byte/parseByte str-val)
(t/with-exception-default default-val (Byte/parseByte str-val)))))
(defn parse-short
"
(parse-short str-val)
(parse-short str-val :default default-val )
A thin wrapper around java.lang.Short/parseShort. Parses the string str-val into a short.
If the optional default-val is specified, it will be returned in the event of an
exception."
[str-val & opts]
{:pre [(string? str-val)]}
(let [opts-map (apply hash-map opts)
default-val (get opts-map :default ::none)]
(if (= default-val ::none)
(Short/parseShort str-val)
(t/with-exception-default default-val (Short/parseShort str-val)))))
(defn parse-int
"
(parse-int str-val)
(parse-int str-val :default default-val )
A thin wrapper around java.lang.Integer/parseInt Parses the string str-val into a integer.
If the optional default-val is specified, it will be returned in the event of an
exception."
[str-val & opts]
{:pre [(string? str-val)]}
(let [opts-map (apply hash-map opts)
default-val (get opts-map :default ::none)]
(if (= default-val ::none)
(Integer/parseInt str-val)
(t/with-exception-default default-val (Integer/parseInt str-val)))))
(defn parse-long
"
(parse-long str-val)
(parse-long str-val :default default-val )
A thin wrapper around java.lang.Long/parseLong. Parses the string str-val into a long.
If the optional default-val is specified, it will be returned in the event of an
exception."
[str-val & opts]
{:pre [(string? str-val)]}
(let [opts-map (apply hash-map opts)
default-val (get opts-map :default ::none)]
(if (= default-val ::none)
(Long/parseLong str-val)
(t/with-exception-default default-val (Long/parseLong str-val)))))
(defn parse-float
"
(parse-float str-val
(parse-float str-val :default default-val )
A thin wrapper around java.lang.Float/parseFloat. Parses the string str-val into a float.
If the optional default-val is specified, it will be returned in the event of an
exception."
[str-val & opts]
{:pre [(string? str-val)]}
(let [opts-map (apply hash-map opts)
default-val (get opts-map :default ::none)]
(if (= default-val ::none)
(Float/parseFloat str-val)
(t/with-exception-default default-val (Float/parseFloat str-val)))))
(defn parse-double
"
(parse-double str-val
(parse-double str-val :default default-val )
A thin wrapper around java.lang.Double/parseDouble. Parses the string str-val into a double.
If the optional default-val is specified, it will be returned in the event of an
exception."
[str-val & opts]
{:pre [(string? str-val)]}
(let [opts-map (apply hash-map opts)
default-val (get opts-map :default ::none)]
(if (= default-val ::none)
(Double/parseDouble str-val)
(t/with-exception-default default-val (Double/parseDouble str-val)))))
(def ^:no-doc unspecified ::unspecified) ; #todo make all use Var instead of ::none ???
(s/defn parse-decimal
"A thin wrapper around java.math/BigDecimal(String value). Parses the string str-val into a BigDecimal.
An optional default value may be specified in the opts map; is present, it will be returned in the event of an exception. "
([str-val :- s/Str] (parse-decimal {} str-val))
([opts :- tsk/KeyMap
str-val :- s/Str]
(let [default-val (get opts :default unspecified)]
(if (= default-val unspecified)
(BigDecimal. str-val)
(t/with-exception-default default-val
(BigDecimal. str-val))))))
#_(defn parse-xxxx
"( [str-val]
[str-val :default default-val] )
A thin wrapper around java.lang.XXXX/parseXXXX. Parses the string str-val into a xxxx.
If the optional default-val is specified, it will be returned in the event of an
exception."
[str-val & opts]
{:pre [(string? str-val)]}
(let [opts-map (apply hash-map opts)
default-val (get opts-map :default ::none)]
(if (= default-val ::none)
(XXXX/parseXXXX str-val)
(t/with-exception-default default-val (XXXX/parseXXXX str-val)))))
; #awt TODO: finish other parse* functions
)) ; clj
#?(:cljs
(do
; These regexes are from the internet and aren't guarenteed to be perfect
(def regex-int
"Nominal regex for signed/unsigned integers"
#"^([+-]?[1-9]\d*|0)$")
(def regex-float
"Nominal regex for signed/unsigned floating-point numbers (possibly in scientific notation)"
#"[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?")
(s/defn parse-int :- s/Int ; #todo => tupelo.cljs.parse
"
(parse-int str-val)
(parse-int str-val :default default-val )
A thin wrapper around js/parseInt Parses the string str-val into a integer.
If the optional default-val is specified, it will be returned in the event of an
Nan."
([str-val :- s/Str]
(t/cond-it-> (str/trim str-val)
(not (re-matches regex-int it)) (throw (ex-info "parse-int: could not parse input value #1"
(t/vals->map str-val)))
true (js/parseInt it)
(js/isNaN it) (throw (ex-info "parse-int: could not parse input value #2"
(t/vals->map str-val)))))
([str-val default-val]
(t/with-exception-default default-val
(parse-int str-val))))
(s/defn parse-float :- s/Num
"
(parse-float str-val)
(parse-float str-val :default default-val )
A thin wrapper around js/parseFloat. Parses the string str-val into a float.
If the optional default-val is specified, it will be returned in the event of an
NaN."
([str-val :- s/Str]
(t/cond-it-> (str/trim str-val)
(not (re-matches regex-float it)) (throw (ex-info "parse-float: could not parse input value #1"
(t/vals->map str-val)))
true (js/parseFloat it)
(js/isNaN it) (throw (ex-info "parse-float: could not parse input value #2"
(t/vals->map str-val)))))
([str-val default-val]
(t/with-exception-default default-val
(parse-float str-val))))
))