/
forms.cljs
233 lines (218 loc) · 7.5 KB
/
forms.cljs
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
(ns nsfw.forms
(:require [clojure.string :as str]))
(defn bound-input [!app path opts]
[:input
(merge
{:on-change (fn [e]
(swap! !app assoc-in path (.. e -target -value)))
:value (get-in @!app path)}
opts)])
(defn textarea [{:keys [!cursor path on-change] :as opts}]
(let [html-opts (dissoc opts :!cursor :path :on-change)]
[:textarea
(merge
{:on-change (fn [e]
(let [v (.. e -target -value)]
(when !cursor
(if path
(swap! !cursor assoc-in path v)
(reset! !cursor v)))
(when on-change
(on-change e))
nil))
:value (when !cursor
(if path
(get-in @!cursor path)
@!cursor))
:class "form-control"}
html-opts)]))
(defn select [{:keys [!cursor path] :as opts} children]
(let [html-opts (dissoc opts :!cursor :path)]
(vec
(concat
[:select
(merge
{:on-change (fn [e]
(when !cursor
(swap! !cursor assoc-in path (.. e -target -value))))
:value (if !cursor (get-in @!cursor path))}
html-opts)]
(for [{:keys [text value]} children]
^{:key (or value text)}
[:option {:value (or value text)}
text])))))
(defn number-char? [n]
(and (>= n 48)
(<= n 57)))
(defn input [& [opts]]
(let [adtl-opt-keys [:!cursor :path :parse-value :format-value
:on-change]
{:keys [!cursor path parse-value format-value
valid-char-code? type on-change]} opts
html-opts (apply dissoc opts adtl-opt-keys)
parse-value (or parse-value
(fn [s]
(if (empty? s)
nil
s)))
format-value (or format-value identity)
valid-char-code? (fn [{:keys [code ctrl? meta?]} value]
(if valid-char-code?
(or (<= code 47)
(>= code 91)
ctrl?
meta?
(valid-char-code? code value))
true))
checkbox? (when type
(= "checkbox" (name type)))
adtl-opts (if checkbox?
{:checked (when !cursor
(get-in @!cursor path))}
{:value (when !cursor
(format-value (get-in @!cursor path)))})
class (if checkbox?
{}
{:class "form-control"})
adtl-opts (if !cursor
(merge
adtl-opts
{:on-key-down
(fn [e]
(let [v (.. e -keyCode)]
(when-not (valid-char-code?
{:code v
:ctrl? (.-ctrlKey e)
:meta? (.-metaKey e)}
(get-in @!cursor path))
(.preventDefault e)
(.stopPropagation e))))
:on-change
(fn [e]
(if checkbox?
(swap! !cursor
assoc-in path
(parse-value (.. e -target -checked)))
(swap! !cursor
assoc-in path
(parse-value (.. e -target -value))))
(when on-change
(on-change e))
(.preventDefault e))})
adtl-opts)]
[:input (merge
adtl-opts
class
html-opts)]))
(defn card-type [s]
(when s
(condp #(re-find %1 %2) s
#"^4[0-9]{6,}$" :visa
#"^5[1-5][0-9]{5,}$" :mastercard
#"^3[47][0-9]{5,}$" :amex
#"^3(?:0[0-5]|[68][0-9])[0-9]{4,}$" :diners
#"^6(?:011|5[0-9]{2})[0-9]{3,}$" :discover
#"^(?:2131|1800|35[0-9]{3})[0-9]{3,}$" :jcb
nil)))
(defn numbers-only [s]
(when s
(->> (str/replace s #"[^0-9]" ""))))
#_(def test-cards ["4242424242424242" :visa
"401288888888188" :visa
"4000056655665556" :visa
"5555555555554444" :mastercard
"5200828282828210" :mastercard
"5105105105105100" :mastercard
"378282246310005" :amex
"371449635398431" :amex
"6011111111111117" :discover
"6011000990139424" :discover
"30569309025904" :diners
"38520000023237" :diners
"3530111333300000" :jcb
"3566002020360505" :jcb])
#_(doseq [[card type] (partition 2 test-cards)]
(prn card type (= type (card-type card))))
(defn cc-number [opts]
(input
(merge
{:valid-char-code? number-char?
:pattern "\\d*"
:format-value (fn [v]
(when v
(->> v
numbers-only
(partition-all 4)
(interpose " ")
flatten
(apply str))))
:parse-value (fn [s]
(when s
(->> (str/replace s #"\s+" "")
(take 20)
(apply str))))}
opts)))
(defn format-number [s & [{:keys [round]}]]
(when s
(let [round (or round 2)
s (str s)
parts (str/split s #"\.")
whole (first parts)
frac (second parts)
cleaned-whole (str/replace
(str whole)
#"[^\d]+"
"")
cleaned-frac (str/replace
(str frac)
#"[^\d]+"
"")
cleaned-frac (if round
(->> cleaned-frac
(take round)
(apply str))
cleaned-frac)]
(if (and (empty? cleaned-whole) (empty? cleaned-frac))
nil
(str
(->> cleaned-whole
reverse
(partition-all 3)
(interpose [","])
(apply concat)
reverse
(apply str))
(when-not (empty? cleaned-frac)
(str "." cleaned-frac)))))))
(defn format-price [s]
(when s
(let [formatted (format-number s)]
(if (empty? formatted)
nil
(str "$" formatted)))))
(defn parse-int [s]
(when s
(try
(let [res (js/parseInt
(str/replace
(str s)
#"[^\d]+"
""))]
(if (js/isNaN res)
nil
res))
(catch js/Error e nil))))
(defn format-us-phone [s]
(when s
(let [numbers (numbers-only s)]
(->> [(take 3 numbers)
(->> numbers
(drop 3)
(take 3))
(->> numbers
(drop 6)
(take 4))]
(remove empty?)
(interpose "-")
flatten
(apply str)))))