@tolki/num 2.0.1 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +468 -0
- package/package.json +9 -2
package/README.md
CHANGED
|
@@ -5,3 +5,471 @@ This package provides number utility functions similar to Laravel's Num class fa
|
|
|
5
5
|
## Documentation
|
|
6
6
|
|
|
7
7
|
The full documentation for the number utilities can be found at [https://tolki.abe.dev](https://tolki.abe.dev/numbers/).
|
|
8
|
+
|
|
9
|
+
<!-- AUTO-GENERATED-DOCS:START -->
|
|
10
|
+
|
|
11
|
+
# Number Utilities Installation
|
|
12
|
+
|
|
13
|
+
The [`@tolki/num`](https://www.npmjs.com/package/@tolki/num) package provides a variety of number manipulation utilities inspired by Laravel's Num class. You can install it via npm, yarn, or pnpm:
|
|
14
|
+
|
|
15
|
+
```bash [npm]
|
|
16
|
+
npm install @tolki/num
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```bash [yarn]
|
|
20
|
+
yarn add @tolki/num
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```bash [pnpm]
|
|
24
|
+
pnpm add @tolki/num
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Importing the Package
|
|
28
|
+
|
|
29
|
+
You can import individual functions for better tree-shaking or the entire package (not recommended).:
|
|
30
|
+
|
|
31
|
+
Importing individual functions:
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
// Import individual functions
|
|
35
|
+
import { format, parse } from "@tolki/num";
|
|
36
|
+
|
|
37
|
+
// Example usage
|
|
38
|
+
const formattedNumber = format(1234567.89); // '1,234,567.89'
|
|
39
|
+
const parsedNumber = parse("1,234,567.89"); // 1234567.89
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Importing the entire package:
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
// Import the entire package (not recommended for tree-shaking)
|
|
46
|
+
import * as Num from "@tolki/num";
|
|
47
|
+
|
|
48
|
+
// Example usage
|
|
49
|
+
const formattedNumber = Num.format(1234567.89); // '1,234,567.89'
|
|
50
|
+
const parsedNumber = Num.parse("1,234,567.89"); // 1234567.89
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
The reason that importing the entire package is not recommended is that it will include all functions in your bundle, even the ones you don't use, which can lead to larger bundle sizes. Importing individual functions allows your end files to only include the code that is actually used, resulting in smaller and more efficient bundles.
|
|
54
|
+
|
|
55
|
+
However, if you are working on a backend project where bundle size is not a concern, importing the entire package can be more convenient.
|
|
56
|
+
|
|
57
|
+
# Tolki Number Utilities List
|
|
58
|
+
|
|
59
|
+
## Number Utilities List
|
|
60
|
+
|
|
61
|
+
These are the number utilities that can be used independently as single functions.
|
|
62
|
+
|
|
63
|
+
[abbreviate](#abbreviate) [clamp](#clamp) [currency](#currency) [defaultCurrency](#defaultcurrency) [defaultLocale](#defaultlocale) [fileSize](#filesize) [forHumans](#forhumans) [format](#format) [minutesToHuman](#minutestohuman) [ordinal](#ordinal) [pairs](#pairs) [parseFloat](#parsefloat) [parseInt](#parseint) [percentage](#percentage) [spell](#spell) [spellOrdinal](#spellordinal) [secondsToHuman](#secondstohuman) [trim](#trim) [useLocale](#uselocale) [withLocale](#withlocale) [useCurrency](#usecurrency) [withCurrency](#withcurrency)
|
|
64
|
+
|
|
65
|
+
## Number Utilities Details
|
|
66
|
+
|
|
67
|
+
### abbreviate
|
|
68
|
+
|
|
69
|
+
Convert the number to its human-readable equivalent.
|
|
70
|
+
|
|
71
|
+
```javascript
|
|
72
|
+
import { abbreviate } from "@tolki/num";
|
|
73
|
+
|
|
74
|
+
const result = abbreviate(1000);
|
|
75
|
+
|
|
76
|
+
// result is "1K"
|
|
77
|
+
|
|
78
|
+
const result2 = abbreviate(489939);
|
|
79
|
+
|
|
80
|
+
// result2 is "490K"
|
|
81
|
+
|
|
82
|
+
const result3 = abbreviate(1230000, 2);
|
|
83
|
+
|
|
84
|
+
// result3 is "1.23M"
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### clamp
|
|
88
|
+
|
|
89
|
+
Clamp the given number between the given minimum and maximum.
|
|
90
|
+
|
|
91
|
+
```javascript
|
|
92
|
+
import { clamp } from "@tolki/num";
|
|
93
|
+
|
|
94
|
+
const result = clamp(105, 10, 100);
|
|
95
|
+
|
|
96
|
+
// result is 100
|
|
97
|
+
|
|
98
|
+
const result2 = clamp(5, 10, 100);
|
|
99
|
+
|
|
100
|
+
// result2 is 10
|
|
101
|
+
|
|
102
|
+
const result3 = clamp(10, 10, 100);
|
|
103
|
+
|
|
104
|
+
// result3 is 10
|
|
105
|
+
|
|
106
|
+
const result4 = clamp(20, 10, 100);
|
|
107
|
+
|
|
108
|
+
// result4 is 20
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### currency
|
|
112
|
+
|
|
113
|
+
Convert the given number to its currency equivalent.
|
|
114
|
+
|
|
115
|
+
```javascript
|
|
116
|
+
import { currency } from "@tolki/num";
|
|
117
|
+
|
|
118
|
+
const result = currency(1000);
|
|
119
|
+
|
|
120
|
+
// result is "$1,000.00"
|
|
121
|
+
|
|
122
|
+
const result2 = currency(1000, "EUR");
|
|
123
|
+
|
|
124
|
+
// result2 is "€1,000.00"
|
|
125
|
+
|
|
126
|
+
const result3 = currency(1000, "EUR", "de");
|
|
127
|
+
|
|
128
|
+
// result3 is "1.000,00 €"
|
|
129
|
+
|
|
130
|
+
const result4 = currency(1000, "EUR", "de", 0);
|
|
131
|
+
|
|
132
|
+
// result4 is "1.000 €"
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### defaultCurrency
|
|
136
|
+
|
|
137
|
+
Get the configured default currency.
|
|
138
|
+
|
|
139
|
+
```javascript
|
|
140
|
+
import { defaultCurrency } from "@tolki/num";
|
|
141
|
+
|
|
142
|
+
const result = defaultCurrency();
|
|
143
|
+
|
|
144
|
+
// result is "USD"
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### defaultLocale
|
|
148
|
+
|
|
149
|
+
Get the configured default locale.
|
|
150
|
+
|
|
151
|
+
```javascript
|
|
152
|
+
import { defaultLocale } from "@tolki/num";
|
|
153
|
+
|
|
154
|
+
const result = defaultLocale();
|
|
155
|
+
|
|
156
|
+
// result is "en"
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### fileSize
|
|
160
|
+
|
|
161
|
+
Convert the given number to its file size equivalent.
|
|
162
|
+
|
|
163
|
+
```javascript
|
|
164
|
+
import { fileSize } from "@tolki/num";
|
|
165
|
+
|
|
166
|
+
const result = fileSize(1024);
|
|
167
|
+
|
|
168
|
+
// result is "1 KB"
|
|
169
|
+
|
|
170
|
+
const result2 = fileSize(1024 * 1024);
|
|
171
|
+
|
|
172
|
+
// result2 is "1 MB"
|
|
173
|
+
|
|
174
|
+
const result3 = fileSize(1024, 2);
|
|
175
|
+
|
|
176
|
+
// result3 is "1.00 KB"
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### forHumans
|
|
180
|
+
|
|
181
|
+
Convert the number to its human-readable equivalent.
|
|
182
|
+
|
|
183
|
+
```javascript
|
|
184
|
+
import { forHumans } from "@tolki/num";
|
|
185
|
+
|
|
186
|
+
const result = forHumans(1000);
|
|
187
|
+
|
|
188
|
+
// result is "1 thousand"
|
|
189
|
+
|
|
190
|
+
const result2 = forHumans(489939);
|
|
191
|
+
|
|
192
|
+
// result2 is "490K"
|
|
193
|
+
|
|
194
|
+
const result3 = forHumans(1230000, 2);
|
|
195
|
+
|
|
196
|
+
// result3 is "1.23 million"
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### format
|
|
200
|
+
|
|
201
|
+
Format the given number according to the current locale.
|
|
202
|
+
|
|
203
|
+
```javascript
|
|
204
|
+
import { format } from "@tolki/num";
|
|
205
|
+
|
|
206
|
+
const result = format(100000);
|
|
207
|
+
|
|
208
|
+
// result is "100,000"
|
|
209
|
+
|
|
210
|
+
const result2 = format(100000, 2);
|
|
211
|
+
|
|
212
|
+
// result2 is "100,000.00"
|
|
213
|
+
|
|
214
|
+
const result3 = format(100000.123, null, 2);
|
|
215
|
+
|
|
216
|
+
// result3 is "100,000.12"
|
|
217
|
+
|
|
218
|
+
const result4 = format(100000, null, null, "de");
|
|
219
|
+
|
|
220
|
+
// result4 is "100.000"
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
### minutesToHuman
|
|
224
|
+
|
|
225
|
+
Convert a duration in minutes to a human-readable format.
|
|
226
|
+
|
|
227
|
+
```javascript
|
|
228
|
+
import { minutesToHuman } from "@tolki/num";
|
|
229
|
+
|
|
230
|
+
const result = minutesToHuman(61);
|
|
231
|
+
|
|
232
|
+
// result is "1 hour"
|
|
233
|
+
|
|
234
|
+
const result2 = minutesToHuman(61, false);
|
|
235
|
+
|
|
236
|
+
// result2 is "1 hour, 1 minute"
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### ordinal
|
|
240
|
+
|
|
241
|
+
Convert the given number to ordinal form.
|
|
242
|
+
|
|
243
|
+
```javascript
|
|
244
|
+
import { ordinal } from "@tolki/num";
|
|
245
|
+
|
|
246
|
+
const result = ordinal(1);
|
|
247
|
+
|
|
248
|
+
// result is "1st"
|
|
249
|
+
|
|
250
|
+
const result2 = ordinal(2);
|
|
251
|
+
|
|
252
|
+
// result2 is "2nd"
|
|
253
|
+
|
|
254
|
+
const result3 = ordinal(21);
|
|
255
|
+
|
|
256
|
+
// result3 is "21st"
|
|
257
|
+
|
|
258
|
+
const result4 = ordinal(13);
|
|
259
|
+
|
|
260
|
+
// result4 is "13th"
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
### pairs
|
|
264
|
+
|
|
265
|
+
Split the given number into pairs of min/max values.
|
|
266
|
+
|
|
267
|
+
```javascript
|
|
268
|
+
import { pairs } from "@tolki/num";
|
|
269
|
+
|
|
270
|
+
const result = pairs(25, 10);
|
|
271
|
+
|
|
272
|
+
// result is [[0, 9], [10, 19], [20, 25]]
|
|
273
|
+
|
|
274
|
+
const result2 = pairs(25, 10, 0, 0);
|
|
275
|
+
|
|
276
|
+
// result2 is [[0, 10], [10, 20], [20, 25]]
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
### parseInt
|
|
280
|
+
|
|
281
|
+
Parse a string into an integer according to the specified locale.
|
|
282
|
+
|
|
283
|
+
```javascript
|
|
284
|
+
import { parseInt } from "@tolki/num";
|
|
285
|
+
|
|
286
|
+
const result = parseInt("10.123");
|
|
287
|
+
|
|
288
|
+
// result is 10
|
|
289
|
+
|
|
290
|
+
const result2 = parseInt("10,123", "fr");
|
|
291
|
+
|
|
292
|
+
// result2 is 10
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
### parseFloat
|
|
296
|
+
|
|
297
|
+
Parse a string into a float according to the specified locale.
|
|
298
|
+
|
|
299
|
+
```javascript
|
|
300
|
+
import { parseFloat } from "@tolki/num";
|
|
301
|
+
|
|
302
|
+
const result = parseFloat("10");
|
|
303
|
+
|
|
304
|
+
// result is 10.0
|
|
305
|
+
|
|
306
|
+
const result2 = parseFloat("10", "fr");
|
|
307
|
+
|
|
308
|
+
// result2 is 10.0
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
### percentage
|
|
312
|
+
|
|
313
|
+
Convert the given number to its percentage equivalent.
|
|
314
|
+
|
|
315
|
+
```javascript
|
|
316
|
+
import { percentage } from "@tolki/num";
|
|
317
|
+
|
|
318
|
+
const result = percentage(10);
|
|
319
|
+
|
|
320
|
+
// result is "10%"
|
|
321
|
+
|
|
322
|
+
const result2 = percentage(10, 2);
|
|
323
|
+
|
|
324
|
+
// result2 is "10.00%"
|
|
325
|
+
|
|
326
|
+
const result3 = percentage(10.123, 0, 2);
|
|
327
|
+
|
|
328
|
+
// result3 is "10.12%"
|
|
329
|
+
|
|
330
|
+
const result4 = percentage(10, 2, null, "de");
|
|
331
|
+
|
|
332
|
+
// result4 is "10,00%"
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
### spell
|
|
336
|
+
|
|
337
|
+
Spell out the given number in the given locale. Returns a `Promise<string>`.
|
|
338
|
+
|
|
339
|
+
Uses the [`to-words`](https://www.npmjs.com/package/to-words) package. Locales are loaded dynamically on demand, so only the locale you request is bundled (~3KB gzip per locale instead of ~54KB for all 94 locales).
|
|
340
|
+
|
|
341
|
+
Supports 94 locales via [`to-words`](https://www.npmjs.com/package/to-words). You can pass a full locale code (e.g., `"fr-FR"`, `"en-IN"`) or a bare language code (e.g., `"fr"`, `"en"`) which will resolve to a sensible default. Unsupported locales fall back to `"en-US"`.
|
|
342
|
+
|
|
343
|
+
```javascript
|
|
344
|
+
import { spell } from "@tolki/num";
|
|
345
|
+
|
|
346
|
+
const result = await spell(102);
|
|
347
|
+
|
|
348
|
+
// result is "One Hundred Two"
|
|
349
|
+
|
|
350
|
+
const result2 = await spell(88, "fr");
|
|
351
|
+
|
|
352
|
+
// result2 is "Quatre-Vingt-Huit"
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
If the `after` argument is provided and the number is less than or equal to `after`, the number will be returned as a formatted string instead of spelled out.
|
|
356
|
+
|
|
357
|
+
```javascript
|
|
358
|
+
import { spell } from "@tolki/num";
|
|
359
|
+
|
|
360
|
+
const result = await spell(5, null, 10);
|
|
361
|
+
|
|
362
|
+
// result is "5"
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
If the `until` argument is provided and the number is greater than or equal to `until`, the number will be returned as a formatted string instead of spelled out.
|
|
366
|
+
|
|
367
|
+
```javascript
|
|
368
|
+
import { spell } from "@tolki/num";
|
|
369
|
+
|
|
370
|
+
const result = await spell(15, null, null, 10);
|
|
371
|
+
|
|
372
|
+
// result is "15"
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
### spellOrdinal
|
|
376
|
+
|
|
377
|
+
Spell out the given number in the given locale in ordinal form. Returns a `Promise<string>`.
|
|
378
|
+
|
|
379
|
+
Uses [`to-words`](https://www.npmjs.com/package/to-words) v5 native ordinal support. See the [spell](#spell) function for details on locale handling.
|
|
380
|
+
|
|
381
|
+
Decimal values are floored before conversion.
|
|
382
|
+
|
|
383
|
+
```javascript
|
|
384
|
+
import { spellOrdinal } from "@tolki/num";
|
|
385
|
+
|
|
386
|
+
const result = await spellOrdinal(1);
|
|
387
|
+
|
|
388
|
+
// result is "First"
|
|
389
|
+
|
|
390
|
+
const result2 = await spellOrdinal(2);
|
|
391
|
+
|
|
392
|
+
// result2 is "Second"
|
|
393
|
+
|
|
394
|
+
const result3 = await spellOrdinal(21);
|
|
395
|
+
|
|
396
|
+
// result3 is "Twenty First"
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
### secondsToHuman
|
|
400
|
+
|
|
401
|
+
Convert a duration in seconds to a human-readable format.
|
|
402
|
+
|
|
403
|
+
```javascript
|
|
404
|
+
import { secondsToHuman } from "@tolki/num";
|
|
405
|
+
|
|
406
|
+
const result = secondsToHuman(3661);
|
|
407
|
+
|
|
408
|
+
// result is "1 hour, 1 minute, 1 second"
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
### trim
|
|
412
|
+
|
|
413
|
+
Remove any trailing zero digits after the decimal point of the given number.
|
|
414
|
+
|
|
415
|
+
```javascript
|
|
416
|
+
import { trim } from "@tolki/num";
|
|
417
|
+
|
|
418
|
+
const result = trim(12.0);
|
|
419
|
+
|
|
420
|
+
// result is 12
|
|
421
|
+
|
|
422
|
+
const result2 = trim(12.3);
|
|
423
|
+
|
|
424
|
+
// result is 12.3
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
### useLocale
|
|
428
|
+
|
|
429
|
+
Set the default locale. This should be used at the start of your application to set the desired locale globally.
|
|
430
|
+
|
|
431
|
+
```javascript
|
|
432
|
+
import { useLocale } from "@tolki/num";
|
|
433
|
+
|
|
434
|
+
useLocale("fr");
|
|
435
|
+
|
|
436
|
+
// The default locale is now set to French
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
### withLocale
|
|
440
|
+
|
|
441
|
+
Execute the given callback using the given locale.
|
|
442
|
+
|
|
443
|
+
```javascript
|
|
444
|
+
import { withLocale, format } from "@tolki/num";
|
|
445
|
+
|
|
446
|
+
withLocale("fr", () => {
|
|
447
|
+
return format(1234.56);
|
|
448
|
+
}); // "1 234,56"
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
### useCurrency
|
|
452
|
+
|
|
453
|
+
Set the default currency. This should be used at the start of your application to set the desired currency globally.
|
|
454
|
+
|
|
455
|
+
```javascript
|
|
456
|
+
import { useCurrency } from "@tolki/num";
|
|
457
|
+
|
|
458
|
+
useCurrency("EUR");
|
|
459
|
+
|
|
460
|
+
// The default currency is now set to Euro
|
|
461
|
+
```
|
|
462
|
+
|
|
463
|
+
### withCurrency
|
|
464
|
+
|
|
465
|
+
Execute the given callback using the given currency.
|
|
466
|
+
|
|
467
|
+
```javascript
|
|
468
|
+
import { withCurrency, format } from "@tolki/num";
|
|
469
|
+
|
|
470
|
+
withCurrency("EUR", () => {
|
|
471
|
+
return format(1234.56);
|
|
472
|
+
}); // "€1,234.56"
|
|
473
|
+
```
|
|
474
|
+
|
|
475
|
+
<!-- AUTO-GENERATED-DOCS:END -->
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tolki/num",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"description": "Utilities for working with numbers similar to Laravel's Num class facade.",
|
|
5
5
|
"author": "Abraham Arango <https://github.com/abetwothree>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -31,8 +31,15 @@
|
|
|
31
31
|
"files": [
|
|
32
32
|
"dist"
|
|
33
33
|
],
|
|
34
|
+
"tolki": {
|
|
35
|
+
"docs": [
|
|
36
|
+
"numbers/index.md",
|
|
37
|
+
"numbers/number-utilities-list.md"
|
|
38
|
+
]
|
|
39
|
+
},
|
|
34
40
|
"scripts": {
|
|
35
|
-
"build": "vite build"
|
|
41
|
+
"build": "vite build",
|
|
42
|
+
"readme:build": "ts-node ../../scripts/readme-build.ts --filter @tolki/num"
|
|
36
43
|
},
|
|
37
44
|
"types": "dist/index.d.ts",
|
|
38
45
|
"exports": {
|