@tolki/num 2.2.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,4 +1,4 @@
1
- let p = "en", $ = "USD";
1
+ let p = "en", b = "USD";
2
2
  function E(t, n = 0, e = null) {
3
3
  return S(t, n, e, !0);
4
4
  }
@@ -39,10 +39,10 @@ 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 x(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,7 +80,7 @@ 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);
@@ -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 N(
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 N(t, n = 0, e = null, r = {}) {
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
- return `-${N(
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 `${N(
149
+ return `${$(
143
150
  t / 1e15,
144
151
  n,
145
152
  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 G(t, n, e) {
176
+ function z(t, n, e) {
170
177
  return Math.min(Math.max(t, n), e);
171
178
  }
172
- function H(t, n, e = 0, r = 1) {
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 k(t) {
190
+ function H(t) {
184
191
  return isFinite(t) ? JSON.parse(JSON.stringify(t)) : t;
185
192
  }
186
- function z(t, n = !0) {
193
+ function k(t, n = !0) {
187
194
  const e = Number(t) * 60;
188
195
  return D(e, n);
189
196
  }
@@ -208,13 +215,13 @@ function D(t, n = !0) {
208
215
  }
209
216
  }
210
217
  let h = e;
211
- const b = [];
218
+ const N = [];
212
219
  for (const [a, d] of c)
213
220
  if (h >= d) {
214
221
  const g = Math.floor(h / d);
215
- h -= g * d, b.push(`${g} ${a}${g === 1 ? "" : "s"}`);
222
+ h -= g * d, N.push(`${g} ${a}${g === 1 ? "" : "s"}`);
216
223
  }
217
- return b.join(", ");
224
+ return N.join(", ");
218
225
  }
219
226
  function A(t, n) {
220
227
  const e = p;
@@ -226,7 +233,7 @@ function A(t, n) {
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();
@@ -238,33 +245,33 @@ function F(t) {
238
245
  p = t;
239
246
  }
240
247
  function M(t) {
241
- $ = t;
248
+ b = t;
242
249
  }
243
250
  function K() {
244
251
  return p;
245
252
  }
246
253
  function L() {
247
- return $;
254
+ return b;
248
255
  }
249
256
  export {
250
257
  E as abbreviate,
251
- G as clamp,
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
- z as minutesToHuman,
259
- O as ordinal,
260
- H as pairs,
265
+ k as minutesToHuman,
266
+ x as ordinal,
267
+ G as pairs,
261
268
  y as parse,
262
- x as parseFloat,
269
+ O as parseFloat,
263
270
  B as parseInt,
264
271
  I as percentage,
265
272
  D as secondsToHuman,
266
- N as summarize,
267
- k as trim,
273
+ $ as summarize,
274
+ H as trim,
268
275
  M as useCurrency,
269
276
  F as useLocale,
270
277
  C as withCurrency,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tolki/num",
3
- "version": "2.2.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",