@zinaid/num 0.0.6 → 0.0.8

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/index.js CHANGED
@@ -1,7 +1,6 @@
1
- import { clamp as a, currency as l, defaultCurrency as o, defaultLocale as n, fileSize as s, forHumans as u, format as t, minutesToHuman as c, ordinal as m, pairs as p, parse as i, parseFloat as f, parseInt as d, percentage as y, secondsToHuman as C, spell as H, spellOrdinal as L, summarize as h, trim as w, useCurrency as x, useLocale as z, withCurrency as S, withLocale as T } from "./num.js";
2
- import { Speller as F } from "./speller/index.js";
1
+ import { clamp as a, currency as l, defaultCurrency as o, defaultLocale as n, fileSize as s, forHumans as u, format as t, minutesToHuman as c, ordinal as m, pairs as i, parse as p, parseFloat as f, parseInt as d, percentage as y, secondsToHuman as C, summarize as H, trim as L, useCurrency as h, useLocale as w, withCurrency as x, withLocale as z } from "./num.js";
2
+ import { spell as g, spellOrdinal as F } from "./speller/index.js";
3
3
  export {
4
- F as Speller,
5
4
  a as clamp,
6
5
  l as currency,
7
6
  o as defaultCurrency,
@@ -11,18 +10,18 @@ export {
11
10
  t as format,
12
11
  c as minutesToHuman,
13
12
  m as ordinal,
14
- p as pairs,
15
- i as parse,
13
+ i as pairs,
14
+ p as parse,
16
15
  f as parseFloat,
17
16
  d as parseInt,
18
17
  y as percentage,
19
18
  C as secondsToHuman,
20
- H as spell,
21
- L as spellOrdinal,
22
- h as summarize,
23
- w as trim,
24
- x as useCurrency,
25
- z as useLocale,
26
- S as withCurrency,
27
- T as withLocale
19
+ g as spell,
20
+ F as spellOrdinal,
21
+ H as summarize,
22
+ L as trim,
23
+ h as useCurrency,
24
+ w as useLocale,
25
+ x as withCurrency,
26
+ z as withLocale
28
27
  };
package/dist/num.d.ts CHANGED
@@ -1,6 +1,12 @@
1
1
  /**
2
2
  * Format the given number according to the current locale.
3
3
  *
4
+ * @param number The number to format.
5
+ * @param precision The number of decimal places to use.
6
+ * @param maxPrecision The maximum number of decimal places to use.
7
+ * @param locale The locale to use for formatting.
8
+ * @returns The formatted number string.
9
+ *
4
10
  * @example
5
11
  *
6
12
  * format(1234.5678, 2); // "1,234.57"
@@ -11,6 +17,10 @@ export declare function format(number: number, precision?: number | null, maxPre
11
17
  /**
12
18
  * Parse the given string according to the specified format type.
13
19
  *
20
+ * @param value The string to parse.
21
+ * @param locale The locale to use for parsing.
22
+ * @returns The parsed number.
23
+ *
14
24
  * @example
15
25
  *
16
26
  * parse("1,234.57"); // 1234.57
@@ -19,6 +29,10 @@ export declare function parse(value: string, locale?: string | null): number;
19
29
  /**
20
30
  * Parse a string into an integer according to the specified locale.
21
31
  *
32
+ * @param value The string to parse.
33
+ * @param locale The locale to use for parsing.
34
+ * @returns The parsed integer or false if parsing fails.
35
+ *
22
36
  * @example
23
37
  *
24
38
  * parseInt("1,234", "en"); // 1234
@@ -27,19 +41,18 @@ export declare function parseInt(value: string, locale?: string | null): number
27
41
  /**
28
42
  * Parse a string into a float according to the specified locale.
29
43
  *
30
- * @param string $string
31
- * @param string|null $locale
32
- * @return float|false
44
+ * @param value The string to parse.
45
+ * @param locale The locale to use for parsing.
46
+ * @returns The parsed float or false if parsing fails.
33
47
  */
34
48
  export declare function parseFloat(value: string, locale?: string | null): number | false;
35
- /**
36
- * Spell out the given number in the given locale.
37
- * @todo
38
- */
39
- export declare function spell(_number: number | string, _locale?: string | null, _after?: number | null, _until?: number | null): string;
40
49
  /**
41
50
  * Convert the given number to ordinal form.
42
51
  *
52
+ * @param value The number to convert.
53
+ * @param locale The locale to use for determining ordinal rules.
54
+ * @returns The number in ordinal form.
55
+ *
43
56
  * @example
44
57
  *
45
58
  * ordinal(1); // "1st"
@@ -48,20 +61,15 @@ export declare function spell(_number: number | string, _locale?: string | null,
48
61
  * ordinal(13); // "13th"
49
62
  */
50
63
  export declare function ordinal(value: number, locale?: string | null): string;
51
- /**
52
- * Spell out the given number in the given locale in ordinal form.
53
- * @todo
54
- *
55
- * @example
56
- *
57
- * spellOrdinal(1); // "first"
58
- * spellOrdinal(2); // "second"
59
- * spellOrdinal(3); // "third"
60
- */
61
- export declare function spellOrdinal(_value: number, _locale?: string | null): string;
62
64
  /**
63
65
  * Convert the given number to its percentage equivalent.
64
66
  *
67
+ * @param number The number to convert.
68
+ * @param precision The number of decimal places to use.
69
+ * @param maxPrecision The maximum number of decimal places to use.
70
+ * @param locale The locale to use for formatting.
71
+ * @returns The formatted percentage string.
72
+ *
65
73
  * @example
66
74
  *
67
75
  * percentage(1); // "1%"
@@ -72,6 +80,12 @@ export declare function percentage(number: number | string, precision?: number,
72
80
  /**
73
81
  * Convert the given number to its currency equivalent.
74
82
  *
83
+ * @param amount The amount to convert.
84
+ * @param currencyCode The currency code to use (e.g. "USD", "EUR").
85
+ * @param locale The locale to use for formatting.
86
+ * @param precision The number of decimal places to use.
87
+ * @returns The formatted currency string.
88
+ *
75
89
  * @example
76
90
  *
77
91
  * currency(5.00); // "$5.00"
@@ -82,6 +96,11 @@ export declare function currency(amount: number | string, currencyCode?: string,
82
96
  /**
83
97
  * Convert the given number to its file size equivalent.
84
98
  *
99
+ * @param bytes The number of bytes.
100
+ * @param precision The number of decimal places to use.
101
+ * @param maxPrecision The maximum number of decimal places to use.
102
+ * @returns The formatted file size string.
103
+ *
85
104
  * @example
86
105
  *
87
106
  * fileSize(1024); // "1 KB"
@@ -92,6 +111,10 @@ export declare function fileSize(bytes: number | string, precision?: number, max
92
111
  /**
93
112
  * Convert the number to its human-readable equivalent.
94
113
  *
114
+ * @param value The number to convert.
115
+ * @param precision The number of decimal places to use.
116
+ * @param maxPrecision The maximum number of decimal places to use.
117
+ *
95
118
  * @example
96
119
  *
97
120
  * forHumans(1234); // "1.234 thousand"
@@ -102,6 +125,12 @@ export declare function forHumans(value: number, precision?: number, maxPrecisio
102
125
  /**
103
126
  * Convert the number to its human-readable equivalent.
104
127
  *
128
+ * @param value The number to convert.
129
+ * @param precision The number of decimal places to use.
130
+ * @param maxPrecision The maximum number of decimal places to use.
131
+ * @param units Custom units mapping (exponent to suffix).
132
+ * @returns The summarized number string.
133
+ *
105
134
  * @example
106
135
  *
107
136
  * summarize(1234); // "1.234 K"
@@ -112,6 +141,11 @@ export declare function summarize(value: number, precision?: number, maxPrecisio
112
141
  /**
113
142
  * Clamp the given number between the given minimum and maximum.
114
143
  *
144
+ * @param value The number to clamp.
145
+ * @param min The minimum value.
146
+ * @param max The maximum value.
147
+ * @returns The clamped number.
148
+ *
115
149
  * @example
116
150
  *
117
151
  * clamp(5, 1, 10); // 5
@@ -122,6 +156,12 @@ export declare function clamp(value: number, min: number, max: number): number;
122
156
  /**
123
157
  * Split the given number into pairs of min/max values.
124
158
  *
159
+ * @param to The maximum value.
160
+ * @param by The size of each pair.
161
+ * @param start The starting value.
162
+ * @param offset The offset to apply to the upper bound of each pair.
163
+ * @returns An array of [min, max] pairs.
164
+ *
125
165
  * @example
126
166
  *
127
167
  * pairs(25, 10, 1, 1); // [[1, 11], [11, 21], [21, 25]]
@@ -130,6 +170,9 @@ export declare function pairs(to: number, by: number, start?: number, offset?: n
130
170
  /**
131
171
  * Remove any trailing zero digits after the decimal point of the given number.
132
172
  *
173
+ * @param value The number to trim.
174
+ * @returns The trimmed number.
175
+ *
133
176
  * @example
134
177
  *
135
178
  * trim(12.3456789); // 12.3456789
@@ -165,6 +208,10 @@ export declare function secondsToHuman(seconds: number | string, round?: boolean
165
208
  /**
166
209
  * Execute the given callback using the given locale.
167
210
  *
211
+ * @param locale The locale to use.
212
+ * @param callback The callback to execute.
213
+ * @returns The result of the callback.
214
+ *
168
215
  * @example
169
216
  *
170
217
  * withLocale("fr", () => {
@@ -175,6 +222,10 @@ export declare function withLocale(locale: string, callback: () => unknown): unk
175
222
  /**
176
223
  * Execute the given callback using the given currency.
177
224
  *
225
+ * @param currency The currency to use.
226
+ * @param callback The callback to execute.
227
+ * @returns The result of the callback.
228
+ *
178
229
  * @example
179
230
  *
180
231
  * withCurrency("EUR", () => {
@@ -184,18 +235,36 @@ export declare function withLocale(locale: string, callback: () => unknown): unk
184
235
  export declare function withCurrency(currency: string, callback: () => unknown): unknown;
185
236
  /**
186
237
  * Set the default locale.
238
+ *
239
+ * @param locale The locale to set.
240
+ * @returns void
187
241
  */
188
242
  export declare function useLocale(locale: string): void;
189
243
  /**
190
244
  * Set the default currency.
245
+ *
246
+ * @param currency The currency to set.
247
+ * @returns void
191
248
  */
192
249
  export declare function useCurrency(currency: string): void;
193
250
  /**
194
251
  * Get the default locale.
252
+ *
253
+ * @returns The default locale.
254
+ *
255
+ * @example
256
+ *
257
+ * defaultLocale(); // "en"
195
258
  */
196
259
  export declare function defaultLocale(): string;
197
260
  /**
198
261
  * Get the default currency.
262
+ *
263
+ * @returns The default currency.
264
+ *
265
+ * @example
266
+ *
267
+ * defaultCurrency(); // "USD"
199
268
  */
200
269
  export declare function defaultCurrency(): string;
201
270
  //# sourceMappingURL=num.d.ts.map
package/dist/num.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"num.d.ts","sourceRoot":"","sources":["../src/num.ts"],"names":[],"mappings":"AAUA;;;;;;;;GAQG;AACH,wBAAgB,MAAM,CAClB,MAAM,EAAE,MAAM,EACd,SAAS,GAAE,MAAM,GAAG,IAAW,EAC/B,YAAY,GAAE,MAAM,GAAG,IAAW,EAClC,MAAM,GAAE,MAAM,GAAG,IAAW,GAC7B,MAAM,GAAG,KAAK,CAmBhB;AAED;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,GAAE,MAAM,GAAG,IAAW,GAAG,MAAM,CA2DzE;AAED;;;;;;GAMG;AACH,wBAAgB,QAAQ,CACpB,KAAK,EAAE,MAAM,EACb,MAAM,GAAE,MAAM,GAAG,IAAW,GAC7B,MAAM,GAAG,KAAK,CAQhB;AAED;;;;;;GAMG;AACH,wBAAgB,UAAU,CACtB,KAAK,EAAE,MAAM,EACb,MAAM,GAAE,MAAM,GAAG,IAAW,GAC7B,MAAM,GAA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;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,GAAG,OAAO,CAU3E;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,YAAY,CACxB,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,OAAO,GACxB,OAAO,CAUT;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAE9C;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAElD;AAED;;;;;;;;GAQG;AACH,wBAAgB,aAAa,IAAI,MAAM,CAEtC;AAED;;;;;;;;GAQG;AACH,wBAAgB,eAAe,IAAI,MAAM,CAExC"}
package/dist/num.js CHANGED
@@ -10,27 +10,27 @@ function g(t, n = null, e = null, r = null) {
10
10
  function $(t, n = null) {
11
11
  n = n ?? m;
12
12
  const r = new Intl.NumberFormat(n).formatToParts(12345.6), o = /* @__PURE__ */ new Set();
13
- let s = ".";
13
+ let l = ".";
14
14
  for (const i of r)
15
- i.type === "group" ? o.add(i.value) : i.type === "decimal" && (s = i.value);
16
- const u = [" ", " ", " "];
17
- for (const i of u) o.add(i);
18
- let l = t;
15
+ i.type === "group" ? o.add(i.value) : i.type === "decimal" && (l = i.value);
16
+ const s = [" ", " ", " "];
17
+ for (const i of s) o.add(i);
18
+ let u = t;
19
19
  for (const i of o) {
20
20
  const c = new RegExp(
21
21
  i.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&"),
22
22
  "g"
23
23
  );
24
- l = l.replace(c, "");
24
+ u = u.replace(c, "");
25
25
  }
26
- if (s !== ".") {
26
+ if (l !== ".") {
27
27
  const i = new RegExp(
28
- s.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&"),
28
+ l.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&"),
29
29
  "g"
30
- ), c = l.lastIndexOf(s);
31
- c !== -1 && (l = l.slice(0, c).replace(i, "") + "." + l.slice(c + s.length));
30
+ ), c = u.lastIndexOf(l);
31
+ c !== -1 && (u = u.slice(0, c).replace(i, "") + "." + u.slice(c + l.length));
32
32
  }
33
- return l = l.replace(/[\s\u00A0\u202F\u2009]+/g, "").trim(), globalThis.Number(l);
33
+ return u = u.replace(/[\s\u00A0\u202F\u2009]+/g, "").trim(), globalThis.Number(u);
34
34
  }
35
35
  function S(t, n = null) {
36
36
  const e = $(t, n);
@@ -39,15 +39,12 @@ function S(t, n = null) {
39
39
  function x(t, n = null) {
40
40
  return $(t, n);
41
41
  }
42
- function E(t, n = null, e = null, r = null) {
43
- return "";
44
- }
45
- function O(t, n = null) {
42
+ function E(t, n = null) {
46
43
  const e = n ?? m;
47
44
  try {
48
- const o = new Intl.PluralRules(e, { type: "ordinal" }).select(t), s = e.split("-")[0] || "en", u = {
45
+ const o = new Intl.PluralRules(e, { type: "ordinal" }).select(t), l = e.split("-")[0] || "en", s = {
49
46
  en: { one: "st", two: "nd", few: "rd", other: "th" }
50
- }, l = u[s] ?? u.en, p = l[o] ?? l.other;
47
+ }, u = s[l] ?? s.en, p = u[o] ?? u.other;
51
48
  return `${t}${p}`;
52
49
  } catch {
53
50
  const r = Math.abs(t), o = r % 100;
@@ -65,25 +62,22 @@ function O(t, n = null) {
65
62
  }
66
63
  }
67
64
  }
68
- function B(t, n = null) {
69
- return "";
70
- }
71
- function I(t, n = 0, e = null, r = null) {
72
- const o = r ?? m, s = typeof t == "string" ? $(t, o) : t, u = {
65
+ function B(t, n = 0, e = null, r = null) {
66
+ const o = r ?? m, l = typeof t == "string" ? $(t, o) : t, s = {
73
67
  style: "percent",
74
68
  useGrouping: !0
75
69
  };
76
- return e != null ? (u.maximumFractionDigits = e, u.minimumFractionDigits = 0) : (u.minimumFractionDigits = n, u.maximumFractionDigits = n), new Intl.NumberFormat(o, u).format(s / 100);
70
+ return e != null ? (s.maximumFractionDigits = e, s.minimumFractionDigits = 0) : (s.minimumFractionDigits = n, s.maximumFractionDigits = n), new Intl.NumberFormat(o, s).format(l / 100);
77
71
  }
78
- function T(t, n = "", e = null, r = null) {
79
- const o = e ?? m, s = typeof t == "string" ? $(t, o) : t, l = {
72
+ function O(t, n = "", e = null, r = null) {
73
+ const o = e ?? m, l = typeof t == "string" ? $(t, o) : t, u = {
80
74
  style: "currency",
81
75
  currency: n && n.length ? n : y,
82
76
  useGrouping: !0
83
77
  };
84
- return r != null && (l.minimumFractionDigits = r, l.maximumFractionDigits = r), new Intl.NumberFormat(o, l).format(s);
78
+ return r != null && (u.minimumFractionDigits = r, u.maximumFractionDigits = r), new Intl.NumberFormat(o, u).format(l);
85
79
  }
86
- function _(t, n = 0, e = null) {
80
+ function I(t, n = 0, e = null) {
87
81
  let r = typeof t == "string" ? Number(t) : t;
88
82
  Number.isFinite(r) || (r = 0);
89
83
  const o = [
@@ -96,13 +90,13 @@ function _(t, n = 0, e = null) {
96
90
  "EB",
97
91
  "ZB",
98
92
  "YB"
99
- ], s = o.length;
100
- let u = 0;
101
- for (; r / 1024 > 0.9 && u < s - 1; )
102
- r /= 1024, u++;
103
- return `${g(r, n, e)} ${o[u]}`;
93
+ ], l = o.length;
94
+ let s = 0;
95
+ for (; r / 1024 > 0.9 && s < l - 1; )
96
+ r /= 1024, s++;
97
+ return `${g(r, n, e)} ${o[s]}`;
104
98
  }
105
- function R(t, n = 0, e = null, r = !1) {
99
+ function T(t, n = 0, e = null, r = !1) {
106
100
  return b(
107
101
  t,
108
102
  n,
@@ -141,46 +135,46 @@ function b(t, n = 0, e = null, r = {}) {
141
135
  r
142
136
  )}${h}`.trim();
143
137
  }
144
- const o = Math.floor(Math.log10(t)), s = o - o % 3, u = t / Math.pow(10, s);
145
- let l;
138
+ const o = Math.floor(Math.log10(t)), l = o - o % 3, s = t / Math.pow(10, l);
139
+ let u;
146
140
  if (e != null)
147
- l = g(u, null, e);
141
+ u = g(s, null, e);
148
142
  else if (n > 0)
149
- l = g(u, n, null);
143
+ u = g(s, n, null);
150
144
  else {
151
145
  const i = Math.abs(t);
152
- l = s === 3 && u < 10 && i % 10 === 0 ? g(u, null, 1) : g(u, 0, null);
146
+ u = l === 3 && s < 10 && i % 10 === 0 ? g(s, null, 1) : g(s, 0, null);
153
147
  }
154
- const p = r[s] ?? "";
155
- return `${l}${p}`.trim();
148
+ const p = r[l] ?? "";
149
+ return `${u}${p}`.trim();
156
150
  }
157
- function G(t, n, e) {
151
+ function R(t, n, e) {
158
152
  return Math.min(Math.max(t, n), e);
159
153
  }
160
- function H(t, n, e = 0, r = 1) {
154
+ function G(t, n, e = 0, r = 1) {
161
155
  const o = [];
162
- for (let s = e; s < t; s += n) {
163
- let u = s + n - r;
164
- u > t && (u = t), o.push([s, u]);
156
+ for (let l = e; l < t; l += n) {
157
+ let s = l + n - r;
158
+ s > t && (s = t), o.push([l, s]);
165
159
  }
166
160
  return o;
167
161
  }
168
- function k(t) {
162
+ function H(t) {
169
163
  return JSON.parse(JSON.stringify(t));
170
164
  }
171
- function A(t, n = !0) {
165
+ function k(t, n = !0) {
172
166
  const e = Number(t) * 60;
173
167
  return D(e, n);
174
168
  }
175
169
  function D(t, n = !0) {
176
170
  let e = Number(t);
177
171
  if ((!Number.isFinite(e) || e < 0) && (e = 0), e === 0) return "0 seconds";
178
- const r = 1, o = 60 * r, s = 60 * o, u = 24 * s, l = 7 * u, p = 4 * l, c = [
172
+ const r = 1, o = 60 * r, l = 60 * o, s = 24 * l, u = 7 * s, p = 4 * u, c = [
179
173
  ["year", 12 * p],
180
174
  ["month", p],
181
- ["week", l],
182
- ["day", u],
183
- ["hour", s],
175
+ ["week", u],
176
+ ["day", s],
177
+ ["hour", l],
184
178
  ["minute", o],
185
179
  ["second", r]
186
180
  ];
@@ -201,7 +195,7 @@ function D(t, n = !0) {
201
195
  }
202
196
  return N.join(", ");
203
197
  }
204
- function C(t, n) {
198
+ function A(t, n) {
205
199
  const e = m;
206
200
  w(t);
207
201
  try {
@@ -210,7 +204,7 @@ function C(t, n) {
210
204
  w(e);
211
205
  }
212
206
  }
213
- function K(t, n) {
207
+ function C(t, n) {
214
208
  const e = y;
215
209
  M(t);
216
210
  try {
@@ -225,34 +219,32 @@ function w(t) {
225
219
  function M(t) {
226
220
  y = t;
227
221
  }
228
- function L() {
222
+ function K() {
229
223
  return m;
230
224
  }
231
- function Y() {
225
+ function L() {
232
226
  return y;
233
227
  }
234
228
  export {
235
- G as clamp,
236
- T as currency,
237
- Y as defaultCurrency,
238
- L as defaultLocale,
239
- _ as fileSize,
240
- R as forHumans,
229
+ R as clamp,
230
+ O as currency,
231
+ L as defaultCurrency,
232
+ K as defaultLocale,
233
+ I as fileSize,
234
+ T as forHumans,
241
235
  g as format,
242
- A as minutesToHuman,
243
- O as ordinal,
244
- H as pairs,
236
+ k as minutesToHuman,
237
+ E as ordinal,
238
+ G as pairs,
245
239
  $ as parse,
246
240
  x as parseFloat,
247
241
  S as parseInt,
248
- I as percentage,
242
+ B as percentage,
249
243
  D as secondsToHuman,
250
- E as spell,
251
- B as spellOrdinal,
252
244
  b as summarize,
253
- k as trim,
245
+ H as trim,
254
246
  M as useCurrency,
255
247
  w as useLocale,
256
- K as withCurrency,
257
- C as withLocale
248
+ C as withCurrency,
249
+ A as withLocale
258
250
  };
@@ -1,7 +1,48 @@
1
- import { ToWords as ToWordsType, ToWords } from 'to-words';
2
- export declare class Speller {
3
- protected toWords: ToWordsType | undefined;
4
- protected getToWords(): ToWords;
5
- spellNumber(num: number): string;
6
- }
1
+ /**
2
+ * Spell out the given number in the given locale.
3
+ *
4
+ * If `after` is provided and the number is less than or equal to `after`,
5
+ * the number will be returned as a formatted string instead of spelled out.
6
+ *
7
+ * If `until` is provided and the number is greater than or equal to `until`,
8
+ * the number will be returned as a formatted string instead of spelled out.
9
+ *
10
+ * @param number - The number to spell out.
11
+ * @param locale - The locale to use for spelling. Defaults to "en-US".
12
+ * @param after - If provided, numbers <= this value will be formatted instead of spelled.
13
+ * @param until - If provided, numbers >= this value will be formatted instead of spelled.
14
+ * @returns The number spelled out as words, or formatted as a string.
15
+ *
16
+ * @requires {@link https://www.npmjs.com/package/to-words to-words package}
17
+ *
18
+ * @example
19
+ *
20
+ * spell(123); // "One Hundred Twenty Three"
21
+ * spell(123.45); // "One Hundred Twenty Three Point Forty Five"
22
+ * spell(5, null, 10); // "5" (5 <= 10, so formatted instead of spelled)
23
+ * spell(11, null, 10); // "Eleven" (11 > 10, so spelled out)
24
+ * spell(100, null, null, 50); // "100" (100 >= 50, so formatted instead of spelled)
25
+ * spell(49, null, null, 50); // "Forty Nine" (49 < 50, so spelled out)
26
+ */
27
+ export declare function spell(number: number | string, locale?: string | null, after?: number | null, until?: number | null): string;
28
+ /**
29
+ * Spell out the given number in the given locale in ordinal form.
30
+ *
31
+ * Note: The to-words package doesn't have native ordinal support, so this
32
+ * function spells out the cardinal number. True ordinal conversion would
33
+ * require locale-specific suffix rules (e.g., "first", "second", "third").
34
+ *
35
+ * @param value - The number to spell out in ordinal form.
36
+ * @param locale - The locale to use for spelling. Defaults to "en-US".
37
+ * @returns The number spelled out as words.
38
+ *
39
+ * @requires {@link https://www.npmjs.com/package/to-words to-words package}
40
+ *
41
+ * @example
42
+ *
43
+ * spellOrdinal(1); // "One"
44
+ * spellOrdinal(2); // "Two"
45
+ * spellOrdinal(21); // "Twenty One"
46
+ */
47
+ export declare function spellOrdinal(value: number, locale?: string | null): string;
7
48
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/speller/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,UAAU,CAAC;AACvD,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAEnC,qBAAa,OAAO;IAChB,SAAS,CAAC,OAAO,EAAE,WAAW,GAAG,SAAS,CAAC;IAE3C,SAAS,CAAC,UAAU,IAAI,OAAO;IAOxB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;CAG1C"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/speller/index.ts"],"names":[],"mappings":"AA6EA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,KAAK,CACjB,MAAM,EAAE,MAAM,GAAG,MAAM,EACvB,MAAM,GAAE,MAAM,GAAG,IAAW,EAC5B,KAAK,GAAE,MAAM,GAAG,IAAW,EAC3B,KAAK,GAAE,MAAM,GAAG,IAAW,GAC5B,MAAM,CAwBR;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,YAAY,CACxB,KAAK,EAAE,MAAM,EACb,MAAM,GAAE,MAAM,GAAG,IAAW,GAC7B,MAAM,CAcR"}
@@ -1,13 +1,83 @@
1
- import { ToWords as r } from "to-words";
2
- class e {
3
- toWords;
4
- getToWords() {
5
- return this.toWords || (this.toWords = new r()), this.toWords;
6
- }
7
- spellNumber(o) {
8
- return this.getToWords().convert(o);
9
- }
1
+ import { format as s } from "@zinaid/num";
2
+ import { ToWords as u } from "to-words";
3
+ const l = /* @__PURE__ */ new Set([
4
+ "bn-IN",
5
+ "ee-EE",
6
+ "en-AE",
7
+ "en-AU",
8
+ "en-BD",
9
+ "en-GB",
10
+ "en-GH",
11
+ "en-IE",
12
+ "en-IN",
13
+ "en-MM",
14
+ "en-MA",
15
+ "en-MU",
16
+ "en-NG",
17
+ "en-NP",
18
+ "en-OM",
19
+ "en-PH",
20
+ "en-SA",
21
+ "en-US",
22
+ "es-AR",
23
+ "es-ES",
24
+ "es-MX",
25
+ "es-VE",
26
+ "fa-IR",
27
+ "fr-BE",
28
+ "fr-FR",
29
+ "fr-MA",
30
+ "fr-SA",
31
+ "gu-IN",
32
+ "hi-IN",
33
+ "kn-IN",
34
+ "ko-KR",
35
+ "lv-LV",
36
+ "mr-IN",
37
+ "nl-SR",
38
+ "np-NP",
39
+ "pt-BR",
40
+ "tr-TR",
41
+ "ur-PK",
42
+ "ar-AE",
43
+ "ar-MA",
44
+ "ar-SA"
45
+ ]);
46
+ function f(n) {
47
+ if (l.has(n))
48
+ return n;
49
+ const o = (n.split("-")[0] || n).toLowerCase();
50
+ for (const t of l)
51
+ if (t.toLowerCase().startsWith(o + "-"))
52
+ return t;
53
+ return "en-US";
54
+ }
55
+ function S(n, r = null, o = null, t = null) {
56
+ const e = typeof n == "string" ? parseFloat(n) : n;
57
+ if (o !== null && e <= o)
58
+ return s(e, null, null, r) || String(e);
59
+ if (t !== null && e >= t)
60
+ return s(e, null, null, r) || String(e);
61
+ const i = f(r ?? "en-US");
62
+ return new u({
63
+ localeCode: i,
64
+ converterOptions: {
65
+ currency: !1,
66
+ ignoreDecimal: !1
67
+ }
68
+ }).convert(e);
69
+ }
70
+ function d(n, r = null) {
71
+ const o = f(r ?? "en-US");
72
+ return new u({
73
+ localeCode: o,
74
+ converterOptions: {
75
+ currency: !1,
76
+ ignoreDecimal: !0
77
+ }
78
+ }).convert(Math.floor(n));
10
79
  }
11
80
  export {
12
- e as Speller
81
+ S as spell,
82
+ d as spellOrdinal
13
83
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zinaid/num",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "description": "Utilities for working with numbers similar to Laravel's Num class facade.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -9,7 +9,7 @@
9
9
  "license": "MIT",
10
10
  "sideEffects": false,
11
11
  "dependencies": {
12
- "@zinaid/utils": "^0.0.4"
12
+ "@zinaid/utils": "^0.0.5"
13
13
  },
14
14
  "devDependencies": {
15
15
  "to-words": "^4.7.0"