@momotombodevs/istmo 0.0.0-stage → 0.1.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/CHANGELOG.md +19 -0
- package/LICENSE +21 -0
- package/README.md +442 -2
- package/dist/dates.cjs +314 -0
- package/dist/dates.d.cts +127 -0
- package/dist/dates.d.ts +127 -0
- package/dist/dates.js +302 -0
- package/dist/formatted-input.cjs +51 -0
- package/dist/formatted-input.js +49 -0
- package/dist/index.cjs +38 -0
- package/dist/index.d.cts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/metadata.cjs +3 -0
- package/dist/metadata.js +4 -0
- package/dist/money.cjs +172 -0
- package/dist/money.d.cts +69 -0
- package/dist/money.d.ts +69 -0
- package/dist/money.js +166 -0
- package/dist/phone-examples.json.cjs +11 -0
- package/dist/phone-examples.json.js +11 -0
- package/dist/phone-input.cjs +34 -0
- package/dist/phone-input.d.cts +18 -0
- package/dist/phone-input.d.ts +18 -0
- package/dist/phone-input.js +33 -0
- package/dist/phone-metadata.json.cjs +224 -0
- package/dist/phone-metadata.json.js +224 -0
- package/dist/phone.cjs +121 -0
- package/dist/phone.d.cts +78 -0
- package/dist/phone.d.ts +78 -0
- package/dist/phone.js +117 -0
- package/dist/react.cjs +238 -0
- package/dist/react.d.cts +128 -0
- package/dist/react.d.ts +128 -0
- package/dist/react.js +232 -0
- package/dist/regions.cjs +94 -0
- package/dist/regions.d.cts +77 -0
- package/dist/regions.d.ts +77 -0
- package/dist/regions.js +85 -0
- package/dist/regions.json.cjs +136 -0
- package/dist/regions.json.js +125 -0
- package/dist/require.cjs +14 -0
- package/dist/require.js +13 -0
- package/flags/LICENSE +21 -0
- package/flags/bz.svg +145 -0
- package/flags/cr.svg +7 -0
- package/flags/gt.svg +204 -0
- package/flags/hn.svg +18 -0
- package/flags/ni.svg +129 -0
- package/flags/pa.svg +7 -0
- package/flags/sv.svg +593 -0
- package/package.json +201 -4
|
@@ -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
|
+
export { phone_metadata_default as default };
|
package/dist/phone.cjs
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
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_phone_examples = require("./phone-examples.json.cjs");
|
|
5
|
+
const require_regions = require("./regions.cjs");
|
|
6
|
+
const require_require = require("./require.cjs");
|
|
7
|
+
// src/phone.ts
|
|
8
|
+
const TYPES = {
|
|
9
|
+
MOBILE: "mobile",
|
|
10
|
+
FIXED_LINE: "fixed",
|
|
11
|
+
FIXED_LINE_OR_MOBILE: "fixed_or_mobile",
|
|
12
|
+
TOLL_FREE: "toll_free",
|
|
13
|
+
PREMIUM_RATE: "premium",
|
|
14
|
+
VOIP: "voip"
|
|
15
|
+
};
|
|
16
|
+
const MAX_INPUT_LENGTH = 64;
|
|
17
|
+
const SEPARATED_DIGITS = /^\+?(?:\d+|\(\+?\d+\))(?:(?: ?[-./] ?| )(?:\d+|\(\+?\d+\)))*$/;
|
|
18
|
+
function readRules(defaultCountry, options) {
|
|
19
|
+
const { type = "any", countries = require_regions.countryCodes } = options ?? {};
|
|
20
|
+
if (type !== "any" && type !== "mobile") throw new RangeError("Opción type inválida: use \"any\" o \"mobile\".");
|
|
21
|
+
if (!Array.isArray(countries) || !countries.every(require_regions.isCountryCode)) throw new RangeError("Opción countries inválida: use códigos de país en mayúsculas, como [\"NI\", \"CR\"].");
|
|
22
|
+
return {
|
|
23
|
+
region: defaultCountry ? require_require.requireCountry(defaultCountry).code : void 0,
|
|
24
|
+
mobileOnly: type === "mobile",
|
|
25
|
+
allowed: countries
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function fail(error) {
|
|
29
|
+
return {
|
|
30
|
+
valid: false,
|
|
31
|
+
error
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function parse(input, defaultCountry) {
|
|
35
|
+
try {
|
|
36
|
+
return (0, libphonenumber_js_core.parsePhoneNumberWithError)(input, {
|
|
37
|
+
defaultCountry,
|
|
38
|
+
extract: false
|
|
39
|
+
}, require_metadata.metadata);
|
|
40
|
+
} catch (error) {
|
|
41
|
+
if (!(error instanceof libphonenumber_js_core.ParseError)) throw error;
|
|
42
|
+
if (error.message === "TOO_SHORT") return "too_short";
|
|
43
|
+
if (error.message === "TOO_LONG") return "too_long";
|
|
44
|
+
if (error.message !== "INVALID_COUNTRY") return "format";
|
|
45
|
+
return !defaultCountry && !input.trimStart().startsWith("+") ? "missing_country" : "country";
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Validates a phone number and tells why it failed. Use it to show a specific error message.
|
|
50
|
+
*
|
|
51
|
+
* @param input What the person typed: groups of digits separated by one space, `-`, `.` or `/`, an optional
|
|
52
|
+
* leading `+`, and one group in parentheses such as `(+505)`. Any other text is rejected as `format`.
|
|
53
|
+
* @param defaultCountry Country used for numbers typed without `+`, as an exact uppercase code.
|
|
54
|
+
* @throws {RangeError} When `defaultCountry` or an option is not valid. These are programming errors, never caused by the input.
|
|
55
|
+
* @example validatePhone('8123 4567', 'NI') // { valid: true, phone: { e164: '+50581234567', ... } }
|
|
56
|
+
* @example validatePhone('1234', 'NI') // { valid: false, error: 'too_short' }
|
|
57
|
+
*/
|
|
58
|
+
function validatePhone(input, defaultCountry, options) {
|
|
59
|
+
const { region, mobileOnly, allowed } = readRules(defaultCountry, options);
|
|
60
|
+
if (typeof input !== "string" || input.length > MAX_INPUT_LENGTH) return fail("format");
|
|
61
|
+
const text = input.replace(/[\s\u00a0]+/g, " ").trim();
|
|
62
|
+
if (!SEPARATED_DIGITS.test(text)) return fail("format");
|
|
63
|
+
const parsed = parse(text.replace(/[()]/g, ""), region);
|
|
64
|
+
if (typeof parsed === "string") return fail(parsed);
|
|
65
|
+
if (!require_regions.isCountryCode(parsed.country) || !allowed.includes(parsed.country)) return fail("country");
|
|
66
|
+
const length = (0, libphonenumber_js_core.validatePhoneNumberLength)(parsed.number, require_metadata.metadata);
|
|
67
|
+
if (length === "TOO_SHORT") return fail("too_short");
|
|
68
|
+
if (length === "TOO_LONG") return fail("too_long");
|
|
69
|
+
if (length !== void 0 || !parsed.isValid()) return fail("invalid");
|
|
70
|
+
const type = TYPES[parsed.getType() ?? ""] ?? "other";
|
|
71
|
+
if (mobileOnly && type !== "mobile" && type !== "fixed_or_mobile") return fail("type");
|
|
72
|
+
const national = parsed.formatNational();
|
|
73
|
+
return {
|
|
74
|
+
valid: true,
|
|
75
|
+
phone: {
|
|
76
|
+
country: parsed.country,
|
|
77
|
+
callingCode: parsed.countryCallingCode,
|
|
78
|
+
nationalNumber: parsed.nationalNumber,
|
|
79
|
+
e164: parsed.number,
|
|
80
|
+
national,
|
|
81
|
+
international: `+${parsed.countryCallingCode} ${national}`,
|
|
82
|
+
type
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Parses a phone number. Same rules as {@link validatePhone}.
|
|
88
|
+
*
|
|
89
|
+
* @returns The number in every format, or `null` when it is not valid.
|
|
90
|
+
* @throws {RangeError} When `defaultCountry` or an option is not valid.
|
|
91
|
+
* @example parsePhone('8123 4567', 'NI')?.e164 // "+50581234567"
|
|
92
|
+
*/
|
|
93
|
+
function parsePhone(input, defaultCountry, options) {
|
|
94
|
+
const result = validatePhone(input, defaultCountry, options);
|
|
95
|
+
return result.valid ? result.phone : null;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Tells whether a phone number is valid. Same rules as {@link validatePhone}.
|
|
99
|
+
*
|
|
100
|
+
* @throws {RangeError} When `defaultCountry` or an option is not valid.
|
|
101
|
+
* @example isValidPhone('2222 2222', 'NI', { type: 'mobile' }) // false, it is a landline
|
|
102
|
+
*/
|
|
103
|
+
function isValidPhone(input, defaultCountry, options) {
|
|
104
|
+
return validatePhone(input, defaultCountry, options).valid;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* An example mobile number of a country, for a placeholder.
|
|
108
|
+
*
|
|
109
|
+
* @throws {RangeError} When the country is not supported.
|
|
110
|
+
* @example phoneExample('NI').national // "8123 4567"
|
|
111
|
+
*/
|
|
112
|
+
function phoneExample(countryCode) {
|
|
113
|
+
const { code } = require_require.requireCountry(countryCode);
|
|
114
|
+
const result = validatePhone(require_phone_examples[code], code);
|
|
115
|
+
if (!result.valid) throw new RangeError("Falta el número de ejemplo del país.");
|
|
116
|
+
return result.phone;
|
|
117
|
+
}
|
|
118
|
+
exports.isValidPhone = isValidPhone;
|
|
119
|
+
exports.parsePhone = parsePhone;
|
|
120
|
+
exports.phoneExample = phoneExample;
|
|
121
|
+
exports.validatePhone = validatePhone;
|
package/dist/phone.d.cts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { CountryCode } from "./regions.cjs";
|
|
2
|
+
/**
|
|
3
|
+
* Kind of line. `fixed_or_mobile` is used where the numbering plan cannot tell them apart.
|
|
4
|
+
* Pager, personal, shared-cost and similar rare kinds are reported as `other`.
|
|
5
|
+
*/
|
|
6
|
+
export type PhoneType = "mobile" | "fixed" | "fixed_or_mobile" | "toll_free" | "premium" | "voip" | "other";
|
|
7
|
+
/**
|
|
8
|
+
* Why a phone number was rejected.
|
|
9
|
+
*
|
|
10
|
+
* - `format`: not a phone number: no digits, letters, stray punctuation, or an extension.
|
|
11
|
+
* - `missing_country`: a local number without `+` and no `defaultCountry` to read it with.
|
|
12
|
+
* - `too_short` / `too_long`: wrong number of digits for the country.
|
|
13
|
+
* - `country`: the number belongs to a country outside Central America or outside `options.countries`.
|
|
14
|
+
* - `invalid`: right length, but not a number that exists in the country's numbering plan.
|
|
15
|
+
* - `type`: `options.type` is `mobile` and the number is a landline.
|
|
16
|
+
*/
|
|
17
|
+
export type PhoneError = "format" | "missing_country" | "too_short" | "too_long" | "country" | "invalid" | "type";
|
|
18
|
+
export interface PhoneOptions {
|
|
19
|
+
/** `mobile` accepts only numbers that can receive SMS or WhatsApp (`mobile` or `fixed_or_mobile`). Default `any`. */
|
|
20
|
+
readonly type?: "any" | "mobile";
|
|
21
|
+
/** Restricts the accepted countries. Default: all seven. */
|
|
22
|
+
readonly countries?: readonly CountryCode[];
|
|
23
|
+
}
|
|
24
|
+
/** A valid phone number in every format the UI and the server need. */
|
|
25
|
+
export interface Phone {
|
|
26
|
+
readonly country: CountryCode;
|
|
27
|
+
/** Calling code without `+`, e.g. `505`. */
|
|
28
|
+
readonly callingCode: string;
|
|
29
|
+
/** Digits after the calling code, e.g. `81234567`. */
|
|
30
|
+
readonly nationalNumber: string;
|
|
31
|
+
/** E.164, e.g. `+50581234567`. Store and send this one. */
|
|
32
|
+
readonly e164: string;
|
|
33
|
+
/** For display inside the country, e.g. `8123 4567`. */
|
|
34
|
+
readonly national: string;
|
|
35
|
+
/** For display anywhere, e.g. `+505 8123 4567`. */
|
|
36
|
+
readonly international: string;
|
|
37
|
+
readonly type: PhoneType;
|
|
38
|
+
}
|
|
39
|
+
export type PhoneValidation = {
|
|
40
|
+
readonly valid: true;
|
|
41
|
+
readonly phone: Phone;
|
|
42
|
+
} | {
|
|
43
|
+
readonly valid: false;
|
|
44
|
+
readonly error: PhoneError;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Validates a phone number and tells why it failed. Use it to show a specific error message.
|
|
48
|
+
*
|
|
49
|
+
* @param input What the person typed: groups of digits separated by one space, `-`, `.` or `/`, an optional
|
|
50
|
+
* leading `+`, and one group in parentheses such as `(+505)`. Any other text is rejected as `format`.
|
|
51
|
+
* @param defaultCountry Country used for numbers typed without `+`, as an exact uppercase code.
|
|
52
|
+
* @throws {RangeError} When `defaultCountry` or an option is not valid. These are programming errors, never caused by the input.
|
|
53
|
+
* @example validatePhone('8123 4567', 'NI') // { valid: true, phone: { e164: '+50581234567', ... } }
|
|
54
|
+
* @example validatePhone('1234', 'NI') // { valid: false, error: 'too_short' }
|
|
55
|
+
*/
|
|
56
|
+
export declare function validatePhone(input: unknown, defaultCountry?: CountryCode | null, options?: PhoneOptions | null): PhoneValidation;
|
|
57
|
+
/**
|
|
58
|
+
* Parses a phone number. Same rules as {@link validatePhone}.
|
|
59
|
+
*
|
|
60
|
+
* @returns The number in every format, or `null` when it is not valid.
|
|
61
|
+
* @throws {RangeError} When `defaultCountry` or an option is not valid.
|
|
62
|
+
* @example parsePhone('8123 4567', 'NI')?.e164 // "+50581234567"
|
|
63
|
+
*/
|
|
64
|
+
export declare function parsePhone(input: unknown, defaultCountry?: CountryCode | null, options?: PhoneOptions | null): Phone | null;
|
|
65
|
+
/**
|
|
66
|
+
* Tells whether a phone number is valid. Same rules as {@link validatePhone}.
|
|
67
|
+
*
|
|
68
|
+
* @throws {RangeError} When `defaultCountry` or an option is not valid.
|
|
69
|
+
* @example isValidPhone('2222 2222', 'NI', { type: 'mobile' }) // false, it is a landline
|
|
70
|
+
*/
|
|
71
|
+
export declare function isValidPhone(input: unknown, defaultCountry?: CountryCode | null, options?: PhoneOptions | null): boolean;
|
|
72
|
+
/**
|
|
73
|
+
* An example mobile number of a country, for a placeholder.
|
|
74
|
+
*
|
|
75
|
+
* @throws {RangeError} When the country is not supported.
|
|
76
|
+
* @example phoneExample('NI').national // "8123 4567"
|
|
77
|
+
*/
|
|
78
|
+
export declare function phoneExample(countryCode: CountryCode): Phone;
|
package/dist/phone.d.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { CountryCode } from "./regions.js";
|
|
2
|
+
/**
|
|
3
|
+
* Kind of line. `fixed_or_mobile` is used where the numbering plan cannot tell them apart.
|
|
4
|
+
* Pager, personal, shared-cost and similar rare kinds are reported as `other`.
|
|
5
|
+
*/
|
|
6
|
+
export type PhoneType = "mobile" | "fixed" | "fixed_or_mobile" | "toll_free" | "premium" | "voip" | "other";
|
|
7
|
+
/**
|
|
8
|
+
* Why a phone number was rejected.
|
|
9
|
+
*
|
|
10
|
+
* - `format`: not a phone number: no digits, letters, stray punctuation, or an extension.
|
|
11
|
+
* - `missing_country`: a local number without `+` and no `defaultCountry` to read it with.
|
|
12
|
+
* - `too_short` / `too_long`: wrong number of digits for the country.
|
|
13
|
+
* - `country`: the number belongs to a country outside Central America or outside `options.countries`.
|
|
14
|
+
* - `invalid`: right length, but not a number that exists in the country's numbering plan.
|
|
15
|
+
* - `type`: `options.type` is `mobile` and the number is a landline.
|
|
16
|
+
*/
|
|
17
|
+
export type PhoneError = "format" | "missing_country" | "too_short" | "too_long" | "country" | "invalid" | "type";
|
|
18
|
+
export interface PhoneOptions {
|
|
19
|
+
/** `mobile` accepts only numbers that can receive SMS or WhatsApp (`mobile` or `fixed_or_mobile`). Default `any`. */
|
|
20
|
+
readonly type?: "any" | "mobile";
|
|
21
|
+
/** Restricts the accepted countries. Default: all seven. */
|
|
22
|
+
readonly countries?: readonly CountryCode[];
|
|
23
|
+
}
|
|
24
|
+
/** A valid phone number in every format the UI and the server need. */
|
|
25
|
+
export interface Phone {
|
|
26
|
+
readonly country: CountryCode;
|
|
27
|
+
/** Calling code without `+`, e.g. `505`. */
|
|
28
|
+
readonly callingCode: string;
|
|
29
|
+
/** Digits after the calling code, e.g. `81234567`. */
|
|
30
|
+
readonly nationalNumber: string;
|
|
31
|
+
/** E.164, e.g. `+50581234567`. Store and send this one. */
|
|
32
|
+
readonly e164: string;
|
|
33
|
+
/** For display inside the country, e.g. `8123 4567`. */
|
|
34
|
+
readonly national: string;
|
|
35
|
+
/** For display anywhere, e.g. `+505 8123 4567`. */
|
|
36
|
+
readonly international: string;
|
|
37
|
+
readonly type: PhoneType;
|
|
38
|
+
}
|
|
39
|
+
export type PhoneValidation = {
|
|
40
|
+
readonly valid: true;
|
|
41
|
+
readonly phone: Phone;
|
|
42
|
+
} | {
|
|
43
|
+
readonly valid: false;
|
|
44
|
+
readonly error: PhoneError;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Validates a phone number and tells why it failed. Use it to show a specific error message.
|
|
48
|
+
*
|
|
49
|
+
* @param input What the person typed: groups of digits separated by one space, `-`, `.` or `/`, an optional
|
|
50
|
+
* leading `+`, and one group in parentheses such as `(+505)`. Any other text is rejected as `format`.
|
|
51
|
+
* @param defaultCountry Country used for numbers typed without `+`, as an exact uppercase code.
|
|
52
|
+
* @throws {RangeError} When `defaultCountry` or an option is not valid. These are programming errors, never caused by the input.
|
|
53
|
+
* @example validatePhone('8123 4567', 'NI') // { valid: true, phone: { e164: '+50581234567', ... } }
|
|
54
|
+
* @example validatePhone('1234', 'NI') // { valid: false, error: 'too_short' }
|
|
55
|
+
*/
|
|
56
|
+
export declare function validatePhone(input: unknown, defaultCountry?: CountryCode | null, options?: PhoneOptions | null): PhoneValidation;
|
|
57
|
+
/**
|
|
58
|
+
* Parses a phone number. Same rules as {@link validatePhone}.
|
|
59
|
+
*
|
|
60
|
+
* @returns The number in every format, or `null` when it is not valid.
|
|
61
|
+
* @throws {RangeError} When `defaultCountry` or an option is not valid.
|
|
62
|
+
* @example parsePhone('8123 4567', 'NI')?.e164 // "+50581234567"
|
|
63
|
+
*/
|
|
64
|
+
export declare function parsePhone(input: unknown, defaultCountry?: CountryCode | null, options?: PhoneOptions | null): Phone | null;
|
|
65
|
+
/**
|
|
66
|
+
* Tells whether a phone number is valid. Same rules as {@link validatePhone}.
|
|
67
|
+
*
|
|
68
|
+
* @throws {RangeError} When `defaultCountry` or an option is not valid.
|
|
69
|
+
* @example isValidPhone('2222 2222', 'NI', { type: 'mobile' }) // false, it is a landline
|
|
70
|
+
*/
|
|
71
|
+
export declare function isValidPhone(input: unknown, defaultCountry?: CountryCode | null, options?: PhoneOptions | null): boolean;
|
|
72
|
+
/**
|
|
73
|
+
* An example mobile number of a country, for a placeholder.
|
|
74
|
+
*
|
|
75
|
+
* @throws {RangeError} When the country is not supported.
|
|
76
|
+
* @example phoneExample('NI').national // "8123 4567"
|
|
77
|
+
*/
|
|
78
|
+
export declare function phoneExample(countryCode: CountryCode): Phone;
|
package/dist/phone.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { ParseError, parsePhoneNumberWithError, validatePhoneNumberLength } from "libphonenumber-js/core";
|
|
2
|
+
import { metadata } from "./metadata.js";
|
|
3
|
+
import phone_examples_default from "./phone-examples.json.js";
|
|
4
|
+
import { countryCodes, isCountryCode } from "./regions.js";
|
|
5
|
+
import { requireCountry } from "./require.js";
|
|
6
|
+
// src/phone.ts
|
|
7
|
+
const TYPES = {
|
|
8
|
+
MOBILE: "mobile",
|
|
9
|
+
FIXED_LINE: "fixed",
|
|
10
|
+
FIXED_LINE_OR_MOBILE: "fixed_or_mobile",
|
|
11
|
+
TOLL_FREE: "toll_free",
|
|
12
|
+
PREMIUM_RATE: "premium",
|
|
13
|
+
VOIP: "voip"
|
|
14
|
+
};
|
|
15
|
+
const MAX_INPUT_LENGTH = 64;
|
|
16
|
+
const SEPARATED_DIGITS = /^\+?(?:\d+|\(\+?\d+\))(?:(?: ?[-./] ?| )(?:\d+|\(\+?\d+\)))*$/;
|
|
17
|
+
function readRules(defaultCountry, options) {
|
|
18
|
+
const { type = "any", countries = countryCodes } = options ?? {};
|
|
19
|
+
if (type !== "any" && type !== "mobile") throw new RangeError("Opción type inválida: use \"any\" o \"mobile\".");
|
|
20
|
+
if (!Array.isArray(countries) || !countries.every(isCountryCode)) throw new RangeError("Opción countries inválida: use códigos de país en mayúsculas, como [\"NI\", \"CR\"].");
|
|
21
|
+
return {
|
|
22
|
+
region: defaultCountry ? requireCountry(defaultCountry).code : void 0,
|
|
23
|
+
mobileOnly: type === "mobile",
|
|
24
|
+
allowed: countries
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function fail(error) {
|
|
28
|
+
return {
|
|
29
|
+
valid: false,
|
|
30
|
+
error
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function parse(input, defaultCountry) {
|
|
34
|
+
try {
|
|
35
|
+
return parsePhoneNumberWithError(input, {
|
|
36
|
+
defaultCountry,
|
|
37
|
+
extract: false
|
|
38
|
+
}, metadata);
|
|
39
|
+
} catch (error) {
|
|
40
|
+
if (!(error instanceof ParseError)) throw error;
|
|
41
|
+
if (error.message === "TOO_SHORT") return "too_short";
|
|
42
|
+
if (error.message === "TOO_LONG") return "too_long";
|
|
43
|
+
if (error.message !== "INVALID_COUNTRY") return "format";
|
|
44
|
+
return !defaultCountry && !input.trimStart().startsWith("+") ? "missing_country" : "country";
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Validates a phone number and tells why it failed. Use it to show a specific error message.
|
|
49
|
+
*
|
|
50
|
+
* @param input What the person typed: groups of digits separated by one space, `-`, `.` or `/`, an optional
|
|
51
|
+
* leading `+`, and one group in parentheses such as `(+505)`. Any other text is rejected as `format`.
|
|
52
|
+
* @param defaultCountry Country used for numbers typed without `+`, as an exact uppercase code.
|
|
53
|
+
* @throws {RangeError} When `defaultCountry` or an option is not valid. These are programming errors, never caused by the input.
|
|
54
|
+
* @example validatePhone('8123 4567', 'NI') // { valid: true, phone: { e164: '+50581234567', ... } }
|
|
55
|
+
* @example validatePhone('1234', 'NI') // { valid: false, error: 'too_short' }
|
|
56
|
+
*/
|
|
57
|
+
function validatePhone(input, defaultCountry, options) {
|
|
58
|
+
const { region, mobileOnly, allowed } = readRules(defaultCountry, options);
|
|
59
|
+
if (typeof input !== "string" || input.length > MAX_INPUT_LENGTH) return fail("format");
|
|
60
|
+
const text = input.replace(/[\s\u00a0]+/g, " ").trim();
|
|
61
|
+
if (!SEPARATED_DIGITS.test(text)) return fail("format");
|
|
62
|
+
const parsed = parse(text.replace(/[()]/g, ""), region);
|
|
63
|
+
if (typeof parsed === "string") return fail(parsed);
|
|
64
|
+
if (!isCountryCode(parsed.country) || !allowed.includes(parsed.country)) return fail("country");
|
|
65
|
+
const length = validatePhoneNumberLength(parsed.number, metadata);
|
|
66
|
+
if (length === "TOO_SHORT") return fail("too_short");
|
|
67
|
+
if (length === "TOO_LONG") return fail("too_long");
|
|
68
|
+
if (length !== void 0 || !parsed.isValid()) return fail("invalid");
|
|
69
|
+
const type = TYPES[parsed.getType() ?? ""] ?? "other";
|
|
70
|
+
if (mobileOnly && type !== "mobile" && type !== "fixed_or_mobile") return fail("type");
|
|
71
|
+
const national = parsed.formatNational();
|
|
72
|
+
return {
|
|
73
|
+
valid: true,
|
|
74
|
+
phone: {
|
|
75
|
+
country: parsed.country,
|
|
76
|
+
callingCode: parsed.countryCallingCode,
|
|
77
|
+
nationalNumber: parsed.nationalNumber,
|
|
78
|
+
e164: parsed.number,
|
|
79
|
+
national,
|
|
80
|
+
international: `+${parsed.countryCallingCode} ${national}`,
|
|
81
|
+
type
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Parses a phone number. Same rules as {@link validatePhone}.
|
|
87
|
+
*
|
|
88
|
+
* @returns The number in every format, or `null` when it is not valid.
|
|
89
|
+
* @throws {RangeError} When `defaultCountry` or an option is not valid.
|
|
90
|
+
* @example parsePhone('8123 4567', 'NI')?.e164 // "+50581234567"
|
|
91
|
+
*/
|
|
92
|
+
function parsePhone(input, defaultCountry, options) {
|
|
93
|
+
const result = validatePhone(input, defaultCountry, options);
|
|
94
|
+
return result.valid ? result.phone : null;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Tells whether a phone number is valid. Same rules as {@link validatePhone}.
|
|
98
|
+
*
|
|
99
|
+
* @throws {RangeError} When `defaultCountry` or an option is not valid.
|
|
100
|
+
* @example isValidPhone('2222 2222', 'NI', { type: 'mobile' }) // false, it is a landline
|
|
101
|
+
*/
|
|
102
|
+
function isValidPhone(input, defaultCountry, options) {
|
|
103
|
+
return validatePhone(input, defaultCountry, options).valid;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* An example mobile number of a country, for a placeholder.
|
|
107
|
+
*
|
|
108
|
+
* @throws {RangeError} When the country is not supported.
|
|
109
|
+
* @example phoneExample('NI').national // "8123 4567"
|
|
110
|
+
*/
|
|
111
|
+
function phoneExample(countryCode) {
|
|
112
|
+
const { code } = requireCountry(countryCode);
|
|
113
|
+
const result = validatePhone(phone_examples_default[code], code);
|
|
114
|
+
if (!result.valid) throw new RangeError("Falta el número de ejemplo del país.");
|
|
115
|
+
return result.phone;
|
|
116
|
+
}
|
|
117
|
+
export { isValidPhone, parsePhone, phoneExample, validatePhone };
|