-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.cljs
226 lines (172 loc) · 7.01 KB
/
format.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
(ns district.format
(:require
[cemerick.url :as url]
[cljs-time.core :as t]
[cljs-time.format :as time-format]
[clojure.string :as string]))
(def ^:dynamic *default-datetime-formatter* :rfc822)
(def ^:dynamic *default-local-datetime-formatter* *default-datetime-formatter*)
(def ^:dynamic *default-date-formatter* "EEE, dd MMM yyyy")
(def ^:dynamic *default-local-date-formatter* *default-date-formatter*)
(def ^:dynamic *default-locale* "en-US")
(def ^:dynamic *default-max-number-fraction-digits* 2)
(def ^:dynamic *default-max-currency-fraction-digits* 2)
(def ^:dynamic *default-min-currency-fraction-digits* nil)
(def ^:dynamic *default-max-token-fraction-digits* 2)
(def ^:dynamic *default-min-token-fraction-digits* nil)
(declare ensure-trailing-slash)
(defn- get-formatter [fmt]
(if (keyword? fmt)
(time-format/formatters fmt)
(time-format/formatter fmt)))
(defn format-datetime
([date]
(format-datetime date *default-datetime-formatter*))
([date formatter]
(when date
(time-format/unparse (get-formatter formatter) date))))
(defn format-local-datetime
([date]
(format-local-datetime date *default-local-datetime-formatter*))
([date formatter]
(when date
(time-format/unparse-local (get-formatter formatter) date))))
(defn format-date
([date]
(format-date date *default-date-formatter*))
([date formatter]
(when date
(time-format/unparse (get-formatter formatter) date))))
(defn format-local-date
([date]
(format-local-date date *default-local-date-formatter*))
([date formatter]
(when date
(time-format/unparse-local (get-formatter formatter) date))))
(defn format-number [x & [{:keys [:locale :max-fraction-digits :min-fraction-digits]
:or {locale *default-locale*
max-fraction-digits *default-max-number-fraction-digits*}}]]
(when x
(.toLocaleString x locale #js {:maximumFractionDigits max-fraction-digits
:minimumFractionDigits min-fraction-digits})))
(defn format-currency [x & [{:keys [:currency :locale :max-fraction-digits :min-fraction-digits]
:or {locale *default-locale*
max-fraction-digits *default-max-currency-fraction-digits*
min-fraction-digits *default-min-currency-fraction-digits*}}]]
(when x
(.toLocaleString x locale #js {:maximumFractionDigits max-fraction-digits
:minimumFractionDigits min-fraction-digits
:style "currency"
:currency currency})))
(defn format-token
[x & [{:keys [:token :locale :max-fraction-digits :min-fraction-digits]
:or {locale *default-locale*
max-fraction-digits *default-max-token-fraction-digits*
min-fraction-digits *default-min-token-fraction-digits*}}]]
(when x
(str (.toLocaleString x locale #js {:maximumFractionDigits max-fraction-digits
:minimumFractionDigits min-fraction-digits})
(when token
(str " " token)))))
(defn format-eth [x & [opts]]
(format-token x (merge {:token "ETH"} opts)))
(defn format-dnt [x & [opts]]
(format-token x (merge {:token "DNT"} opts)))
(defn format-number-metric [x & [opts]]
(when x
(cond
(< x 1000) (format-number x opts)
(< 999 x 1000000) (str (format-number (/ x 1000) opts) "K")
(< 999999 x) (str (format-number (/ x 1000000) opts) "M"))))
(defn etherscan-addr-url
([address]
(str "https://etherscan.io/address/" address))
([root-url address]
(str (ensure-trailing-slash root-url) "address/" address)))
(defn etherscan-tx-url
([tx-hash]
(str "https://etherscan.io/tx/" tx-hash))
([root-url tx-hash]
(str (ensure-trailing-slash root-url) "tx/" tx-hash)))
(defn time-ago
([from-time]
(time-ago from-time (t/now)))
([from-time to-time]
(when to-time
(let [units [{:name "second" :limit 60 :in-second 1}
{:name "minute" :limit 3600 :in-second 60}
{:name "hour" :limit 86400 :in-second 3600}
{:name "day" :limit 604800 :in-second 86400}
{:name "week" :limit 2629743 :in-second 604800}
{:name "month" :limit 31556926 :in-second 2629743}
{:name "year" :limit nil :in-second 31556926}]
diff (t/in-seconds (t/interval from-time to-time))]
(if (< diff 5)
"just now"
(let [unit (first (drop-while #(or (>= diff (:limit %))
(not (:limit %)))
units))]
(-> (/ diff (:in-second unit))
js/Math.floor
int
(#(str % " " (:name unit) (when (> % 1) "s") " ago")))))))))
(defn pluralize
"Taken from reagent-utils
pluralizes the word based on the number of items
(util/pluralize [\"John\"] \"lad\")
(util/pluralize [\"John\" \"James\"] \"lad\")
(util/pluralize [\"Alice\"] \"lad\" \"y\" \"ies\")"
[n & [word ending1 ending2 :as opts]]
(let [plural (case (count opts)
1 "s"
2 ending1
3 ending2)
singular (case (count opts)
(list 1 2) ""
3 ending1)]
(str n " " word (if (== 1 n) singular plural))))
(defn truncate
"Truncate a string with suffix (ellipsis by default) if it is
longer than specified length."
([string length]
(truncate string length "..."))
([string length suffix]
(let [string-len (count string)
suffix-len (count suffix)]
(if (<= string-len length)
string
(str (subs string 0 (- length suffix-len)) suffix)))))
(defn format-bool [x]
(if x "true" "false"))
(def time-unit->text {:days "day" :hours "hour" :minutes "minute" :seconds "second"})
(def time-unit->text-short {:days "day" :hours "hour" :minutes "min." :seconds "sec."})
(defn format-time-unit [unit amount & [{:keys [:short?]}]]
(let [f (if short? time-unit->text-short time-unit->text)
unit-text (f unit)]
(if (and short? (string/ends-with? unit-text "."))
(pluralize amount unit-text "")
(pluralize amount unit-text))))
(defn format-time-units [{:keys [:seconds :minutes :hours :days]} & [opts]]
;; To ensure proper order
(.slice
(reduce (fn [acc [unit amount]]
(if (pos? amount)
(str acc (format-time-unit unit amount opts) " ")
acc))
""
[[:days days] [:hours hours] [:minutes minutes] [:seconds seconds]])
0 -1))
(defn format-url [path query-map]
(str path "?" (url/map->query query-map)))
(defn format-namespaced-kw [kw]
(when kw
(str (when-let [n (namespace kw)] (str n "/")) (name kw))))
(defn format-percentage [t p]
(str (int (js-invoke js/Math "fround" (* (/ t p) 100.0))) "%"))
(defn ensure-trailing-slash [s]
(str s
(when-not (string/ends-with? s "/")
"/")))
(defn clj->json
[coll]
(.stringify js/JSON (clj->js coll)))