-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberFormat.purs
349 lines (287 loc) · 11.3 KB
/
NumberFormat.purs
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
module JS.Intl.NumberFormat
-- * Types
( NumberFormat
, NumberFormatOptions
, ToNumberFormatOptions
-- * Constructor
, new
, new_
-- * Methods
, supportedLocalesOf
, supportedLocalesOf_
, format
, formatRange
, formatRangeToParts
, formatToParts
, resolvedOptions
) where
import Prelude
import ConvertableOptions (class ConvertOption, class ConvertOptionsWithDefaults)
import ConvertableOptions as ConvertableOptions
import Data.Function.Uncurried (Fn2, Fn3)
import Data.Function.Uncurried as Function.Uncurried
import Data.Interval (DurationComponent(..))
import Effect (Effect)
import Effect.Uncurried (EffectFn1, EffectFn2)
import Effect.Uncurried as Effect.Uncurried
import Foreign (Foreign)
import Foreign as Foreign
import JS.Intl.Locale (Locale)
import JS.Intl.Options.CompactDisplay (CompactDisplay)
import JS.Intl.Options.CompactDisplay as CompactDisplay
import JS.Intl.Options.CurrencyDisplay (CurrencyDisplay)
import JS.Intl.Options.CurrencyDisplay as CurrencyDisplay
import JS.Intl.Options.CurrencySign (CurrencySign)
import JS.Intl.Options.CurrencySign as CurrencySign
import JS.Intl.Options.LocaleMatcher (LocaleMatcher)
import JS.Intl.Options.LocaleMatcher as LocaleMatcher
import JS.Intl.Options.Notation (Notation)
import JS.Intl.Options.Notation as Notation
import JS.Intl.Options.NumberFormatStyle (NumberFormatStyle)
import JS.Intl.Options.NumberFormatStyle as NumberFormatStyle
import JS.Intl.Options.RoundingIncrement (RoundingIncrement)
import JS.Intl.Options.RoundingIncrement as RoundingIncrement
import JS.Intl.Options.RoundingMode (RoundingMode)
import JS.Intl.Options.RoundingMode as RoundingMode
import JS.Intl.Options.RoundingPriority (RoundingPriority)
import JS.Intl.Options.RoundingPriority as RoundingPriority
import JS.Intl.Options.SignDisplay (SignDisplay)
import JS.Intl.Options.SignDisplay as SignDisplay
import JS.Intl.Options.TrailingZeroDisplay (TrailingZeroDisplay)
import JS.Intl.Options.TrailingZeroDisplay as TrailingZeroDisplay
import JS.Intl.Options.UnitDisplay (UnitDisplay)
import JS.Intl.Options.UnitDisplay as UnitDisplay
import JS.Intl.Options.UseGrouping (UseGrouping)
import JS.Intl.Options.UseGrouping as UseGrouping
import Unsafe.Coerce as Unsafe.Coerce
-- | Language-sensitive number formatting
foreign import data NumberFormat :: Type
-- | Options to pass to the `NumberFormat` constructor (`new` or `new_`)
-- |
-- | **Note:** Not all combinations of options are valid. See the specification
-- | (https://tc39.es/ecma402/#sec-intl.numberformat) or the MDN reference
-- | (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat)
type NumberFormatOptions =
( localeMatcher :: String
, numberingSystem :: String
, compactDisplay :: String
, style :: String
, currency :: String
, currencyDisplay :: String
, currencySign :: String
, notation :: String
, signDisplay :: String
, unit :: String
, unitDisplay :: String
, useGrouping :: Foreign
, roundingMode :: String
, roundingIncrement :: Int
, roundingPriority :: String
, trailingZeroDisplay :: String
, minimumIntegerDigits :: Int
, minimumFractionDigits :: Int
, maximumFractionDigits :: Int
, minimumSignificantDigits :: Int
, maximumSignificantDigits :: Int
)
defaultOptions :: { | NumberFormatOptions }
defaultOptions =
Unsafe.Coerce.unsafeCoerce {}
foreign import _new
:: EffectFn2
(Array Locale)
{ | NumberFormatOptions }
NumberFormat
data ToNumberFormatOptions = ToNumberFormatOptions
new
:: forall provided
. ConvertOptionsWithDefaults
ToNumberFormatOptions
{ | NumberFormatOptions }
{ | provided }
{ | NumberFormatOptions }
=> Array Locale
-> { | provided }
-> Effect NumberFormat
new locales provided =
Effect.Uncurried.runEffectFn2
_new
locales
options
where
options :: { | NumberFormatOptions }
options = ConvertableOptions.convertOptionsWithDefaults ToNumberFormatOptions defaultOptions provided
instance ConvertOption ToNumberFormatOptions "localeMatcher" LocaleMatcher String where
convertOption _ _ = LocaleMatcher.toString
instance ConvertOption ToNumberFormatOptions "localeMatcher" String String where
convertOption _ _ = identity
instance ConvertOption ToNumberFormatOptions "numberingSystem" String String where
convertOption _ _ = identity
instance ConvertOption ToNumberFormatOptions "compactDisplay" CompactDisplay String where
convertOption _ _ = CompactDisplay.toString
instance ConvertOption ToNumberFormatOptions "compactDisplay" String String where
convertOption _ _ = identity
instance ConvertOption ToNumberFormatOptions "style" NumberFormatStyle String where
convertOption _ _ = NumberFormatStyle.toString
instance ConvertOption ToNumberFormatOptions "style" String String where
convertOption _ _ = identity
instance ConvertOption ToNumberFormatOptions "currency" String String where
convertOption _ _ = identity
instance ConvertOption ToNumberFormatOptions "currencyDisplay" CurrencyDisplay String where
convertOption _ _ = CurrencyDisplay.toString
instance ConvertOption ToNumberFormatOptions "currencyDisplay" String String where
convertOption _ _ = identity
instance ConvertOption ToNumberFormatOptions "currencySign" CurrencySign String where
convertOption _ _ = CurrencySign.toString
instance ConvertOption ToNumberFormatOptions "currencySign" String String where
convertOption _ _ = identity
instance ConvertOption ToNumberFormatOptions "notation" Notation String where
convertOption _ _ = Notation.toString
instance ConvertOption ToNumberFormatOptions "notation" String String where
convertOption _ _ = identity
instance ConvertOption ToNumberFormatOptions "signDisplay" SignDisplay String where
convertOption _ _ = SignDisplay.toString
instance ConvertOption ToNumberFormatOptions "signDisplay" String String where
convertOption _ _ = identity
instance ConvertOption ToNumberFormatOptions "unit" DurationComponent String where
convertOption _ _ = case _ of
Second -> "second"
Minute -> "minute"
Hour -> "hour"
Day -> "day"
Week -> "week"
Month -> "month"
Year -> "year"
else instance ConvertOption ToNumberFormatOptions "unit" String String where
convertOption _ _ = identity
instance ConvertOption ToNumberFormatOptions "unitDisplay" UnitDisplay String where
convertOption _ _ = UnitDisplay.toString
instance ConvertOption ToNumberFormatOptions "unitDisplay" String String where
convertOption _ _ = identity
instance ConvertOption ToNumberFormatOptions "useGrouping" UseGrouping Foreign where
convertOption _ _ = Foreign.unsafeToForeign <<< UseGrouping.toString
instance ConvertOption ToNumberFormatOptions "useGrouping" String Foreign where
convertOption _ _ = Foreign.unsafeToForeign
instance ConvertOption ToNumberFormatOptions "useGrouping" Boolean Foreign where
convertOption _ _ = Foreign.unsafeToForeign
instance ConvertOption ToNumberFormatOptions "useGrouping" Foreign Foreign where
convertOption _ _ = identity
instance ConvertOption ToNumberFormatOptions "roundingMode" RoundingMode String where
convertOption _ _ = RoundingMode.toString
instance ConvertOption ToNumberFormatOptions "roundingMode" String String where
convertOption _ _ = identity
instance ConvertOption ToNumberFormatOptions "roundingIncrement" RoundingIncrement Int where
convertOption _ _ = RoundingIncrement.toInt
instance ConvertOption ToNumberFormatOptions "roundingIncrement" Int Int where
convertOption _ _ = identity
instance ConvertOption ToNumberFormatOptions "roundingPriority" RoundingPriority String where
convertOption _ _ = RoundingPriority.toString
instance ConvertOption ToNumberFormatOptions "roundingPriority" String String where
convertOption _ _ = identity
instance ConvertOption ToNumberFormatOptions "trailingZeroDisplay" TrailingZeroDisplay String where
convertOption _ _ = TrailingZeroDisplay.toString
instance ConvertOption ToNumberFormatOptions "trailingZeroDisplay" String String where
convertOption _ _ = identity
instance ConvertOption ToNumberFormatOptions "minimumIntegerDigits" Int Int where
convertOption _ _ = identity
instance ConvertOption ToNumberFormatOptions "minimumFractionDigits" Int Int where
convertOption _ _ = identity
instance ConvertOption ToNumberFormatOptions "maximumFractionDigits" Int Int where
convertOption _ _ = identity
instance ConvertOption ToNumberFormatOptions "minimumSignificantDigits" Int Int where
convertOption _ _ = identity
instance ConvertOption ToNumberFormatOptions "maximumSignificantDigits" Int Int where
convertOption _ _ = identity
new_
:: Array Locale
-> Effect NumberFormat
new_ locales =
new locales defaultOptions
foreign import _supportedLocalesOf
:: Fn2
(Array Locale)
{ | NumberFormatOptions }
(Array String)
supportedLocalesOf
:: forall provided
. ConvertOptionsWithDefaults
ToNumberFormatOptions
{ | NumberFormatOptions }
{ | provided }
{ | NumberFormatOptions }
=> Array Locale
-> { | provided }
-> Array String
supportedLocalesOf locales provided =
Function.Uncurried.runFn2
_supportedLocalesOf
locales
options
where
options :: { | NumberFormatOptions }
options = ConvertableOptions.convertOptionsWithDefaults ToNumberFormatOptions defaultOptions provided
supportedLocalesOf_
:: Array Locale
-> Array String
supportedLocalesOf_ locales =
supportedLocalesOf locales defaultOptions
foreign import _format
:: Fn2
NumberFormat
Number
String
-- | Formats a number according to the locale and formatting options of the
-- | `NumberFormat`
format :: NumberFormat -> Number -> String
format = Function.Uncurried.runFn2 _format
foreign import _formatRange
:: Fn3
NumberFormat
Number
Number
String
-- | Formats a range of numbers according to the locale and formatting options
-- | of the `NumberFormat`
formatRange :: NumberFormat -> Number -> Number -> String
formatRange = Function.Uncurried.runFn3 _formatRange
foreign import _formatRangeToParts
:: Fn3
NumberFormat
Number
Number
(Array { type :: String, value :: String })
-- | Returns an array of objects containing the locale-specific tokens from
-- | which it is possible to build custom strings while preserving the
-- | locale-specific parts
formatRangeToParts :: NumberFormat -> Number -> Number -> Array { type :: String, value :: String }
formatRangeToParts = Function.Uncurried.runFn3 _formatRangeToParts
foreign import _formatToParts
:: Fn2
NumberFormat
Number
(Array { type :: String, value :: String })
-- | Allows locale-aware formatting of strings produced by the `NumberFormat`
formatToParts :: NumberFormat -> Number -> Array { type :: String, value :: String }
formatToParts = Function.Uncurried.runFn2 _formatToParts
type ResolvedOptions =
{ locale :: String
, numberingSystem :: String
, style :: String
, currency :: String
, currencyDisplay :: String
, currencySign :: String
, minimumIntegerDigits :: Int
, minimumFractionDigits :: Int
, maximumFractionDigits :: Int
, useGrouping :: Foreign
, notation :: String
, signDisplay :: String
}
foreign import _resolvedOptions
:: EffectFn1
NumberFormat
ResolvedOptions
resolvedOptions
:: NumberFormat
-> Effect ResolvedOptions
resolvedOptions = Effect.Uncurried.runEffectFn1 _resolvedOptions