@momotombodevs/istmo 0.0.0-stage → 0.2.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.
Files changed (52) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/LICENSE +21 -0
  3. package/README.md +458 -2
  4. package/dist/dates.cjs +314 -0
  5. package/dist/dates.d.cts +127 -0
  6. package/dist/dates.d.ts +127 -0
  7. package/dist/dates.js +302 -0
  8. package/dist/formatted-input.cjs +51 -0
  9. package/dist/formatted-input.js +49 -0
  10. package/dist/index.cjs +38 -0
  11. package/dist/index.d.cts +6 -0
  12. package/dist/index.d.ts +6 -0
  13. package/dist/index.js +6 -0
  14. package/dist/metadata.cjs +3 -0
  15. package/dist/metadata.js +4 -0
  16. package/dist/money.cjs +172 -0
  17. package/dist/money.d.cts +69 -0
  18. package/dist/money.d.ts +69 -0
  19. package/dist/money.js +166 -0
  20. package/dist/phone-examples.json.cjs +11 -0
  21. package/dist/phone-examples.json.js +11 -0
  22. package/dist/phone-input.cjs +34 -0
  23. package/dist/phone-input.d.cts +18 -0
  24. package/dist/phone-input.d.ts +18 -0
  25. package/dist/phone-input.js +33 -0
  26. package/dist/phone-metadata.json.cjs +224 -0
  27. package/dist/phone-metadata.json.js +224 -0
  28. package/dist/phone.cjs +121 -0
  29. package/dist/phone.d.cts +78 -0
  30. package/dist/phone.d.ts +78 -0
  31. package/dist/phone.js +117 -0
  32. package/dist/react.cjs +274 -0
  33. package/dist/react.d.cts +148 -0
  34. package/dist/react.d.ts +148 -0
  35. package/dist/react.js +268 -0
  36. package/dist/regions.cjs +94 -0
  37. package/dist/regions.d.cts +77 -0
  38. package/dist/regions.d.ts +77 -0
  39. package/dist/regions.js +85 -0
  40. package/dist/regions.json.cjs +136 -0
  41. package/dist/regions.json.js +125 -0
  42. package/dist/require.cjs +14 -0
  43. package/dist/require.js +13 -0
  44. package/flags/LICENSE +21 -0
  45. package/flags/bz.svg +145 -0
  46. package/flags/cr.svg +7 -0
  47. package/flags/gt.svg +204 -0
  48. package/flags/hn.svg +18 -0
  49. package/flags/ni.svg +129 -0
  50. package/flags/pa.svg +7 -0
  51. package/flags/sv.svg +593 -0
  52. package/package.json +201 -4
@@ -0,0 +1,69 @@
1
+ import { CurrencyCode } from "./regions.cjs";
2
+ /** How {@link formatMoney} labels the amount. */
3
+ export type MoneyDisplay = "symbol" | "code" | "none";
4
+ export interface FormatMoneyOptions {
5
+ /** `symbol` (default) shows `C$`, `code` shows `NIO`, `none` shows only the number. */
6
+ readonly display?: MoneyDisplay;
7
+ /** `always` (default) keeps every decimal; `auto` drops them when they are all zero (`C$ 180`). */
8
+ readonly decimals?: "always" | "auto";
9
+ }
10
+ /** An amount as decimal text with `.` as separator, such as `"1250.50"`, or a safe JavaScript number. */
11
+ export type Amount = string | number;
12
+ /** Longest integer part accepted, in digits. Large enough for any `decimal` database column. */
13
+ export declare const MAX_INTEGER_DIGITS = 30;
14
+ /**
15
+ * Formats an amount for display. Rounds half away from zero on the decimal text, so `1.005` gives `1.01`.
16
+ * The label and the number are joined with a non-breaking space so they never wrap apart.
17
+ *
18
+ * @param amount Decimal text with `.` (`"1250.5"`, as stored in a `decimal` column) or a safe number.
19
+ * @throws {RangeError} When the currency is not supported or the amount is not a decimal.
20
+ * @example formatMoney('1250.5', 'NIO') // "C$ 1,250.50"
21
+ * @example formatMoney('1234567.5', 'CRC') // "₡ 1,234,567.50"
22
+ * @example formatMoney('180', 'NIO', { decimals: 'auto' }) // "C$ 180"
23
+ */
24
+ export declare function formatMoney(amount: Amount, currencyCode: CurrencyCode, options?: FormatMoneyOptions): string;
25
+ /**
26
+ * Reads an amount typed by a person and returns it as decimal text with exactly the currency's decimals,
27
+ * ready to send to the server. Accepts an optional symbol or code of that currency (any case) and the three ways
28
+ * amounts are written in the region: `1,234.50`, `1.234,50` and `1 234,50`. A separator followed by one or two
29
+ * digits at the end is the decimal one; the others must all be the same and group exactly three digits, so
30
+ * `12.345` is twelve thousand. Negative amounts are rejected.
31
+ *
32
+ * @returns The normalized amount, or `null` when the text is not a valid amount in that currency.
33
+ * @throws {RangeError} When the currency is not supported.
34
+ * @example parseMoney('C$ 1,250.50', 'NIO') // "1250.50"
35
+ * @example parseMoney('₡1.234,5', 'CRC') // "1234.50"
36
+ * @example parseMoney('12.345', 'NIO') // "12345.00"
37
+ * @example parseMoney('$20', 'NIO') // null, symbol of another currency
38
+ */
39
+ export declare function parseMoney(input: unknown, currencyCode: CurrencyCode): string | null;
40
+ /**
41
+ * Converts an amount to an integer count of the currency's minor unit (cents), rounding like {@link formatMoney}.
42
+ *
43
+ * @throws {RangeError} When the currency is not supported, the amount is not a decimal, or the result is not a safe integer.
44
+ * @example toMinorUnits('12.34', 'USD') // 1234
45
+ */
46
+ export declare function toMinorUnits(amount: Amount, currencyCode: CurrencyCode): number;
47
+ /**
48
+ * Converts an integer count of minor units back to decimal text.
49
+ *
50
+ * @throws {RangeError} When the currency is not supported or `minor` is not a safe integer.
51
+ * @example fromMinorUnits(1234, 'USD') // "12.34"
52
+ */
53
+ export declare function fromMinorUnits(minor: number, currencyCode: CurrencyCode): string;
54
+ /** What {@link formatMoneyInput} returns. */
55
+ export interface MoneyInputText {
56
+ /** The text to show in the field, grouped while typing, e.g. `1,250.5`. */
57
+ readonly text: string;
58
+ /** The amount as decimal text with every decimal of the currency (`"1250.50"`), or `null` while there is none. */
59
+ readonly amount: string | null;
60
+ }
61
+ /**
62
+ * Formats an amount while it is being typed, for the value of a text field. Keeps digits and the first `.` as the
63
+ * decimal point, groups thousands with `,`, and cuts the decimals the currency does not have.
64
+ *
65
+ * @throws {RangeError} When the currency is not supported.
66
+ * @example formatMoneyInput('1250.5', 'NIO') // { text: '1,250.5', amount: '1250.50' }
67
+ * @example formatMoneyInput('.', 'NIO') // { text: '0.', amount: '0.00' }
68
+ */
69
+ export declare function formatMoneyInput(text: unknown, currencyCode: CurrencyCode): MoneyInputText;
@@ -0,0 +1,69 @@
1
+ import { CurrencyCode } from "./regions.js";
2
+ /** How {@link formatMoney} labels the amount. */
3
+ export type MoneyDisplay = "symbol" | "code" | "none";
4
+ export interface FormatMoneyOptions {
5
+ /** `symbol` (default) shows `C$`, `code` shows `NIO`, `none` shows only the number. */
6
+ readonly display?: MoneyDisplay;
7
+ /** `always` (default) keeps every decimal; `auto` drops them when they are all zero (`C$ 180`). */
8
+ readonly decimals?: "always" | "auto";
9
+ }
10
+ /** An amount as decimal text with `.` as separator, such as `"1250.50"`, or a safe JavaScript number. */
11
+ export type Amount = string | number;
12
+ /** Longest integer part accepted, in digits. Large enough for any `decimal` database column. */
13
+ export declare const MAX_INTEGER_DIGITS = 30;
14
+ /**
15
+ * Formats an amount for display. Rounds half away from zero on the decimal text, so `1.005` gives `1.01`.
16
+ * The label and the number are joined with a non-breaking space so they never wrap apart.
17
+ *
18
+ * @param amount Decimal text with `.` (`"1250.5"`, as stored in a `decimal` column) or a safe number.
19
+ * @throws {RangeError} When the currency is not supported or the amount is not a decimal.
20
+ * @example formatMoney('1250.5', 'NIO') // "C$ 1,250.50"
21
+ * @example formatMoney('1234567.5', 'CRC') // "₡ 1,234,567.50"
22
+ * @example formatMoney('180', 'NIO', { decimals: 'auto' }) // "C$ 180"
23
+ */
24
+ export declare function formatMoney(amount: Amount, currencyCode: CurrencyCode, options?: FormatMoneyOptions): string;
25
+ /**
26
+ * Reads an amount typed by a person and returns it as decimal text with exactly the currency's decimals,
27
+ * ready to send to the server. Accepts an optional symbol or code of that currency (any case) and the three ways
28
+ * amounts are written in the region: `1,234.50`, `1.234,50` and `1 234,50`. A separator followed by one or two
29
+ * digits at the end is the decimal one; the others must all be the same and group exactly three digits, so
30
+ * `12.345` is twelve thousand. Negative amounts are rejected.
31
+ *
32
+ * @returns The normalized amount, or `null` when the text is not a valid amount in that currency.
33
+ * @throws {RangeError} When the currency is not supported.
34
+ * @example parseMoney('C$ 1,250.50', 'NIO') // "1250.50"
35
+ * @example parseMoney('₡1.234,5', 'CRC') // "1234.50"
36
+ * @example parseMoney('12.345', 'NIO') // "12345.00"
37
+ * @example parseMoney('$20', 'NIO') // null, symbol of another currency
38
+ */
39
+ export declare function parseMoney(input: unknown, currencyCode: CurrencyCode): string | null;
40
+ /**
41
+ * Converts an amount to an integer count of the currency's minor unit (cents), rounding like {@link formatMoney}.
42
+ *
43
+ * @throws {RangeError} When the currency is not supported, the amount is not a decimal, or the result is not a safe integer.
44
+ * @example toMinorUnits('12.34', 'USD') // 1234
45
+ */
46
+ export declare function toMinorUnits(amount: Amount, currencyCode: CurrencyCode): number;
47
+ /**
48
+ * Converts an integer count of minor units back to decimal text.
49
+ *
50
+ * @throws {RangeError} When the currency is not supported or `minor` is not a safe integer.
51
+ * @example fromMinorUnits(1234, 'USD') // "12.34"
52
+ */
53
+ export declare function fromMinorUnits(minor: number, currencyCode: CurrencyCode): string;
54
+ /** What {@link formatMoneyInput} returns. */
55
+ export interface MoneyInputText {
56
+ /** The text to show in the field, grouped while typing, e.g. `1,250.5`. */
57
+ readonly text: string;
58
+ /** The amount as decimal text with every decimal of the currency (`"1250.50"`), or `null` while there is none. */
59
+ readonly amount: string | null;
60
+ }
61
+ /**
62
+ * Formats an amount while it is being typed, for the value of a text field. Keeps digits and the first `.` as the
63
+ * decimal point, groups thousands with `,`, and cuts the decimals the currency does not have.
64
+ *
65
+ * @throws {RangeError} When the currency is not supported.
66
+ * @example formatMoneyInput('1250.5', 'NIO') // { text: '1,250.5', amount: '1250.50' }
67
+ * @example formatMoneyInput('.', 'NIO') // { text: '0.', amount: '0.00' }
68
+ */
69
+ export declare function formatMoneyInput(text: unknown, currencyCode: CurrencyCode): MoneyInputText;
package/dist/money.js ADDED
@@ -0,0 +1,166 @@
1
+ import { requireCurrency } from "./require.js";
2
+ // src/money.ts
3
+ /** Longest integer part accepted, in digits. Large enough for any `decimal` database column. */
4
+ const MAX_INTEGER_DIGITS = 30;
5
+ const MAX_INPUT_LENGTH = 64;
6
+ const DECIMAL = /^(-)?(\d+)(?:\.(\d+))?$/;
7
+ const NBSP = "\xA0";
8
+ const SPACES = " \xA0 ";
9
+ function toDecimal(amount) {
10
+ if (typeof amount === "number" && (!Number.isFinite(amount) || Math.abs(amount) > Number.MAX_SAFE_INTEGER)) throw new RangeError("Monto inválido: use un texto decimal para montos fuera del rango seguro de number.");
11
+ const text = typeof amount === "number" ? String(amount) : typeof amount === "string" ? amount.trim() : "";
12
+ const match = DECIMAL.exec(text);
13
+ if (!match || (match[2] ?? "").replace(/^0+(?=\d)/, "").length > 30) throw new RangeError("Monto inválido: se espera un decimal como \"1250.50\".");
14
+ return {
15
+ negative: match[1] === "-",
16
+ integer: match[2] ?? "0",
17
+ fraction: match[3] ?? ""
18
+ };
19
+ }
20
+ function increment(digits) {
21
+ let result = "";
22
+ let carry = 1;
23
+ for (let index = digits.length - 1; index >= 0; index--) {
24
+ const sum = Number(digits[index]) + carry;
25
+ result = String(sum % 10) + result;
26
+ carry = sum > 9 ? 1 : 0;
27
+ }
28
+ return carry ? `1${result}` : result;
29
+ }
30
+ function round({ negative, integer, fraction }, decimals) {
31
+ const kept = `${integer}${fraction.padEnd(decimals, "0").slice(0, decimals)}`;
32
+ const digits = (Number(fraction[decimals] ?? "0") >= 5 ? increment(kept) : kept).replace(/^0+(?=\d)/, "").padStart(decimals + 1, "0");
33
+ const result = {
34
+ integer: digits.slice(0, digits.length - decimals),
35
+ fraction: decimals > 0 ? digits.slice(-decimals) : ""
36
+ };
37
+ return {
38
+ negative: negative && /[1-9]/.test(digits),
39
+ ...result
40
+ };
41
+ }
42
+ function group(integer, separator) {
43
+ const head = integer.length % 3 || 3;
44
+ const parts = [integer.slice(0, head)];
45
+ for (let index = head; index < integer.length; index += 3) parts.push(integer.slice(index, index + 3));
46
+ return parts.join(separator);
47
+ }
48
+ /**
49
+ * Formats an amount for display. Rounds half away from zero on the decimal text, so `1.005` gives `1.01`.
50
+ * The label and the number are joined with a non-breaking space so they never wrap apart.
51
+ *
52
+ * @param amount Decimal text with `.` (`"1250.5"`, as stored in a `decimal` column) or a safe number.
53
+ * @throws {RangeError} When the currency is not supported or the amount is not a decimal.
54
+ * @example formatMoney('1250.5', 'NIO') // "C$ 1,250.50"
55
+ * @example formatMoney('1234567.5', 'CRC') // "₡ 1,234,567.50"
56
+ * @example formatMoney('180', 'NIO', { decimals: 'auto' }) // "C$ 180"
57
+ */
58
+ function formatMoney(amount, currencyCode, options = {}) {
59
+ const target = requireCurrency(currencyCode);
60
+ const { negative, integer, fraction } = round(toDecimal(amount), target.decimals);
61
+ const hideFraction = options.decimals === "auto" && !/[1-9]/.test(fraction);
62
+ const number = `${group(integer, target.groupSeparator)}${hideFraction || !fraction ? "" : target.decimalSeparator + fraction}`;
63
+ const display = options.display ?? "symbol";
64
+ const label = display === "none" ? "" : `${display === "code" ? target.code : target.symbol}${NBSP}`;
65
+ return `${negative ? "-" : ""}${label}${number}`;
66
+ }
67
+ function escapeRegExp(text) {
68
+ return text.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&");
69
+ }
70
+ const GROUPS = [
71
+ ",",
72
+ "\\.",
73
+ `[${SPACES}]`
74
+ ];
75
+ function amountPatterns(decimals) {
76
+ const fraction = decimals > 0 ? `(?:[.,](\\d{1,${decimals}}))?` : "";
77
+ return [new RegExp(`^(\\d+)${fraction}$`), ...GROUPS.map((group) => {
78
+ const decimal = decimals > 0 ? `(?:${group === "," ? "\\." : group === "\\." ? "," : "[.,]"}(\\d{1,${decimals}}))?` : "";
79
+ return new RegExp(`^(\\d{1,3}(?:${group}\\d{3})+)${decimal}$`);
80
+ })];
81
+ }
82
+ /**
83
+ * Reads an amount typed by a person and returns it as decimal text with exactly the currency's decimals,
84
+ * ready to send to the server. Accepts an optional symbol or code of that currency (any case) and the three ways
85
+ * amounts are written in the region: `1,234.50`, `1.234,50` and `1 234,50`. A separator followed by one or two
86
+ * digits at the end is the decimal one; the others must all be the same and group exactly three digits, so
87
+ * `12.345` is twelve thousand. Negative amounts are rejected.
88
+ *
89
+ * @returns The normalized amount, or `null` when the text is not a valid amount in that currency.
90
+ * @throws {RangeError} When the currency is not supported.
91
+ * @example parseMoney('C$ 1,250.50', 'NIO') // "1250.50"
92
+ * @example parseMoney('₡1.234,5', 'CRC') // "1234.50"
93
+ * @example parseMoney('12.345', 'NIO') // "12345.00"
94
+ * @example parseMoney('$20', 'NIO') // null, symbol of another currency
95
+ */
96
+ function parseMoney(input, currencyCode) {
97
+ const target = requireCurrency(currencyCode);
98
+ if (typeof input !== "string" || input.length > MAX_INPUT_LENGTH) return null;
99
+ let text = input.trim();
100
+ for (const label of [target.code, target.symbol]) {
101
+ const prefix = new RegExp(`^${escapeRegExp(label)}[${SPACES}]*`, "i");
102
+ if (prefix.test(text)) {
103
+ text = text.replace(prefix, "");
104
+ break;
105
+ }
106
+ }
107
+ const match = amountPatterns(target.decimals).map((pattern) => pattern.exec(text)).find((result) => result !== null);
108
+ if (!match) return null;
109
+ const integer = (match[1] ?? "").replace(/\D/g, "").replace(/^0+(?=\d)/, "");
110
+ const fraction = match[2] ?? "";
111
+ if (integer.length > 30) return null;
112
+ return target.decimals > 0 ? `${integer}.${fraction.padEnd(target.decimals, "0")}` : integer;
113
+ }
114
+ /**
115
+ * Converts an amount to an integer count of the currency's minor unit (cents), rounding like {@link formatMoney}.
116
+ *
117
+ * @throws {RangeError} When the currency is not supported, the amount is not a decimal, or the result is not a safe integer.
118
+ * @example toMinorUnits('12.34', 'USD') // 1234
119
+ */
120
+ function toMinorUnits(amount, currencyCode) {
121
+ const target = requireCurrency(currencyCode);
122
+ const { negative, integer, fraction } = round(toDecimal(amount), target.decimals);
123
+ const minor = Number(`${integer}${fraction}`);
124
+ if (!Number.isSafeInteger(minor)) throw new RangeError("Monto demasiado grande para unidades menores.");
125
+ return negative ? -minor : minor;
126
+ }
127
+ /**
128
+ * Converts an integer count of minor units back to decimal text.
129
+ *
130
+ * @throws {RangeError} When the currency is not supported or `minor` is not a safe integer.
131
+ * @example fromMinorUnits(1234, 'USD') // "12.34"
132
+ */
133
+ function fromMinorUnits(minor, currencyCode) {
134
+ const target = requireCurrency(currencyCode);
135
+ if (!Number.isSafeInteger(minor)) throw new RangeError("Las unidades menores tienen que ser un entero seguro.");
136
+ const digits = Math.abs(minor).toString().padStart(target.decimals + 1, "0");
137
+ const integer = digits.slice(0, digits.length - target.decimals);
138
+ const fraction = target.decimals > 0 ? `.${digits.slice(-target.decimals)}` : "";
139
+ return `${minor < 0 ? "-" : ""}${integer}${fraction}`;
140
+ }
141
+ /**
142
+ * Formats an amount while it is being typed, for the value of a text field. Keeps digits and the first `.` as the
143
+ * decimal point, groups thousands with `,`, and cuts the decimals the currency does not have.
144
+ *
145
+ * @throws {RangeError} When the currency is not supported.
146
+ * @example formatMoneyInput('1250.5', 'NIO') // { text: '1,250.5', amount: '1250.50' }
147
+ * @example formatMoneyInput('.', 'NIO') // { text: '0.', amount: '0.00' }
148
+ */
149
+ function formatMoneyInput(text, currencyCode) {
150
+ const target = requireCurrency(currencyCode);
151
+ const typed = typeof text === "string" ? text.slice(0, MAX_INPUT_LENGTH) : "";
152
+ const point = typed.indexOf(".");
153
+ const hasPoint = point >= 0 && target.decimals > 0;
154
+ const integerDigits = (hasPoint ? typed.slice(0, point) : typed).replace(/\D/g, "");
155
+ const fraction = hasPoint ? typed.slice(point + 1).replace(/\D/g, "").slice(0, target.decimals) : "";
156
+ if (!integerDigits && !hasPoint) return {
157
+ text: "",
158
+ amount: null
159
+ };
160
+ const integer = (integerDigits.replace(/^0+(?=\d)/, "") || "0").slice(0, 30);
161
+ return {
162
+ text: `${group(integer, target.groupSeparator)}${hasPoint ? `${target.decimalSeparator}${fraction}` : ""}`,
163
+ amount: target.decimals > 0 ? `${integer}.${fraction.padEnd(target.decimals, "0")}` : integer
164
+ };
165
+ }
166
+ export { MAX_INTEGER_DIGITS, formatMoney, formatMoneyInput, fromMinorUnits, parseMoney, toMinorUnits };
@@ -0,0 +1,11 @@
1
+ // src/phone-examples.json
2
+ var phone_examples_default = {
3
+ GT: "51234567",
4
+ BZ: "6221234",
5
+ SV: "70123456",
6
+ HN: "91234567",
7
+ NI: "81234567",
8
+ CR: "83123456",
9
+ PA: "61234567"
10
+ };
11
+ module.exports = phone_examples_default;
@@ -0,0 +1,11 @@
1
+ // src/phone-examples.json
2
+ var phone_examples_default = {
3
+ GT: "51234567",
4
+ BZ: "6221234",
5
+ SV: "70123456",
6
+ HN: "91234567",
7
+ NI: "81234567",
8
+ CR: "83123456",
9
+ PA: "61234567"
10
+ };
11
+ export { phone_examples_default as default };
@@ -0,0 +1,34 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let libphonenumber_js_core = require("libphonenumber-js/core");
3
+ const require_metadata = require("./metadata.cjs");
4
+ const require_regions = require("./regions.cjs");
5
+ const require_require = require("./require.cjs");
6
+ // src/phone-input.ts
7
+ const MAX_TYPED_DIGITS = 20;
8
+ /**
9
+ * Formats a phone number while it is being typed, for the value of a text field. Keeps only digits and a
10
+ * leading `+`, so pasted text such as `(+506) 8312-3456` becomes `+506 8312 3456`.
11
+ *
12
+ * @param defaultCountry Country used for numbers typed without `+`.
13
+ * @throws {RangeError} When `defaultCountry` is not one of the seven supported countries.
14
+ * @example formatPhoneInput('812345', 'NI') // { text: '8123 45', country: undefined }
15
+ * @example formatPhoneInput('+50683123456', 'NI') // { text: '+506 8312 3456', country: 'CR' }
16
+ */
17
+ function formatPhoneInput(text, defaultCountry) {
18
+ const region = require_require.requireCountry(defaultCountry).code;
19
+ const typed = typeof text === "string" ? text : "";
20
+ const digits = typed.replace(/\D/g, "").slice(0, MAX_TYPED_DIGITS);
21
+ const international = /^[\s(]*\+/.test(typed);
22
+ if (!digits) return {
23
+ text: international ? "+" : "",
24
+ country: void 0
25
+ };
26
+ const formatter = new libphonenumber_js_core.AsYouType(region, require_metadata.metadata);
27
+ const formatted = formatter.input(international ? `+${digits}` : digits);
28
+ const detected = international ? formatter.getCountry() : void 0;
29
+ return {
30
+ text: formatted,
31
+ country: require_regions.isCountryCode(detected) ? detected : void 0
32
+ };
33
+ }
34
+ exports.formatPhoneInput = formatPhoneInput;
@@ -0,0 +1,18 @@
1
+ import { CountryCode } from "./regions.cjs";
2
+ /** What {@link formatPhoneInput} returns. */
3
+ export interface PhoneInputText {
4
+ /** The text to show in the field, formatted as far as it can be, e.g. `8123 45`. */
5
+ readonly text: string;
6
+ /** Country the number belongs to once its calling code is typed (`+506…`), otherwise `undefined`. */
7
+ readonly country: CountryCode | undefined;
8
+ }
9
+ /**
10
+ * Formats a phone number while it is being typed, for the value of a text field. Keeps only digits and a
11
+ * leading `+`, so pasted text such as `(+506) 8312-3456` becomes `+506 8312 3456`.
12
+ *
13
+ * @param defaultCountry Country used for numbers typed without `+`.
14
+ * @throws {RangeError} When `defaultCountry` is not one of the seven supported countries.
15
+ * @example formatPhoneInput('812345', 'NI') // { text: '8123 45', country: undefined }
16
+ * @example formatPhoneInput('+50683123456', 'NI') // { text: '+506 8312 3456', country: 'CR' }
17
+ */
18
+ export declare function formatPhoneInput(text: unknown, defaultCountry: CountryCode): PhoneInputText;
@@ -0,0 +1,18 @@
1
+ import { CountryCode } from "./regions.js";
2
+ /** What {@link formatPhoneInput} returns. */
3
+ export interface PhoneInputText {
4
+ /** The text to show in the field, formatted as far as it can be, e.g. `8123 45`. */
5
+ readonly text: string;
6
+ /** Country the number belongs to once its calling code is typed (`+506…`), otherwise `undefined`. */
7
+ readonly country: CountryCode | undefined;
8
+ }
9
+ /**
10
+ * Formats a phone number while it is being typed, for the value of a text field. Keeps only digits and a
11
+ * leading `+`, so pasted text such as `(+506) 8312-3456` becomes `+506 8312 3456`.
12
+ *
13
+ * @param defaultCountry Country used for numbers typed without `+`.
14
+ * @throws {RangeError} When `defaultCountry` is not one of the seven supported countries.
15
+ * @example formatPhoneInput('812345', 'NI') // { text: '8123 45', country: undefined }
16
+ * @example formatPhoneInput('+50683123456', 'NI') // { text: '+506 8312 3456', country: 'CR' }
17
+ */
18
+ export declare function formatPhoneInput(text: unknown, defaultCountry: CountryCode): PhoneInputText;
@@ -0,0 +1,33 @@
1
+ import { AsYouType } from "libphonenumber-js/core";
2
+ import { metadata } from "./metadata.js";
3
+ import { isCountryCode } from "./regions.js";
4
+ import { requireCountry } from "./require.js";
5
+ // src/phone-input.ts
6
+ const MAX_TYPED_DIGITS = 20;
7
+ /**
8
+ * Formats a phone number while it is being typed, for the value of a text field. Keeps only digits and a
9
+ * leading `+`, so pasted text such as `(+506) 8312-3456` becomes `+506 8312 3456`.
10
+ *
11
+ * @param defaultCountry Country used for numbers typed without `+`.
12
+ * @throws {RangeError} When `defaultCountry` is not one of the seven supported countries.
13
+ * @example formatPhoneInput('812345', 'NI') // { text: '8123 45', country: undefined }
14
+ * @example formatPhoneInput('+50683123456', 'NI') // { text: '+506 8312 3456', country: 'CR' }
15
+ */
16
+ function formatPhoneInput(text, defaultCountry) {
17
+ const region = requireCountry(defaultCountry).code;
18
+ const typed = typeof text === "string" ? text : "";
19
+ const digits = typed.replace(/\D/g, "").slice(0, MAX_TYPED_DIGITS);
20
+ const international = /^[\s(]*\+/.test(typed);
21
+ if (!digits) return {
22
+ text: international ? "+" : "",
23
+ country: void 0
24
+ };
25
+ const formatter = new AsYouType(region, metadata);
26
+ const formatted = formatter.input(international ? `+${digits}` : digits);
27
+ const detected = international ? formatter.getCountry() : void 0;
28
+ return {
29
+ text: formatted,
30
+ country: isCountryCode(detected) ? detected : void 0
31
+ };
32
+ }
33
+ export { formatPhoneInput };
@@ -0,0 +1,224 @@
1
+ // src/phone-metadata.json
2
+ var phone_metadata_default = {
3
+ version: 4,
4
+ country_calling_codes: {
5
+ "501": ["BZ"],
6
+ "502": ["GT"],
7
+ "503": ["SV"],
8
+ "504": ["HN"],
9
+ "505": ["NI"],
10
+ "506": ["CR"],
11
+ "507": ["PA"]
12
+ },
13
+ countries: {
14
+ "GT": [
15
+ "502",
16
+ "00",
17
+ "80\\d{6}|(?:1\\d{3}|[2-7])\\d{7}",
18
+ [8, 11],
19
+ [[
20
+ "(\\d{4})(\\d{4})",
21
+ "$1 $2",
22
+ ["[2-8]"]
23
+ ], [
24
+ "(\\d{4})(\\d{3})(\\d{4})",
25
+ "$1 $2 $3",
26
+ ["1"]
27
+ ]],
28
+ 0,
29
+ 0,
30
+ 0,
31
+ 0,
32
+ 0,
33
+ 0,
34
+ [
35
+ ["[267][2-9]\\d{6}", [8]],
36
+ ["(?:[3-5]\\d\\d|80[0-4])\\d{5}", [8]],
37
+ ["18[01]\\d{8}", [11]],
38
+ ["19\\d{9}", [11]]
39
+ ]
40
+ ],
41
+ "BZ": [
42
+ "501",
43
+ "00",
44
+ "(?:0800\\d|[2-8])\\d{6}",
45
+ [7, 11],
46
+ [[
47
+ "(\\d{3})(\\d{4})",
48
+ "$1-$2",
49
+ ["[2-8]"]
50
+ ], [
51
+ "(\\d)(\\d{3})(\\d{4})(\\d{3})",
52
+ "$1-$2-$3-$4",
53
+ ["0"]
54
+ ]],
55
+ 0,
56
+ 0,
57
+ 0,
58
+ 0,
59
+ 0,
60
+ 0,
61
+ [
62
+ ["(?:2(?:[02]\\d|36|[68]0)|[3-58](?:[02]\\d|[68]0)|7(?:[02]\\d|32|[68]0))\\d{4}", [7]],
63
+ ["6[0-35-7]\\d{5}", [7]],
64
+ ["0800\\d{7}", [11]]
65
+ ]
66
+ ],
67
+ "SV": [
68
+ "503",
69
+ "00",
70
+ "[25-7]\\d{7}|(?:80\\d|900)\\d{4}(?:\\d{4})?",
71
+ [
72
+ 7,
73
+ 8,
74
+ 11
75
+ ],
76
+ [
77
+ [
78
+ "(\\d{3})(\\d{4})",
79
+ "$1 $2",
80
+ ["[89]"]
81
+ ],
82
+ [
83
+ "(\\d{4})(\\d{4})",
84
+ "$1 $2",
85
+ ["[25-7]"]
86
+ ],
87
+ [
88
+ "(\\d{3})(\\d{4})(\\d{4})",
89
+ "$1 $2 $3",
90
+ ["[89]"]
91
+ ]
92
+ ],
93
+ 0,
94
+ 0,
95
+ 0,
96
+ 0,
97
+ 0,
98
+ 0,
99
+ [
100
+ ["2(?:79(?:0[0347-9]|[1-9]\\d)|89(?:0[024589]|[1-9]\\d))\\d{3}|2(?:[1-69]\\d|[78][0-8])\\d{5}", [8]],
101
+ ["[5-7]\\d{7}", [8]],
102
+ ["800\\d{8}|80[01]\\d{4}", [7, 11]],
103
+ ["900\\d{4}(?:\\d{4})?", [7, 11]]
104
+ ]
105
+ ],
106
+ "HN": [
107
+ "504",
108
+ "00",
109
+ "8\\d{10}|[237-9]\\d{7}",
110
+ [8, 11],
111
+ [[
112
+ "(\\d{4})(\\d{4})",
113
+ "$1-$2",
114
+ ["[237-9]"]
115
+ ]],
116
+ 0,
117
+ 0,
118
+ 0,
119
+ 0,
120
+ 0,
121
+ 0,
122
+ [
123
+ ["2(?:2(?:0[0-59]|1[1-9]|[23]\\d|4[02-8]|5[57]|6[2458]|7[0135689]|8[01346-9]|9[0-2])|4(?:0[578]|2[3-59]|3[13-9]|4[0-68]|5[1-3589]|80)|5(?:0[2357-9]|1[1-6]|4[03-58]|5\\d|6[014-69]|7[04]|80)|6(?:[056]\\d|17|2[067]|3[047]|4[0-378]|[78][0-8]|9[01])|7(?:0[5-79]|2[01]|6[46-9]|7[02-9]|8[034]|91)|8(?:79|8[0-357-9]|9[1-57-9]))\\d{4}", [8]],
124
+ ["[37-9]\\d{7}", [8]],
125
+ ["8002\\d{7}", [11]]
126
+ ]
127
+ ],
128
+ "NI": [
129
+ "505",
130
+ "00",
131
+ "(?:1800|[25-8]\\d{3})\\d{4}",
132
+ [8],
133
+ [[
134
+ "(\\d{4})(\\d{4})",
135
+ "$1 $2",
136
+ ["[125-8]"]
137
+ ]],
138
+ 0,
139
+ 0,
140
+ 0,
141
+ 0,
142
+ 0,
143
+ 0,
144
+ [
145
+ ["2\\d{7}"],
146
+ ["(?:5(?:5[0-7]|[78]\\d)|6(?:20|3[035]|4[045]|5[05]|77|8[1-9]|9[059])|(?:7[5-8]|8\\d)\\d)\\d{5}"],
147
+ ["1800\\d{4}"]
148
+ ]
149
+ ],
150
+ "CR": [
151
+ "506",
152
+ "00",
153
+ "(?:8\\d|90)\\d{8}|(?:[24-8]\\d{3}|3005)\\d{4}",
154
+ [8, 10],
155
+ [[
156
+ "(\\d{4})(\\d{4})",
157
+ "$1 $2",
158
+ ["[2-7]|8[3-9]"]
159
+ ], [
160
+ "(\\d{3})(\\d{3})(\\d{4})",
161
+ "$1-$2-$3",
162
+ ["[89]"]
163
+ ]],
164
+ 0,
165
+ 0,
166
+ "(19(?:0[0-2468]|1[09]|20|66|77|99))",
167
+ 0,
168
+ 0,
169
+ 0,
170
+ [
171
+ ["2(?:010|1(?:0[7-9]|[1-9]\\d)|[24-7]\\d\\d)\\d{4}", [8]],
172
+ ["(?:3005\\d|6500[01])\\d{3}|(?:5[07]|6[0-4]|7[0-3]|8[3-9])\\d{6}", [8]],
173
+ ["800\\d{7}", [10]],
174
+ ["90[059]\\d{7}", [10]],
175
+ 0,
176
+ 0,
177
+ 0,
178
+ 0,
179
+ ["(?:2(?:00[0-3]|10[0-6])|4\\d{3}|5100)\\d{4}", [8]]
180
+ ]
181
+ ],
182
+ "PA": [
183
+ "507",
184
+ "00",
185
+ "(?:00800|8\\d{3})\\d{6}|[68]\\d{7}|[1-57-9]\\d{6}",
186
+ [
187
+ 7,
188
+ 8,
189
+ 10,
190
+ 11
191
+ ],
192
+ [
193
+ [
194
+ "(\\d{3})(\\d{4})",
195
+ "$1-$2",
196
+ ["[1-57-9]"]
197
+ ],
198
+ [
199
+ "(\\d{4})(\\d{4})",
200
+ "$1-$2",
201
+ ["[68]"]
202
+ ],
203
+ [
204
+ "(\\d{3})(\\d{3})(\\d{4})",
205
+ "$1 $2 $3",
206
+ ["8"]
207
+ ]
208
+ ],
209
+ 0,
210
+ 0,
211
+ 0,
212
+ 0,
213
+ 0,
214
+ 0,
215
+ [
216
+ ["(?:1(?:0\\d|1[0479]|2[37]|3[0137]|4[147]|5[05]|6[058]|7[0167]|8[2358]|9[1389])|2(?:[0235-79]\\d|1[0-7]|4[013-9]|8[02-9])|3(?:[0147-9]\\d|[25][0-5]|33|6[068])|4(?:00|3[0-79]|4\\d|7[0-57-9])|5(?:[01]\\d|2[0-7]|[56]0|79)|7(?:0[09]|2[0-26-8]|3[03]|4[04]|5[05-9]|6[0156]|7[0-24-9]|8[4-9]|90)|8(?:09|2[89]|3\\d|4[0-24-689]|5[014]|8[02])|9(?:0[5-9]|1[0-35-8]|2[036-9]|3[35-79]|40|5[0457-9]|6[05-9]|7[04-9]|8[235-8]|9\\d))\\d{4}", [7]],
217
+ ["(?:1[16]1|21[89]|6\\d{3}|8(?:1[01]|7[23]))\\d{4}", [7, 8]],
218
+ ["800\\d{4,5}|(?:00800|800\\d)\\d{6}"],
219
+ ["(?:8(?:22|55|60|7[78]|86)|9(?:00|81))\\d{4}", [7]]
220
+ ]
221
+ ]
222
+ }
223
+ };
224
+ module.exports = phone_metadata_default;