@tolki/num 2.2.0 → 2.2.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 +6 -0
- package/dist/num.d.ts.map +1 -1
- package/dist/num.js +49 -42
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -136,6 +136,8 @@ const result4 = currency(1000, "EUR", "de", 0);
|
|
|
136
136
|
|
|
137
137
|
#### defaultCurrency
|
|
138
138
|
|
|
139
|
+
`defaultCurrency` and `defaultLocale` read module-wide state that [useLocale](#uselocale) and [useCurrency](#usecurrency) elsewhere on this page can change, so their result would depend on what else you've clicked on this page — there's no live example here for that reason.
|
|
140
|
+
|
|
139
141
|
Get the configured default currency.
|
|
140
142
|
|
|
141
143
|
```javascript
|
|
@@ -352,6 +354,8 @@ const result4 = percentage(10, 2, null, "de");
|
|
|
352
354
|
|
|
353
355
|
#### spell
|
|
354
356
|
|
|
357
|
+
`spell` and `spellOrdinal` return a `Promise<string>` and dynamically import a locale bundle on first use. The playground component above doesn't yet support async functions, so these two don't have a live example — see the code samples below instead.
|
|
358
|
+
|
|
355
359
|
Spell out the given number in the given locale. Returns a `Promise<string>`.
|
|
356
360
|
|
|
357
361
|
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).
|
|
@@ -444,6 +448,8 @@ const result2 = trim(12.3);
|
|
|
444
448
|
|
|
445
449
|
#### useLocale
|
|
446
450
|
|
|
451
|
+
`useLocale` and `useCurrency` permanently change the module-wide default for every other example on this page, so they don't have a live playground here — trying them would silently change the output of every other widget below. See [withLocale](#withlocale) and [withCurrency](#withcurrency) for a scoped, safe-to-try alternative.
|
|
452
|
+
|
|
447
453
|
Set the default locale. This should be used at the start of your application to set the desired locale globally.
|
|
448
454
|
|
|
449
455
|
```javascript
|
package/dist/num.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{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eAAe,IAAI,MAAM,CAExC"}
|
package/dist/num.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
let
|
|
1
|
+
let g = "en", b = "USD";
|
|
2
2
|
function E(t, n = 0, e = null) {
|
|
3
3
|
return S(t, n, e, !0);
|
|
4
4
|
}
|
|
5
5
|
function f(t, n = null, e = null, r = null) {
|
|
6
|
-
r = r ??
|
|
6
|
+
r = r ?? g;
|
|
7
7
|
const o = {
|
|
8
8
|
style: "decimal",
|
|
9
9
|
useGrouping: !0
|
|
@@ -11,7 +11,7 @@ function f(t, n = null, e = null, r = null) {
|
|
|
11
11
|
return e != null ? (o.maximumFractionDigits = e, o.minimumFractionDigits = 0) : n != null && (o.minimumFractionDigits = n, o.maximumFractionDigits = n), new Intl.NumberFormat(r, o).format(t);
|
|
12
12
|
}
|
|
13
13
|
function y(t, n = null) {
|
|
14
|
-
n = n ??
|
|
14
|
+
n = n ?? g;
|
|
15
15
|
const r = new Intl.NumberFormat(n).formatToParts(12345.6), o = /* @__PURE__ */ new Set();
|
|
16
16
|
let l = ".";
|
|
17
17
|
for (const i of r)
|
|
@@ -43,7 +43,7 @@ function x(t, n = null) {
|
|
|
43
43
|
return y(t, n);
|
|
44
44
|
}
|
|
45
45
|
function O(t, n = null) {
|
|
46
|
-
const e = n ??
|
|
46
|
+
const e = n ?? g;
|
|
47
47
|
try {
|
|
48
48
|
const o = new Intl.PluralRules(e, { type: "ordinal" }).select(t), l = String(e.split("-")[0]), u = {
|
|
49
49
|
one: "st",
|
|
@@ -71,16 +71,16 @@ function O(t, n = null) {
|
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
73
|
function I(t, n = 0, e = null, r = null) {
|
|
74
|
-
const o = r ??
|
|
74
|
+
const o = r ?? g, l = typeof t == "string" ? y(t, o) : t, u = {
|
|
75
75
|
style: "percent",
|
|
76
76
|
useGrouping: !0
|
|
77
77
|
};
|
|
78
78
|
return e != null ? (u.maximumFractionDigits = e, u.minimumFractionDigits = 0) : (u.minimumFractionDigits = n, u.maximumFractionDigits = n), new Intl.NumberFormat(o, u).format(l / 100);
|
|
79
79
|
}
|
|
80
80
|
function T(t, n = "", e = null, r = null) {
|
|
81
|
-
const o = e ??
|
|
81
|
+
const o = e ?? g, l = typeof t == "string" ? y(t, o) : t, s = {
|
|
82
82
|
style: "currency",
|
|
83
|
-
currency: n && n.length ? n :
|
|
83
|
+
currency: n && n.length ? n : b,
|
|
84
84
|
useGrouping: !0
|
|
85
85
|
};
|
|
86
86
|
return r != null && (s.minimumFractionDigits = r, s.maximumFractionDigits = r), new Intl.NumberFormat(o, s).format(l);
|
|
@@ -106,7 +106,7 @@ function R(t, n = 0, e = null) {
|
|
|
106
106
|
return `${f(r, n, e)} ${o[u]}`;
|
|
107
107
|
}
|
|
108
108
|
function S(t, n = 0, e = null, r = !1) {
|
|
109
|
-
return
|
|
109
|
+
return $(
|
|
110
110
|
t,
|
|
111
111
|
n,
|
|
112
112
|
e,
|
|
@@ -119,7 +119,7 @@ function S(t, n = 0, e = null, r = !1) {
|
|
|
119
119
|
}
|
|
120
120
|
);
|
|
121
121
|
}
|
|
122
|
-
function
|
|
122
|
+
function $(t, n = 0, e = null, r = {}) {
|
|
123
123
|
if (!Number.isFinite(t))
|
|
124
124
|
return f(t, n, e);
|
|
125
125
|
if (Object.keys(r).length === 0 && (r = {
|
|
@@ -130,16 +130,23 @@ function N(t, n = 0, e = null, r = {}) {
|
|
|
130
130
|
15: "Q"
|
|
131
131
|
}), Number(t) === 0)
|
|
132
132
|
return n > 0 ? f(0, n, e) : "0";
|
|
133
|
-
if (t < 0)
|
|
134
|
-
|
|
133
|
+
if (t < 0) {
|
|
134
|
+
const i = $(
|
|
135
135
|
Math.abs(t),
|
|
136
136
|
n,
|
|
137
137
|
e,
|
|
138
138
|
r
|
|
139
|
-
)
|
|
139
|
+
), c = $(
|
|
140
|
+
0,
|
|
141
|
+
n,
|
|
142
|
+
e,
|
|
143
|
+
r
|
|
144
|
+
);
|
|
145
|
+
return i === c ? i : `-${i}`;
|
|
146
|
+
}
|
|
140
147
|
if (t >= 1e15) {
|
|
141
148
|
const i = Object.keys(r).map((a) => Number(a)).filter((a) => !Number.isNaN(a)).sort((a, d) => a - d), c = i[i.length - 1], h = r[c];
|
|
142
|
-
return `${
|
|
149
|
+
return `${$(
|
|
143
150
|
t / 1e15,
|
|
144
151
|
n,
|
|
145
152
|
e,
|
|
@@ -147,8 +154,8 @@ function N(t, n = 0, e = null, r = {}) {
|
|
|
147
154
|
)}${h}`.trim();
|
|
148
155
|
}
|
|
149
156
|
const o = Math.floor(Math.log10(t));
|
|
150
|
-
let l = o - o % 3;
|
|
151
|
-
const u = t /
|
|
157
|
+
let l = Math.max(0, o - o % 3);
|
|
158
|
+
const u = t / 10 ** l;
|
|
152
159
|
let s;
|
|
153
160
|
if (e != null)
|
|
154
161
|
s = f(u, null, e);
|
|
@@ -166,10 +173,10 @@ function N(t, n = 0, e = null, r = {}) {
|
|
|
166
173
|
const m = r[l] ?? "";
|
|
167
174
|
return `${s}${m}`.trim();
|
|
168
175
|
}
|
|
169
|
-
function
|
|
176
|
+
function z(t, n, e) {
|
|
170
177
|
return Math.min(Math.max(t, n), e);
|
|
171
178
|
}
|
|
172
|
-
function
|
|
179
|
+
function G(t, n, e = 0, r = 1) {
|
|
173
180
|
if (n === 0)
|
|
174
181
|
throw new Error("The 'by' argument must not be zero.");
|
|
175
182
|
n = Math.abs(n);
|
|
@@ -180,10 +187,10 @@ function H(t, n, e = 0, r = 1) {
|
|
|
180
187
|
}
|
|
181
188
|
return o;
|
|
182
189
|
}
|
|
183
|
-
function
|
|
190
|
+
function H(t) {
|
|
184
191
|
return isFinite(t) ? JSON.parse(JSON.stringify(t)) : t;
|
|
185
192
|
}
|
|
186
|
-
function
|
|
193
|
+
function k(t, n = !0) {
|
|
187
194
|
const e = Number(t) * 60;
|
|
188
195
|
return D(e, n);
|
|
189
196
|
}
|
|
@@ -201,32 +208,32 @@ function D(t, n = !0) {
|
|
|
201
208
|
];
|
|
202
209
|
if (n)
|
|
203
210
|
for (const [a, d] of c) {
|
|
204
|
-
const
|
|
205
|
-
if (
|
|
206
|
-
const
|
|
207
|
-
return `${
|
|
211
|
+
const p = e / d;
|
|
212
|
+
if (p >= 0.5 || d === r) {
|
|
213
|
+
const F = Math.round(p);
|
|
214
|
+
return `${F} ${a}${F === 1 ? "" : "s"}`;
|
|
208
215
|
}
|
|
209
216
|
}
|
|
210
217
|
let h = e;
|
|
211
|
-
const
|
|
218
|
+
const N = [];
|
|
212
219
|
for (const [a, d] of c)
|
|
213
220
|
if (h >= d) {
|
|
214
|
-
const
|
|
215
|
-
h -=
|
|
221
|
+
const p = Math.floor(h / d);
|
|
222
|
+
h -= p * d, N.push(`${p} ${a}${p === 1 ? "" : "s"}`);
|
|
216
223
|
}
|
|
217
|
-
return
|
|
224
|
+
return N.join(", ");
|
|
218
225
|
}
|
|
219
226
|
function A(t, n) {
|
|
220
|
-
const e =
|
|
221
|
-
|
|
227
|
+
const e = g;
|
|
228
|
+
w(t);
|
|
222
229
|
try {
|
|
223
230
|
return n();
|
|
224
231
|
} finally {
|
|
225
|
-
|
|
232
|
+
w(e);
|
|
226
233
|
}
|
|
227
234
|
}
|
|
228
235
|
function C(t, n) {
|
|
229
|
-
const e =
|
|
236
|
+
const e = b;
|
|
230
237
|
M(t);
|
|
231
238
|
try {
|
|
232
239
|
return n();
|
|
@@ -234,39 +241,39 @@ function C(t, n) {
|
|
|
234
241
|
M(e);
|
|
235
242
|
}
|
|
236
243
|
}
|
|
237
|
-
function
|
|
238
|
-
|
|
244
|
+
function w(t) {
|
|
245
|
+
g = t;
|
|
239
246
|
}
|
|
240
247
|
function M(t) {
|
|
241
|
-
|
|
248
|
+
b = t;
|
|
242
249
|
}
|
|
243
250
|
function K() {
|
|
244
|
-
return
|
|
251
|
+
return g;
|
|
245
252
|
}
|
|
246
253
|
function L() {
|
|
247
|
-
return
|
|
254
|
+
return b;
|
|
248
255
|
}
|
|
249
256
|
export {
|
|
250
257
|
E as abbreviate,
|
|
251
|
-
|
|
258
|
+
z as clamp,
|
|
252
259
|
T as currency,
|
|
253
260
|
L as defaultCurrency,
|
|
254
261
|
K as defaultLocale,
|
|
255
262
|
R as fileSize,
|
|
256
263
|
S as forHumans,
|
|
257
264
|
f as format,
|
|
258
|
-
|
|
265
|
+
k as minutesToHuman,
|
|
259
266
|
O as ordinal,
|
|
260
|
-
|
|
267
|
+
G as pairs,
|
|
261
268
|
y as parse,
|
|
262
269
|
x as parseFloat,
|
|
263
270
|
B as parseInt,
|
|
264
271
|
I as percentage,
|
|
265
272
|
D as secondsToHuman,
|
|
266
|
-
|
|
267
|
-
|
|
273
|
+
$ as summarize,
|
|
274
|
+
H as trim,
|
|
268
275
|
M as useCurrency,
|
|
269
|
-
|
|
276
|
+
w as useLocale,
|
|
270
277
|
C as withCurrency,
|
|
271
278
|
A as withLocale
|
|
272
279
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tolki/num",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.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",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"sideEffects": false,
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"to-words": "^5.0.0",
|
|
29
|
-
"@tolki/utils": "^1.0
|
|
29
|
+
"@tolki/utils": "^1.1.0"
|
|
30
30
|
},
|
|
31
31
|
"files": [
|
|
32
32
|
"dist"
|