@tolki/num 2.1.0 → 2.2.1

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 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
- {"version":3,"file":"num.d.ts","sourceRoot":"","sources":["../src/num.ts"],"names":[],"mappings":"AAUA;;;;;;;;;GASG;AACH,wBAAgB,UAAU,CACtB,MAAM,EAAE,MAAM,EACd,SAAS,GAAE,MAAU,EACrB,YAAY,GAAE,MAAM,GAAG,IAAW,GACnC,MAAM,GAAG,KAAK,CAEhB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB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,KAAK,GAAE,MAAU,EACjB,MAAM,GAAE,MAAU,GACnB,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAoBpB;AAED;;;;;;;GAOG;AACH,wBAAgB,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAM1C;AAED;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAC1B,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,KAAK,GAAE,OAAc,GACtB,MAAM,CAIR;AAED;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAC1B,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,KAAK,GAAE,OAAc,GACtB,MAAM,CAoDR;AAED;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAC9B,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,OAAO,GACxB,OAAO,GAAG,IAAI,CAUhB;AAED;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAChC,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,OAAO,GACxB,OAAO,GAAG,IAAI,CAUhB;AAED;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAE9C;AAED;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAElD;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,IAAI,MAAM,CAEtC;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,IAAI,MAAM,CAExC"}
package/dist/num.js CHANGED
@@ -1,5 +1,5 @@
1
- let p = "en", $ = "USD";
2
- function x(t, n = 0, e = null) {
1
+ let p = "en", b = "USD";
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) {
@@ -35,14 +35,14 @@ function y(t, n = null) {
35
35
  }
36
36
  return s = s.replace(/[\s\u00A0\u202F\u2009]+/g, "").trim(), globalThis.Number(s);
37
37
  }
38
- function E(t, n = null) {
38
+ function B(t, n = null) {
39
39
  const e = y(t, n);
40
40
  return Number.isNaN(e) ? !1 : e < 0 ? Math.ceil(e) : Math.floor(e);
41
41
  }
42
- function B(t, n = null) {
42
+ function O(t, n = null) {
43
43
  return y(t, n);
44
44
  }
45
- function O(t, n = null) {
45
+ function x(t, n = null) {
46
46
  const e = n ?? p;
47
47
  try {
48
48
  const o = new Intl.PluralRules(e, { type: "ordinal" }).select(t), l = String(e.split("-")[0]), u = {
@@ -80,14 +80,15 @@ function I(t, n = 0, e = null, r = null) {
80
80
  function T(t, n = "", e = null, r = null) {
81
81
  const o = e ?? p, 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);
87
87
  }
88
88
  function R(t, n = 0, e = null) {
89
89
  let r = typeof t == "string" ? Number(t) : t;
90
- Number.isFinite(r) || (r = 0);
90
+ if (!Number.isFinite(r))
91
+ return `${f(r, n, e)} B`;
91
92
  const o = [
92
93
  "B",
93
94
  "KB",
@@ -105,7 +106,7 @@ function R(t, n = 0, e = null) {
105
106
  return `${f(r, n, e)} ${o[u]}`;
106
107
  }
107
108
  function S(t, n = 0, e = null, r = !1) {
108
- return N(
109
+ return $(
109
110
  t,
110
111
  n,
111
112
  e,
@@ -118,7 +119,9 @@ function S(t, n = 0, e = null, r = !1) {
118
119
  }
119
120
  );
120
121
  }
121
- function N(t, n = 0, e = null, r = {}) {
122
+ function $(t, n = 0, e = null, r = {}) {
123
+ if (!Number.isFinite(t))
124
+ return f(t, n, e);
122
125
  if (Object.keys(r).length === 0 && (r = {
123
126
  3: "K",
124
127
  6: "M",
@@ -127,16 +130,23 @@ function N(t, n = 0, e = null, r = {}) {
127
130
  15: "Q"
128
131
  }), Number(t) === 0)
129
132
  return n > 0 ? f(0, n, e) : "0";
130
- if (t < 0)
131
- return `-${N(
133
+ if (t < 0) {
134
+ const i = $(
132
135
  Math.abs(t),
133
136
  n,
134
137
  e,
135
138
  r
136
- )}`;
139
+ ), c = $(
140
+ 0,
141
+ n,
142
+ e,
143
+ r
144
+ );
145
+ return i === c ? i : `-${i}`;
146
+ }
137
147
  if (t >= 1e15) {
138
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];
139
- return `${N(
149
+ return `${$(
140
150
  t / 1e15,
141
151
  n,
142
152
  e,
@@ -163,12 +173,12 @@ function N(t, n = 0, e = null, r = {}) {
163
173
  const m = r[l] ?? "";
164
174
  return `${s}${m}`.trim();
165
175
  }
166
- function G(t, n, e) {
176
+ function z(t, n, e) {
167
177
  return Math.min(Math.max(t, n), e);
168
178
  }
169
- function H(t, n, e = 0, r = 1) {
179
+ function G(t, n, e = 0, r = 1) {
170
180
  if (n === 0)
171
- throw new Error("The $by argument must not be zero.");
181
+ throw new Error("The 'by' argument must not be zero.");
172
182
  n = Math.abs(n);
173
183
  const o = [];
174
184
  for (let l = e; l < t; l += n) {
@@ -177,10 +187,10 @@ function H(t, n, e = 0, r = 1) {
177
187
  }
178
188
  return o;
179
189
  }
180
- function k(t) {
190
+ function H(t) {
181
191
  return isFinite(t) ? JSON.parse(JSON.stringify(t)) : t;
182
192
  }
183
- function z(t, n = !0) {
193
+ function k(t, n = !0) {
184
194
  const e = Number(t) * 60;
185
195
  return D(e, n);
186
196
  }
@@ -205,13 +215,13 @@ function D(t, n = !0) {
205
215
  }
206
216
  }
207
217
  let h = e;
208
- const b = [];
218
+ const N = [];
209
219
  for (const [a, d] of c)
210
220
  if (h >= d) {
211
221
  const g = Math.floor(h / d);
212
- h -= g * d, b.push(`${g} ${a}${g === 1 ? "" : "s"}`);
222
+ h -= g * d, N.push(`${g} ${a}${g === 1 ? "" : "s"}`);
213
223
  }
214
- return b.join(", ");
224
+ return N.join(", ");
215
225
  }
216
226
  function A(t, n) {
217
227
  const e = p;
@@ -223,7 +233,7 @@ function A(t, n) {
223
233
  }
224
234
  }
225
235
  function C(t, n) {
226
- const e = $;
236
+ const e = b;
227
237
  M(t);
228
238
  try {
229
239
  return n();
@@ -235,33 +245,33 @@ function F(t) {
235
245
  p = t;
236
246
  }
237
247
  function M(t) {
238
- $ = t;
248
+ b = t;
239
249
  }
240
250
  function K() {
241
251
  return p;
242
252
  }
243
253
  function L() {
244
- return $;
254
+ return b;
245
255
  }
246
256
  export {
247
- x as abbreviate,
248
- G as clamp,
257
+ E as abbreviate,
258
+ z as clamp,
249
259
  T as currency,
250
260
  L as defaultCurrency,
251
261
  K as defaultLocale,
252
262
  R as fileSize,
253
263
  S as forHumans,
254
264
  f as format,
255
- z as minutesToHuman,
256
- O as ordinal,
257
- H as pairs,
265
+ k as minutesToHuman,
266
+ x as ordinal,
267
+ G as pairs,
258
268
  y as parse,
259
- B as parseFloat,
260
- E as parseInt,
269
+ O as parseFloat,
270
+ B as parseInt,
261
271
  I as percentage,
262
272
  D as secondsToHuman,
263
- N as summarize,
264
- k as trim,
273
+ $ as summarize,
274
+ H as trim,
265
275
  M as useCurrency,
266
276
  F as useLocale,
267
277
  C as withCurrency,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tolki/num",
3
- "version": "2.1.0",
3
+ "version": "2.2.1",
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",