@tolki/num 2.2.4 → 2.2.6

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/dist/num.js CHANGED
@@ -1,293 +1,233 @@
1
- import { InvalidArgumentException as D } from "@tolki/utils";
2
- let p = "en", N = "USD";
3
- function I(t, n = 0, e = null) {
4
- return E(t, n, e, !0);
5
- }
6
- function a(t, n = null, e = null, r = null) {
7
- r = r ?? p;
8
- const o = {
9
- style: "decimal",
10
- useGrouping: !0
11
- };
12
- e != null ? (o.maximumFractionDigits = e, o.minimumFractionDigits = 0) : n != null && (o.minimumFractionDigits = n, o.maximumFractionDigits = n);
13
- const s = new Intl.NumberFormat(r, o);
14
- return s.format(w(t, s.format));
15
- }
16
- function y(t, n = null) {
17
- n = n ?? p;
18
- const r = new Intl.NumberFormat(n).formatToParts(12345.6), o = /* @__PURE__ */ new Set();
19
- let s = ".";
20
- for (const i of r)
21
- i.type === "group" ? o.add(i.value) : i.type === "decimal" && (s = i.value);
22
- const l = [" ", " ", " "];
23
- for (const i of l) o.add(i);
24
- let u = t;
25
- for (const i of o) {
26
- const c = new RegExp(
27
- i.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&"),
28
- "g"
29
- );
30
- u = u.replace(c, "");
31
- }
32
- if (s !== ".") {
33
- const i = new RegExp(
34
- s.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&"),
35
- "g"
36
- ), c = u.lastIndexOf(s);
37
- c !== -1 && (u = u.slice(0, c).replace(i, "") + "." + u.slice(c + s.length));
38
- }
39
- return u = u.replace(/[\s\u00A0\u202F\u2009]+/g, "").trim(), globalThis.Number(u);
40
- }
41
- function O(t, n = null) {
42
- const e = y(t, n);
43
- return Number.isNaN(e) ? !1 : e < 0 ? Math.ceil(e) : Math.floor(e);
44
- }
45
- function T(t, n = null) {
46
- return y(t, n);
47
- }
48
- function R(t, n = null) {
49
- const e = n ?? p;
50
- try {
51
- const o = new Intl.PluralRules(e, { type: "ordinal" }).select(t), s = String(e.split("-")[0]), l = {
52
- one: "st",
53
- two: "nd",
54
- few: "rd",
55
- other: "th"
56
- }, f = {
57
- en: l
58
- }[s] ?? l, i = f[o] ?? f.other;
59
- return `${t}${i}`;
60
- } catch {
61
- const r = Math.abs(t), o = r % 100;
62
- if (o >= 11 && o <= 13)
63
- return `${t}th`;
64
- switch (r % 10) {
65
- case 1:
66
- return `${t}st`;
67
- case 2:
68
- return `${t}nd`;
69
- case 3:
70
- return `${t}rd`;
71
- default:
72
- return `${t}th`;
73
- }
74
- }
75
- }
76
- function z(t, n = 0, e = null, r = null) {
77
- const o = r ?? p, s = typeof t == "string" ? y(t, o) : t, l = {
78
- style: "percent",
79
- useGrouping: !0
80
- };
81
- e != null ? (l.maximumFractionDigits = e, l.minimumFractionDigits = 0) : (l.minimumFractionDigits = n, l.maximumFractionDigits = n);
82
- const u = new Intl.NumberFormat(o, l);
83
- return u.format(w(s / 100, u.format));
84
- }
85
- function A(t, n = "", e = null, r = null) {
86
- const o = e ?? p, s = typeof t == "string" ? y(t, o) : t, u = {
87
- style: "currency",
88
- currency: n && n.length ? n : N,
89
- useGrouping: !0
90
- };
91
- r != null && (u.minimumFractionDigits = r, u.maximumFractionDigits = r);
92
- const f = new Intl.NumberFormat(o, u);
93
- return f.format(w(s, f.format));
94
- }
95
- function G(t, n = 0, e = null) {
96
- let r = typeof t == "string" ? Number(t) : t;
97
- if (!Number.isFinite(r))
98
- return `${a(r, n, e)} B`;
99
- const o = [
100
- "B",
101
- "KB",
102
- "MB",
103
- "GB",
104
- "TB",
105
- "PB",
106
- "EB",
107
- "ZB",
108
- "YB"
109
- ], s = o.length;
110
- let l = 0;
111
- for (; Math.abs(r) / 1024 > 0.9 && l < s - 1; )
112
- r /= 1024, l++;
113
- return `${a(r, n, e)} ${o[l]}`;
114
- }
115
- function E(t, n = 0, e = null, r = !1) {
116
- return $(
117
- t,
118
- n,
119
- e,
120
- r ? {} : {
121
- 3: " thousand",
122
- 6: " million",
123
- 9: " billion",
124
- 12: " trillion",
125
- 15: " quadrillion"
126
- }
127
- );
128
- }
129
- function $(t, n = 0, e = null, r = {}) {
130
- if (!Number.isFinite(t))
131
- return a(t, n, e);
132
- if (Object.keys(r).length === 0 && (r = {
133
- 3: "K",
134
- 6: "M",
135
- 9: "B",
136
- 12: "T",
137
- 15: "Q"
138
- }), Number(t) === 0)
139
- return n > 0 ? a(0, n, e) : "0";
140
- if (t < 0) {
141
- const i = $(
142
- Math.abs(t),
143
- n,
144
- e,
145
- r
146
- ), c = $(
147
- 0,
148
- n,
149
- e,
150
- r
151
- );
152
- return i === c ? i : `-${i}`;
153
- }
154
- if (t >= 1e15) {
155
- const i = Object.keys(r).map((m) => Number(m)).filter((m) => !Number.isNaN(m)).sort((m, d) => m - d), c = i[i.length - 1], h = r[c];
156
- return `${$(
157
- t / 1e15,
158
- n,
159
- e,
160
- r
161
- )}${h}`.trim();
162
- }
163
- const o = Math.floor(Math.log10(t));
164
- let s = Math.max(0, o - o % 3);
165
- const l = t / 10 ** s;
166
- let u;
167
- if (e != null)
168
- u = a(l, null, e);
169
- else if (n > 0)
170
- u = a(l, n, null);
171
- else {
172
- const i = Math.abs(t);
173
- u = s === 3 && l < 10 && i % 10 === 0 ? a(l, null, 1) : a(l, 0, null);
174
- }
175
- if (y(u) >= 1e3 && r[s + 3] !== void 0) {
176
- s += 3;
177
- const i = l / 1e3;
178
- e != null ? u = a(i, null, e) : n > 0 ? u = a(i, n, null) : u = a(i, 0, null);
179
- }
180
- const f = r[s] ?? "";
181
- return `${u}${f}`.trim();
182
- }
183
- function H(t, n, e) {
184
- return Math.min(Math.max(t, n), e);
185
- }
186
- function k(t, n, e = 0, r = 1) {
187
- if (n === 0)
188
- throw new D(
189
- "The $by argument must not be zero."
190
- );
191
- n = Math.abs(n);
192
- const o = [];
193
- let s = e;
194
- for (; s < t; s += n) {
195
- let u = s + n - r;
196
- u > t && (u = t), o.push([s, u]);
197
- }
198
- const l = o.at(-1);
199
- return l !== void 0 && s === t && l[1] < t && o.push([s, t]), o;
200
- }
201
- function C(t) {
202
- return isFinite(t) ? JSON.parse(JSON.stringify(t)) : t;
203
- }
204
- function K(t, n = !0) {
205
- const e = Number(t) * 60;
206
- return x(e, n);
207
- }
208
- function x(t, n = !0) {
209
- let e = Number(t);
210
- if ((!Number.isFinite(e) || e < 0) && (e = 0), e === 0) return "0 seconds";
211
- const r = 1, o = 60 * r, s = 60 * o, l = 24 * s, u = 7 * l, f = 4 * u, c = [
212
- ["year", 12 * f],
213
- ["month", f],
214
- ["week", u],
215
- ["day", l],
216
- ["hour", s],
217
- ["minute", o],
218
- ["second", r]
219
- ];
220
- if (n)
221
- for (const [m, d] of c) {
222
- const g = e / d;
223
- if (g >= 0.5 || d === r) {
224
- const F = Math.round(g);
225
- return `${F} ${m}${F === 1 ? "" : "s"}`;
226
- }
227
- }
228
- let h = e;
229
- const b = [];
230
- for (const [m, d] of c)
231
- if (h >= d) {
232
- const g = Math.floor(h / d);
233
- h -= g * d, b.push(`${g} ${m}${g === 1 ? "" : "s"}`);
234
- }
235
- return b.join(", ");
236
- }
237
- function L(t, n) {
238
- const e = p;
239
- M(t);
240
- try {
241
- return n();
242
- } finally {
243
- M(e);
244
- }
245
- }
246
- function Y(t, n) {
247
- const e = N;
248
- S(t);
249
- try {
250
- return n();
251
- } finally {
252
- S(e);
253
- }
254
- }
255
- function M(t) {
256
- p = t;
257
- }
258
- function S(t) {
259
- N = t;
260
- }
261
- function j() {
262
- return p;
263
- }
264
- function U() {
265
- return N;
266
- }
267
- function w(t, n) {
268
- return t === 0 || t < 0 && n(Math.abs(t)) === n(0) ? 0 : t;
269
- }
270
- export {
271
- I as abbreviate,
272
- H as clamp,
273
- A as currency,
274
- U as defaultCurrency,
275
- j as defaultLocale,
276
- G as fileSize,
277
- E as forHumans,
278
- a as format,
279
- K as minutesToHuman,
280
- R as ordinal,
281
- k as pairs,
282
- y as parse,
283
- T as parseFloat,
284
- O as parseInt,
285
- z as percentage,
286
- x as secondsToHuman,
287
- $ as summarize,
288
- C as trim,
289
- S as useCurrency,
290
- M as useLocale,
291
- Y as withCurrency,
292
- L as withLocale
293
- };
1
+ import { InvalidArgumentException as e } from "@tolki/utils";
2
+ // src/num.ts
3
+ var t = "en", n = "USD";
4
+ /* @__NO_SIDE_EFFECTS__ */
5
+ function r(e, t = 0, n = null) {
6
+ return /* @__PURE__ */ f(e, t, n, !0);
7
+ }
8
+ /* @__NO_SIDE_EFFECTS__ */
9
+ function i(e, n = null, r = null, i = null) {
10
+ i ??= t;
11
+ let a = {
12
+ style: "decimal",
13
+ useGrouping: !0
14
+ };
15
+ r == null ? n != null && (a.minimumFractionDigits = n, a.maximumFractionDigits = n) : (a.maximumFractionDigits = r, a.minimumFractionDigits = 0);
16
+ let o = new Intl.NumberFormat(i, a);
17
+ return o.format(T(e, o.format));
18
+ }
19
+ /* @__NO_SIDE_EFFECTS__ */
20
+ function a(e, n = null) {
21
+ n ??= t;
22
+ let r = new Intl.NumberFormat(n).formatToParts(12345.6), i = /* @__PURE__ */ new Set(), a = ".";
23
+ for (let e of r) e.type === "group" ? i.add(e.value) : e.type === "decimal" && (a = e.value);
24
+ for (let e of [
25
+ "\xA0",
26
+ " ",
27
+ " "
28
+ ]) i.add(e);
29
+ let o = e;
30
+ for (let e of i) {
31
+ let t = new RegExp(e.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&"), "g");
32
+ o = o.replace(t, "");
33
+ }
34
+ if (a !== ".") {
35
+ let e = new RegExp(a.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&"), "g"), t = o.lastIndexOf(a);
36
+ t !== -1 && (o = o.slice(0, t).replace(e, "") + "." + o.slice(t + a.length));
37
+ }
38
+ return o = o.replace(/[\s\u00A0\u202F\u2009]+/g, "").trim(), globalThis.Number(o);
39
+ }
40
+ /* @__NO_SIDE_EFFECTS__ */
41
+ function o(e, t = null) {
42
+ let n = /* @__PURE__ */ a(e, t);
43
+ return Number.isNaN(n) ? !1 : n < 0 ? Math.ceil(n) : Math.floor(n);
44
+ }
45
+ /* @__NO_SIDE_EFFECTS__ */
46
+ function s(e, t = null) {
47
+ return /* @__PURE__ */ a(e, t);
48
+ }
49
+ /* @__NO_SIDE_EFFECTS__ */
50
+ function c(e, n = null) {
51
+ let r = n ?? t;
52
+ try {
53
+ let t = new Intl.PluralRules(r, { type: "ordinal" }).select(e), n = String(r.split("-")[0]), i = {
54
+ one: "st",
55
+ two: "nd",
56
+ few: "rd",
57
+ other: "th"
58
+ }, a = { en: i }[n] ?? i;
59
+ return `${e}${a[t] ?? a.other}`;
60
+ } catch {
61
+ let t = Math.abs(e), n = t % 100;
62
+ if (n >= 11 && n <= 13) return `${e}th`;
63
+ switch (t % 10) {
64
+ case 1: return `${e}st`;
65
+ case 2: return `${e}nd`;
66
+ case 3: return `${e}rd`;
67
+ default: return `${e}th`;
68
+ }
69
+ }
70
+ }
71
+ /* @__NO_SIDE_EFFECTS__ */
72
+ function l(e, n = 0, r = null, i = null) {
73
+ let o = i ?? t, s = typeof e == "string" ? /* @__PURE__ */ a(e, o) : e, c = {
74
+ style: "percent",
75
+ useGrouping: !0
76
+ };
77
+ r == null ? (c.minimumFractionDigits = n, c.maximumFractionDigits = n) : (c.maximumFractionDigits = r, c.minimumFractionDigits = 0);
78
+ let l = new Intl.NumberFormat(o, c);
79
+ return l.format(T(s / 100, l.format));
80
+ }
81
+ /* @__NO_SIDE_EFFECTS__ */
82
+ function u(e, r = "", i = null, o = null) {
83
+ let s = i ?? t, c = typeof e == "string" ? /* @__PURE__ */ a(e, s) : e, l = {
84
+ style: "currency",
85
+ currency: r && r.length ? r : n,
86
+ useGrouping: !0
87
+ };
88
+ o != null && (l.minimumFractionDigits = o, l.maximumFractionDigits = o);
89
+ let u = new Intl.NumberFormat(s, l);
90
+ return u.format(T(c, u.format));
91
+ }
92
+ /* @__NO_SIDE_EFFECTS__ */
93
+ function d(e, t = 0, n = null) {
94
+ let r = typeof e == "string" ? Number(e) : e;
95
+ if (!Number.isFinite(r)) return `${/* @__PURE__ */ i(r, t, n)} B`;
96
+ let a = [
97
+ "B",
98
+ "KB",
99
+ "MB",
100
+ "GB",
101
+ "TB",
102
+ "PB",
103
+ "EB",
104
+ "ZB",
105
+ "YB"
106
+ ], o = a.length, s = 0;
107
+ for (; Math.abs(r) / 1024 > .9 && s < o - 1;) r /= 1024, s++;
108
+ return `${/* @__PURE__ */ i(r, t, n)} ${a[s]}`;
109
+ }
110
+ /* @__NO_SIDE_EFFECTS__ */
111
+ function f(e, t = 0, n = null, r = !1) {
112
+ return /* @__PURE__ */ p(e, t, n, r ? {} : {
113
+ 3: " thousand",
114
+ 6: " million",
115
+ 9: " billion",
116
+ 12: " trillion",
117
+ 15: " quadrillion"
118
+ });
119
+ }
120
+ /* @__NO_SIDE_EFFECTS__ */
121
+ function p(e, t = 0, n = null, r = {}) {
122
+ if (!Number.isFinite(e)) return /* @__PURE__ */ i(e, t, n);
123
+ if (Object.keys(r).length === 0 && (r = {
124
+ 3: "K",
125
+ 6: "M",
126
+ 9: "B",
127
+ 12: "T",
128
+ 15: "Q"
129
+ }), Number(e) === 0) return t > 0 ? /* @__PURE__ */ i(0, t, n) : "0";
130
+ if (e < 0) {
131
+ let i = /* @__PURE__ */ p(Math.abs(e), t, n, r);
132
+ return i === /* @__PURE__ */ p(0, t, n, r) ? i : `-${i}`;
133
+ }
134
+ if (e >= 0x38d7ea4c68000) {
135
+ let i = Object.keys(r).map((e) => Number(e)).filter((e) => !Number.isNaN(e)).sort((e, t) => e - t), a = i[i.length - 1], o = r[a];
136
+ return `${/* @__PURE__ */ p(e / 0x38d7ea4c68000, t, n, r)}${o}`.trim();
137
+ }
138
+ let o = Math.floor(Math.log10(e)), s = Math.max(0, o - o % 3), c = e / 10 ** s, l;
139
+ if (l = n == null ? t > 0 ? /* @__PURE__ */ i(c, t, null) : s === 3 && c < 10 && Math.abs(e) % 10 == 0 ? /* @__PURE__ */ i(c, null, 1) : /* @__PURE__ */ i(c, 0, null) : /* @__PURE__ */ i(c, null, n), /* @__PURE__ */ a(l) >= 1e3 && r[s + 3] !== void 0) {
140
+ s += 3;
141
+ let e = c / 1e3;
142
+ l = n == null ? t > 0 ? /* @__PURE__ */ i(e, t, null) : /* @__PURE__ */ i(e, 0, null) : /* @__PURE__ */ i(e, null, n);
143
+ }
144
+ let u = r[s] ?? "";
145
+ return `${l}${u}`.trim();
146
+ }
147
+ /* @__NO_SIDE_EFFECTS__ */
148
+ function m(e, t, n) {
149
+ return Math.min(Math.max(e, t), n);
150
+ }
151
+ /* @__NO_SIDE_EFFECTS__ */
152
+ function h(t, n, r = 0, i = 1) {
153
+ if (n === 0) throw new e("The $by argument must not be zero.");
154
+ n = Math.abs(n);
155
+ let a = [], o = r;
156
+ for (; o < t; o += n) {
157
+ let e = o + n - i;
158
+ e > t && (e = t), a.push([o, e]);
159
+ }
160
+ let s = a.at(-1);
161
+ return s !== void 0 && o === t && s[1] < t && a.push([o, t]), a;
162
+ }
163
+ /* @__NO_SIDE_EFFECTS__ */
164
+ function g(e) {
165
+ return isFinite(e) ? JSON.parse(JSON.stringify(e)) : e;
166
+ }
167
+ /* @__NO_SIDE_EFFECTS__ */
168
+ function _(e, t = !0) {
169
+ return /* @__PURE__ */ v(Number(e) * 60, t);
170
+ }
171
+ /* @__NO_SIDE_EFFECTS__ */
172
+ function v(e, t = !0) {
173
+ let n = Number(e);
174
+ if ((!Number.isFinite(n) || n < 0) && (n = 0), n === 0) return "0 seconds";
175
+ let r = 3600, i = 24 * r, a = 7 * i, o = 4 * a, s = [
176
+ ["year", 12 * o],
177
+ ["month", o],
178
+ ["week", a],
179
+ ["day", i],
180
+ ["hour", r],
181
+ ["minute", 60],
182
+ ["second", 1]
183
+ ];
184
+ if (t) for (let [e, t] of s) {
185
+ let r = n / t;
186
+ if (r >= .5 || t === 1) {
187
+ let t = Math.round(r);
188
+ return `${t} ${e}${t === 1 ? "" : "s"}`;
189
+ }
190
+ }
191
+ let c = n, l = [];
192
+ for (let [e, t] of s) if (c >= t) {
193
+ let n = Math.floor(c / t);
194
+ c -= n * t, l.push(`${n} ${e}${n === 1 ? "" : "s"}`);
195
+ }
196
+ return l.join(", ");
197
+ }
198
+ function y(e, n) {
199
+ let r = t;
200
+ x(e);
201
+ try {
202
+ return n();
203
+ } finally {
204
+ x(r);
205
+ }
206
+ }
207
+ function b(e, t) {
208
+ let r = n;
209
+ S(e);
210
+ try {
211
+ return t();
212
+ } finally {
213
+ S(r);
214
+ }
215
+ }
216
+ function x(e) {
217
+ t = e;
218
+ }
219
+ function S(e) {
220
+ n = e;
221
+ }
222
+ /* @__NO_SIDE_EFFECTS__ */
223
+ function C() {
224
+ return t;
225
+ }
226
+ /* @__NO_SIDE_EFFECTS__ */
227
+ function w() {
228
+ return n;
229
+ }
230
+ function T(e, t) {
231
+ return e === 0 || e < 0 && t(Math.abs(e)) === t(0) ? 0 : e;
232
+ }
233
+ export { r as abbreviate, m as clamp, u as currency, w as defaultCurrency, C as defaultLocale, d as fileSize, f as forHumans, i as format, _ as minutesToHuman, c as ordinal, h as pairs, a as parse, s as parseFloat, o as parseInt, l as percentage, v as secondsToHuman, p as summarize, g as trim, S as useCurrency, x as useLocale, b as withCurrency, y as withLocale };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/speller/index.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAsB,KAAK,CACvB,MAAM,EAAE,MAAM,GAAG,MAAM,EACvB,MAAM,GAAE,MAAM,GAAG,IAAW,EAC5B,KAAK,GAAE,MAAM,GAAG,IAAW,EAC3B,KAAK,GAAE,MAAM,GAAG,IAAW,GAC5B,OAAO,CAAC,MAAM,CAAC,CAgBjB;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,YAAY,CAC9B,MAAM,EAAE,MAAM,GAAG,MAAM,EACvB,MAAM,GAAE,MAAM,GAAG,IAAW,GAC7B,OAAO,CAAC,MAAM,CAAC,CAKjB"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/speller/index.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,wBAAsB,KAAK,CACvB,MAAM,EAAE,MAAM,GAAG,MAAM,EACvB,MAAM,GAAE,MAAM,GAAG,IAAW,EAC5B,KAAK,GAAE,MAAM,GAAG,IAAW,EAC3B,KAAK,GAAE,MAAM,GAAG,IAAW,GAC5B,OAAO,CAAC,MAAM,CAAC,CAgBjB;AAED;;;;;;;;;;GAUG;AAEH,wBAAsB,YAAY,CAC9B,MAAM,EAAE,MAAM,GAAG,MAAM,EACvB,MAAM,GAAE,MAAM,GAAG,IAAW,GAC7B,OAAO,CAAC,MAAM,CAAC,CAKjB"}