@tolki/num 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Abraham Arango
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # Tolki JS Num Package
2
+
3
+ This package provides number utility functions similar to Laravel's Num class facade.
4
+
5
+ ## Documentation
6
+
7
+ The full documentation for the number utilities can be found at [https://tolki.abe.dev](https://tolki.abe.dev/numbers/).
@@ -0,0 +1,3 @@
1
+ export * from './num';
2
+ export * from './speller';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC;AACtB,cAAc,WAAW,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,27 @@
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
+ export {
4
+ a as clamp,
5
+ l as currency,
6
+ o as defaultCurrency,
7
+ n as defaultLocale,
8
+ s as fileSize,
9
+ u as forHumans,
10
+ t as format,
11
+ c as minutesToHuman,
12
+ m as ordinal,
13
+ i as pairs,
14
+ p as parse,
15
+ f as parseFloat,
16
+ d as parseInt,
17
+ y as percentage,
18
+ C as secondsToHuman,
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
27
+ };
package/dist/num.d.ts ADDED
@@ -0,0 +1,270 @@
1
+ /**
2
+ * Format the given number according to the current locale.
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
+ *
10
+ * @example
11
+ *
12
+ * format(1234.5678, 2); // "1,234.57"
13
+ * format(1234.5678, 2, 4); // "1,234.5678"
14
+ * format(1234.5678, 2, 4, "de"); // "1.234,5678"
15
+ */
16
+ export declare function format(number: number, precision?: number | null, maxPrecision?: number | null, locale?: string | null): string | false;
17
+ /**
18
+ * Parse the given string according to the specified format type.
19
+ *
20
+ * @param value The string to parse.
21
+ * @param locale The locale to use for parsing.
22
+ * @returns The parsed number.
23
+ *
24
+ * @example
25
+ *
26
+ * parse("1,234.57"); // 1234.57
27
+ */
28
+ export declare function parse(value: string, locale?: string | null): number;
29
+ /**
30
+ * Parse a string into an integer according to the specified locale.
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
+ *
36
+ * @example
37
+ *
38
+ * parseInt("1,234", "en"); // 1234
39
+ */
40
+ export declare function parseInt(value: string, locale?: string | null): number | false;
41
+ /**
42
+ * Parse a string into a float according to the specified locale.
43
+ *
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.
47
+ */
48
+ export declare function parseFloat(value: string, locale?: string | null): number | false;
49
+ /**
50
+ * Convert the given number to ordinal form.
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
+ *
56
+ * @example
57
+ *
58
+ * ordinal(1); // "1st"
59
+ * ordinal(2); // "2nd"
60
+ * ordinal(3); // "3rd"
61
+ * ordinal(13); // "13th"
62
+ */
63
+ export declare function ordinal(value: number, locale?: string | null): string;
64
+ /**
65
+ * Convert the given number to its percentage equivalent.
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
+ *
73
+ * @example
74
+ *
75
+ * percentage(1); // "1%"
76
+ * percentage(1.75, 2); // "1.75%"
77
+ * percentage(0.12345, 4); // "0.1235%"
78
+ */
79
+ export declare function percentage(number: number | string, precision?: number, maxPrecision?: number | null, locale?: string | null): string | false;
80
+ /**
81
+ * Convert the given number to its currency equivalent.
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
+ *
89
+ * @example
90
+ *
91
+ * currency(5.00); // "$5.00"
92
+ * currency(-5); // "-$5.00"
93
+ * currency(10, 'EUR'); // "€10.00"
94
+ */
95
+ export declare function currency(amount: number | string, currencyCode?: string, locale?: string | null, precision?: number | null): string | false;
96
+ /**
97
+ * Convert the given number to its file size equivalent.
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
+ *
104
+ * @example
105
+ *
106
+ * fileSize(1024); // "1 KB"
107
+ * fileSize(2048); // "2 KB"
108
+ * fileSize(1264.12345, 3); // "1.234 KB"
109
+ */
110
+ export declare function fileSize(bytes: number | string, precision?: number, maxPrecision?: number | null): string;
111
+ /**
112
+ * Convert the number to its human-readable equivalent.
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
+ *
118
+ * @example
119
+ *
120
+ * forHumans(1234); // "1.234 thousand"
121
+ * forHumans(1234567); // "1.234 million"
122
+ * forHumans(1234567890); // "1.234 billion"
123
+ */
124
+ export declare function forHumans(value: number, precision?: number, maxPrecision?: number | null, abbreviate?: boolean): string | false;
125
+ /**
126
+ * Convert the number to its human-readable equivalent.
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
+ *
134
+ * @example
135
+ *
136
+ * summarize(1234); // "1.234 K"
137
+ * summarize(1234567); // "1.234 M"
138
+ * summarize(1234567890); // "1.234 B"
139
+ */
140
+ export declare function summarize(value: number, precision?: number, maxPrecision?: number | null, units?: Record<number, string>): string | false;
141
+ /**
142
+ * Clamp the given number between the given minimum and maximum.
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
+ *
149
+ * @example
150
+ *
151
+ * clamp(5, 1, 10); // 5
152
+ * clamp(0, 1, 10); // 1
153
+ * clamp(15, 1, 10); // 10
154
+ */
155
+ export declare function clamp(value: number, min: number, max: number): number;
156
+ /**
157
+ * Split the given number into pairs of min/max values.
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
+ *
165
+ * @example
166
+ *
167
+ * pairs(25, 10, 1, 1); // [[1, 11], [11, 21], [21, 25]]
168
+ */
169
+ export declare function pairs(to: number, by: number, start?: number, offset?: number): [number, number][];
170
+ /**
171
+ * Remove any trailing zero digits after the decimal point of the given number.
172
+ *
173
+ * @param value The number to trim.
174
+ * @returns The trimmed number.
175
+ *
176
+ * @example
177
+ *
178
+ * trim(12.3456789); // 12.3456789
179
+ * trim(12.34567890000); // 12.3456789
180
+ */
181
+ export declare function trim(value: number): number;
182
+ /**
183
+ * Convert a duration in minutes to a human-readable format.
184
+ *
185
+ * @param minutes The duration in minutes.
186
+ * @param round Whether to round to the nearest unit.
187
+ * @returns The human-readable duration string.
188
+ *
189
+ * @example
190
+ *
191
+ * minutesToHuman(61); // "1 hour"
192
+ * minutesToHuman(61, false); // "1 hour, 1 minute"
193
+ */
194
+ export declare function minutesToHuman(minutes: number | string, round?: boolean): string;
195
+ /**
196
+ * Convert a duration in seconds to a human-readable format.
197
+ *
198
+ * @param seconds The duration in seconds.
199
+ * @param round Whether to round to the nearest unit.
200
+ * @returns The human-readable duration string.
201
+ *
202
+ * @example
203
+ *
204
+ * secondsToHuman(3661); // "1 hour"
205
+ * secondsToHuman(3661, false); // "1 hour, 1 minute, 1 second"
206
+ */
207
+ export declare function secondsToHuman(seconds: number | string, round?: boolean): string;
208
+ /**
209
+ * Execute the given callback using the given locale.
210
+ *
211
+ * @param locale The locale to use.
212
+ * @param callback The callback to execute.
213
+ * @returns The result of the callback.
214
+ *
215
+ * @example
216
+ *
217
+ * withLocale("fr", () => {
218
+ * return format(1234.56);
219
+ * }); // "1 234,56"
220
+ */
221
+ export declare function withLocale(locale: string, callback: () => unknown): unknown;
222
+ /**
223
+ * Execute the given callback using the given currency.
224
+ *
225
+ * @param currency The currency to use.
226
+ * @param callback The callback to execute.
227
+ * @returns The result of the callback.
228
+ *
229
+ * @example
230
+ *
231
+ * withCurrency("EUR", () => {
232
+ * return format(1234.56);
233
+ * }); // "€1,234.56"
234
+ */
235
+ export declare function withCurrency(currency: string, callback: () => unknown): unknown;
236
+ /**
237
+ * Set the default locale.
238
+ *
239
+ * @param locale The locale to set.
240
+ * @returns void
241
+ */
242
+ export declare function useLocale(locale: string): void;
243
+ /**
244
+ * Set the default currency.
245
+ *
246
+ * @param currency The currency to set.
247
+ * @returns void
248
+ */
249
+ export declare function useCurrency(currency: string): void;
250
+ /**
251
+ * Get the default locale.
252
+ *
253
+ * @returns The default locale.
254
+ *
255
+ * @example
256
+ *
257
+ * defaultLocale(); // "en"
258
+ */
259
+ export declare function defaultLocale(): string;
260
+ /**
261
+ * Get the default currency.
262
+ *
263
+ * @returns The default currency.
264
+ *
265
+ * @example
266
+ *
267
+ * defaultCurrency(); // "USD"
268
+ */
269
+ export declare function defaultCurrency(): string;
270
+ //# sourceMappingURL=num.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"num.d.ts","sourceRoot":"","sources":["../src/num.ts"],"names":[],"mappings":"AAUA;;;;;;;;;;;;;;GAcG;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;;;;;;;;;;GAUG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,GAAE,MAAM,GAAG,IAAW,GAAG,MAAM,CA2DzE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,QAAQ,CACpB,KAAK,EAAE,MAAM,EACb,MAAM,GAAE,MAAM,GAAG,IAAW,GAC7B,MAAM,GAAG,KAAK,CAQhB;AAED;;;;;;GAMG;AAC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= r ?? m;
4
+ const o = {
5
+ style: "decimal",
6
+ useGrouping: !0
7
+ };
8
+ return e != null ? (o.maximumFractionDigits = e, o.minimumFractionDigits = 0) : n != null && (o.minimumFractionDigits = n, o.maximumFractionDigits = n), new Intl.NumberFormat(r, o).format(t);
9
+ }
10
+ function $(t, n = null) {
11
+ n = n ?? m;
12
+ const r = new Intl.NumberFormat(n).formatToParts(12345.6), o = /* @__PURE__ */ new Set();
13
+ let l = ".";
14
+ for (const i of r)
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
+ for (const i of o) {
20
+ const c = new RegExp(
21
+ i.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&"),
22
+ "g"
23
+ );
24
+ u = u.replace(c, "");
25
+ }
26
+ if (l !== ".") {
27
+ const i = new RegExp(
28
+ l.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&"),
29
+ "g"
30
+ ), c = u.lastIndexOf(l);
31
+ c !== -1 && (u = u.slice(0, c).replace(i, "") + "." + u.slice(c + l.length));
32
+ }
33
+ return u = u.replace(/[\s\u00A0\u202F\u2009]+/g, "").trim(), globalThis.Number(u);
34
+ }
35
+ function S(t, n = null) {
36
+ const e = $(t, n);
37
+ return Number.isNaN(e) ? !1 : e < 0 ? Math.ceil(e) : Math.floor(e);
38
+ }
39
+ function x(t, n = null) {
40
+ return $(t, n);
41
+ }
42
+ function E(t, n = null) {
43
+ const e = n ?? m;
44
+ try {
45
+ const o = new Intl.PluralRules(e, { type: "ordinal" }).select(t), l = e.split("-")[0] || "en", s = {
46
+ en: { one: "st", two: "nd", few: "rd", other: "th" }
47
+ }, u = s[l] ?? s.en, p = u[o] ?? u.other;
48
+ return `${t}${p}`;
49
+ } catch {
50
+ const r = Math.abs(t), o = r % 100;
51
+ if (o >= 11 && o <= 13)
52
+ return `${t}th`;
53
+ switch (r % 10) {
54
+ case 1:
55
+ return `${t}st`;
56
+ case 2:
57
+ return `${t}nd`;
58
+ case 3:
59
+ return `${t}rd`;
60
+ default:
61
+ return `${t}th`;
62
+ }
63
+ }
64
+ }
65
+ function B(t, n = 0, e = null, r = null) {
66
+ const o = r ?? m, l = typeof t == "string" ? $(t, o) : t, s = {
67
+ style: "percent",
68
+ useGrouping: !0
69
+ };
70
+ return e != null ? (s.maximumFractionDigits = e, s.minimumFractionDigits = 0) : (s.minimumFractionDigits = n, s.maximumFractionDigits = n), new Intl.NumberFormat(o, s).format(l / 100);
71
+ }
72
+ function O(t, n = "", e = null, r = null) {
73
+ const o = e ?? m, l = typeof t == "string" ? $(t, o) : t, u = {
74
+ style: "currency",
75
+ currency: n && n.length ? n : y,
76
+ useGrouping: !0
77
+ };
78
+ return r != null && (u.minimumFractionDigits = r, u.maximumFractionDigits = r), new Intl.NumberFormat(o, u).format(l);
79
+ }
80
+ function I(t, n = 0, e = null) {
81
+ let r = typeof t == "string" ? Number(t) : t;
82
+ Number.isFinite(r) || (r = 0);
83
+ const o = [
84
+ "B",
85
+ "KB",
86
+ "MB",
87
+ "GB",
88
+ "TB",
89
+ "PB",
90
+ "EB",
91
+ "ZB",
92
+ "YB"
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]}`;
98
+ }
99
+ function T(t, n = 0, e = null, r = !1) {
100
+ return b(
101
+ t,
102
+ n,
103
+ e,
104
+ r ? {} : {
105
+ 3: " thousand",
106
+ 6: " million",
107
+ 9: " billion",
108
+ 12: " trillion",
109
+ 15: " quadrillion"
110
+ }
111
+ );
112
+ }
113
+ function b(t, n = 0, e = null, r = {}) {
114
+ if (Object.keys(r).length === 0 && (r = {
115
+ 3: "K",
116
+ 6: "M",
117
+ 9: "B",
118
+ 12: "T",
119
+ 15: "Q"
120
+ }), Number(t) === 0)
121
+ return n > 0 ? g(0, n, e) : "0";
122
+ if (t < 0)
123
+ return `-${b(
124
+ Math.abs(t),
125
+ n,
126
+ e,
127
+ r
128
+ )}`;
129
+ if (t >= 1e15) {
130
+ const i = Object.keys(r).map((a) => Number(a)).filter((a) => !Number.isNaN(a)).sort((a, f) => a - f), c = i[i.length - 1], h = r[c];
131
+ return `${b(
132
+ t / 1e15,
133
+ n,
134
+ e,
135
+ r
136
+ )}${h}`.trim();
137
+ }
138
+ const o = Math.floor(Math.log10(t)), l = o - o % 3, s = t / Math.pow(10, l);
139
+ let u;
140
+ if (e != null)
141
+ u = g(s, null, e);
142
+ else if (n > 0)
143
+ u = g(s, n, null);
144
+ else {
145
+ const i = Math.abs(t);
146
+ u = l === 3 && s < 10 && i % 10 === 0 ? g(s, null, 1) : g(s, 0, null);
147
+ }
148
+ const p = r[l] ?? "";
149
+ return `${u}${p}`.trim();
150
+ }
151
+ function R(t, n, e) {
152
+ return Math.min(Math.max(t, n), e);
153
+ }
154
+ function G(t, n, e = 0, r = 1) {
155
+ const o = [];
156
+ for (let l = e; l < t; l += n) {
157
+ let s = l + n - r;
158
+ s > t && (s = t), o.push([l, s]);
159
+ }
160
+ return o;
161
+ }
162
+ function H(t) {
163
+ return JSON.parse(JSON.stringify(t));
164
+ }
165
+ function k(t, n = !0) {
166
+ const e = Number(t) * 60;
167
+ return D(e, n);
168
+ }
169
+ function D(t, n = !0) {
170
+ let e = Number(t);
171
+ if ((!Number.isFinite(e) || e < 0) && (e = 0), e === 0) return "0 seconds";
172
+ const r = 1, o = 60 * r, l = 60 * o, s = 24 * l, u = 7 * s, p = 4 * u, c = [
173
+ ["year", 12 * p],
174
+ ["month", p],
175
+ ["week", u],
176
+ ["day", s],
177
+ ["hour", l],
178
+ ["minute", o],
179
+ ["second", r]
180
+ ];
181
+ if (n)
182
+ for (const [a, f] of c) {
183
+ const d = e / f;
184
+ if (d >= 0.5 || f === r) {
185
+ const F = Math.round(d);
186
+ return `${F} ${a}${F === 1 ? "" : "s"}`;
187
+ }
188
+ }
189
+ let h = e;
190
+ const N = [];
191
+ for (const [a, f] of c)
192
+ if (h >= f) {
193
+ const d = Math.floor(h / f);
194
+ h -= d * f, N.push(`${d} ${a}${d === 1 ? "" : "s"}`);
195
+ }
196
+ return N.join(", ");
197
+ }
198
+ function A(t, n) {
199
+ const e = m;
200
+ w(t);
201
+ try {
202
+ return n();
203
+ } finally {
204
+ w(e);
205
+ }
206
+ }
207
+ function C(t, n) {
208
+ const e = y;
209
+ M(t);
210
+ try {
211
+ return n();
212
+ } finally {
213
+ M(e);
214
+ }
215
+ }
216
+ function w(t) {
217
+ m = t;
218
+ }
219
+ function M(t) {
220
+ y = t;
221
+ }
222
+ function K() {
223
+ return m;
224
+ }
225
+ function L() {
226
+ return y;
227
+ }
228
+ export {
229
+ R as clamp,
230
+ O as currency,
231
+ L as defaultCurrency,
232
+ K as defaultLocale,
233
+ I as fileSize,
234
+ T as forHumans,
235
+ g as format,
236
+ k as minutesToHuman,
237
+ E as ordinal,
238
+ G as pairs,
239
+ $ as parse,
240
+ x as parseFloat,
241
+ S as parseInt,
242
+ B as percentage,
243
+ D as secondsToHuman,
244
+ b as summarize,
245
+ H as trim,
246
+ M as useCurrency,
247
+ w as useLocale,
248
+ C as withCurrency,
249
+ A as withLocale
250
+ };
@@ -0,0 +1,48 @@
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;
48
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,83 @@
1
+ import { format as s } from "@tolki/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));
79
+ }
80
+ export {
81
+ S as spell,
82
+ d as spellOrdinal
83
+ };
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@tolki/num",
3
+ "version": "1.0.0",
4
+ "description": "Utilities for working with numbers similar to Laravel's Num class facade.",
5
+ "author": "Abraham Arango <https://github.com/abetwothree>",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/abetwothree/tolki.git",
10
+ "directory": "packages/num"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/abetwothree/tolki/issues"
14
+ },
15
+ "type": "module",
16
+ "main": "dist/index.js",
17
+ "keywords": [
18
+ "number",
19
+ "numbers",
20
+ "num",
21
+ "laravel",
22
+ "utils",
23
+ "utilities"
24
+ ],
25
+ "sideEffects": false,
26
+ "dependencies": {
27
+ "@tolki/utils": "^1.0.0"
28
+ },
29
+ "devDependencies": {
30
+ "to-words": "^4.7.0"
31
+ },
32
+ "peerDependencies": {
33
+ "to-words": "^4.7.0"
34
+ },
35
+ "files": [
36
+ "dist"
37
+ ],
38
+ "scripts": {
39
+ "build": "vite build"
40
+ },
41
+ "types": "dist/index.d.ts",
42
+ "exports": {
43
+ ".": {
44
+ "import": "./dist/index.js",
45
+ "require": "./dist/index.js",
46
+ "types": "./dist/index.d.ts"
47
+ },
48
+ "./*": {
49
+ "import": "./dist/*",
50
+ "require": "./dist/*",
51
+ "types": "./dist/*"
52
+ },
53
+ "./num": {
54
+ "import": "./dist/num.js",
55
+ "types": "./dist/num.d.ts"
56
+ },
57
+ "./speller": {
58
+ "import": "./dist/speller/index.js",
59
+ "types": "./dist/speller/index.d.ts"
60
+ }
61
+ }
62
+ }