@wow-two-beta/fe 0.0.38 → 0.0.39
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/foundation/phone/PhoneCountries.d.ts +13 -0
- package/dist/foundation/phone/PhoneCountries.js +62 -0
- package/dist/foundation/phone/PhoneCountries.js.map +1 -0
- package/dist/foundation/phone/PhoneFormats.d.ts +12 -0
- package/dist/foundation/phone/PhoneFormats.js +32 -0
- package/dist/foundation/phone/PhoneFormats.js.map +1 -0
- package/dist/foundation/phone/PhoneNumberExtensions.d.ts +94 -0
- package/dist/foundation/phone/PhoneNumberExtensions.js +137 -0
- package/dist/foundation/phone/PhoneNumberExtensions.js.map +1 -0
- package/dist/foundation/phone/index.d.ts +3 -0
- package/dist/foundation/phone/index.js +9 -0
- package/dist/foundation/phone/index.js.map +1 -0
- package/dist/foundation/validators/Formats.d.ts +15 -0
- package/dist/foundation/validators/Formats.js +32 -1
- package/dist/foundation/validators/Formats.js.map +1 -1
- package/dist/foundation/validators/Messages.d.ts +1 -1
- package/dist/foundation/validators/Messages.js +10 -0
- package/dist/foundation/validators/Messages.js.map +1 -1
- package/dist/foundation/validators/index.d.ts +1 -1
- package/dist/foundation/validators/index.js +2 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/presentation/actions/refreshButton/RefreshButton.vue.js.map +1 -1
- package/dist/presentation/forms/field/Field.vue.js +10 -3
- package/dist/presentation/forms/field/Field.vue.js.map +1 -1
- package/dist/presentation/forms/index.js +2 -1
- package/dist/presentation/forms/index.js.map +1 -1
- package/dist/presentation/forms/labeledField/LabeledField.vue.js +2 -2
- package/dist/presentation/forms/labeledField/LabeledField.vue.js.map +1 -1
- package/dist/presentation/forms/phoneInput/PhoneInput.vue.d.ts +1 -9
- package/dist/presentation/forms/phoneInput/PhoneInput.vue.js +85 -86
- package/dist/presentation/forms/phoneInput/PhoneInput.vue.js.map +1 -1
- package/dist/presentation/forms/phoneInput/PhoneInput2.vue.js +0 -2
- package/dist/presentation/forms/phoneInput/PhoneInput2.vue.js.map +1 -1
- package/dist/presentation/forms/phoneInput/index.d.ts +2 -1
- package/dist/presentation/layout/navbar/Navbar.vue.js.map +1 -1
- package/package.json +6 -1
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Represents one selectable dialling country. */
|
|
2
|
+
export interface PhoneCountry {
|
|
3
|
+
/** The ISO 3166-1 alpha-2 code. */
|
|
4
|
+
readonly iso: string;
|
|
5
|
+
/** The English display name. */
|
|
6
|
+
readonly name: string;
|
|
7
|
+
/** The E.164 country calling code, leading `+` included. */
|
|
8
|
+
readonly dial: string;
|
|
9
|
+
/** The flag emoji. */
|
|
10
|
+
readonly flag: string;
|
|
11
|
+
}
|
|
12
|
+
/** Lists the known dialling countries in display order. */
|
|
13
|
+
export declare const PhoneCountries: ReadonlyArray<PhoneCountry>;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const PhoneCountries = Object.freeze([
|
|
2
|
+
{ iso: "US", name: "United States", dial: "+1", flag: "🇺🇸" },
|
|
3
|
+
{ iso: "CA", name: "Canada", dial: "+1", flag: "🇨🇦" },
|
|
4
|
+
{ iso: "GB", name: "United Kingdom", dial: "+44", flag: "🇬🇧" },
|
|
5
|
+
{ iso: "AU", name: "Australia", dial: "+61", flag: "🇦🇺" },
|
|
6
|
+
{ iso: "DE", name: "Germany", dial: "+49", flag: "🇩🇪" },
|
|
7
|
+
{ iso: "FR", name: "France", dial: "+33", flag: "🇫🇷" },
|
|
8
|
+
{ iso: "ES", name: "Spain", dial: "+34", flag: "🇪🇸" },
|
|
9
|
+
{ iso: "IT", name: "Italy", dial: "+39", flag: "🇮🇹" },
|
|
10
|
+
{ iso: "NL", name: "Netherlands", dial: "+31", flag: "🇳🇱" },
|
|
11
|
+
{ iso: "BE", name: "Belgium", dial: "+32", flag: "🇧🇪" },
|
|
12
|
+
{ iso: "CH", name: "Switzerland", dial: "+41", flag: "🇨ðŸ‡" },
|
|
13
|
+
{ iso: "AT", name: "Austria", dial: "+43", flag: "🇦🇹" },
|
|
14
|
+
{ iso: "SE", name: "Sweden", dial: "+46", flag: "🇸🇪" },
|
|
15
|
+
{ iso: "NO", name: "Norway", dial: "+47", flag: "🇳🇴" },
|
|
16
|
+
{ iso: "DK", name: "Denmark", dial: "+45", flag: "🇩🇰" },
|
|
17
|
+
{ iso: "FI", name: "Finland", dial: "+358", flag: "🇫🇮" },
|
|
18
|
+
{ iso: "IE", name: "Ireland", dial: "+353", flag: "🇮🇪" },
|
|
19
|
+
{ iso: "PT", name: "Portugal", dial: "+351", flag: "🇵🇹" },
|
|
20
|
+
{ iso: "PL", name: "Poland", dial: "+48", flag: "🇵🇱" },
|
|
21
|
+
{ iso: "CZ", name: "Czechia", dial: "+420", flag: "🇨🇿" },
|
|
22
|
+
{ iso: "GR", name: "Greece", dial: "+30", flag: "🇬🇷" },
|
|
23
|
+
{ iso: "TR", name: "Türkiye", dial: "+90", flag: "🇹🇷" },
|
|
24
|
+
{ iso: "RU", name: "Russia", dial: "+7", flag: "🇷🇺" },
|
|
25
|
+
{ iso: "UA", name: "Ukraine", dial: "+380", flag: "🇺🇦" },
|
|
26
|
+
{ iso: "UZ", name: "Uzbekistan", dial: "+998", flag: "🇺🇿" },
|
|
27
|
+
{ iso: "KZ", name: "Kazakhstan", dial: "+7", flag: "🇰🇿" },
|
|
28
|
+
{ iso: "KG", name: "Kyrgyzstan", dial: "+996", flag: "🇰🇬" },
|
|
29
|
+
{ iso: "TJ", name: "Tajikistan", dial: "+992", flag: "🇹🇯" },
|
|
30
|
+
{ iso: "TM", name: "Turkmenistan", dial: "+993", flag: "🇹🇲" },
|
|
31
|
+
{ iso: "IL", name: "Israel", dial: "+972", flag: "🇮🇱" },
|
|
32
|
+
{ iso: "AE", name: "UAE", dial: "+971", flag: "🇦🇪" },
|
|
33
|
+
{ iso: "SA", name: "Saudi Arabia", dial: "+966", flag: "🇸🇦" },
|
|
34
|
+
{ iso: "EG", name: "Egypt", dial: "+20", flag: "🇪🇬" },
|
|
35
|
+
{ iso: "ZA", name: "South Africa", dial: "+27", flag: "🇿🇦" },
|
|
36
|
+
{ iso: "NG", name: "Nigeria", dial: "+234", flag: "🇳🇬" },
|
|
37
|
+
{ iso: "KE", name: "Kenya", dial: "+254", flag: "🇰🇪" },
|
|
38
|
+
{ iso: "IN", name: "India", dial: "+91", flag: "🇮🇳" },
|
|
39
|
+
{ iso: "PK", name: "Pakistan", dial: "+92", flag: "🇵🇰" },
|
|
40
|
+
{ iso: "BD", name: "Bangladesh", dial: "+880", flag: "🇧🇩" },
|
|
41
|
+
{ iso: "SG", name: "Singapore", dial: "+65", flag: "🇸🇬" },
|
|
42
|
+
{ iso: "MY", name: "Malaysia", dial: "+60", flag: "🇲🇾" },
|
|
43
|
+
{ iso: "TH", name: "Thailand", dial: "+66", flag: "🇹ðŸ‡" },
|
|
44
|
+
{ iso: "VN", name: "Vietnam", dial: "+84", flag: "🇻🇳" },
|
|
45
|
+
{ iso: "PH", name: "Philippines", dial: "+63", flag: "🇵ðŸ‡" },
|
|
46
|
+
{ iso: "ID", name: "Indonesia", dial: "+62", flag: "🇮🇩" },
|
|
47
|
+
{ iso: "JP", name: "Japan", dial: "+81", flag: "🇯🇵" },
|
|
48
|
+
{ iso: "KR", name: "South Korea", dial: "+82", flag: "🇰🇷" },
|
|
49
|
+
{ iso: "CN", name: "China", dial: "+86", flag: "🇨🇳" },
|
|
50
|
+
{ iso: "HK", name: "Hong Kong", dial: "+852", flag: "ðŸ‡ðŸ‡°" },
|
|
51
|
+
{ iso: "TW", name: "Taiwan", dial: "+886", flag: "🇹🇼" },
|
|
52
|
+
{ iso: "NZ", name: "New Zealand", dial: "+64", flag: "🇳🇿" },
|
|
53
|
+
{ iso: "BR", name: "Brazil", dial: "+55", flag: "🇧🇷" },
|
|
54
|
+
{ iso: "AR", name: "Argentina", dial: "+54", flag: "🇦🇷" },
|
|
55
|
+
{ iso: "CL", name: "Chile", dial: "+56", flag: "🇨🇱" },
|
|
56
|
+
{ iso: "CO", name: "Colombia", dial: "+57", flag: "🇨🇴" },
|
|
57
|
+
{ iso: "MX", name: "Mexico", dial: "+52", flag: "🇲🇽" }
|
|
58
|
+
]);
|
|
59
|
+
export {
|
|
60
|
+
PhoneCountries
|
|
61
|
+
};
|
|
62
|
+
//# sourceMappingURL=PhoneCountries.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PhoneCountries.js","sources":["../../../src/foundation/phone/PhoneCountries.ts"],"sourcesContent":["/** Represents one selectable dialling country. */\nexport interface PhoneCountry {\n /** The ISO 3166-1 alpha-2 code. */\n readonly iso: string;\n /** The English display name. */\n readonly name: string;\n /** The E.164 country calling code, leading `+` included. */\n readonly dial: string;\n /** The flag emoji. */\n readonly flag: string;\n}\n\n/*\n * E.164 calling codes are prefix-free, so the longest dial code a value starts with names its country. A shared code\n * (`+1` for the US and Canada, `+7` for Russia and Kazakhstan) reads as the earlier country in this list.\n */\n/** Lists the known dialling countries in display order. */\nexport const PhoneCountries: ReadonlyArray<PhoneCountry> = Object.freeze([\n { iso: 'US', name: 'United States', dial: '+1', flag: '🇺🇸' },\n { iso: 'CA', name: 'Canada', dial: '+1', flag: '🇨🇦' },\n { iso: 'GB', name: 'United Kingdom', dial: '+44', flag: '🇬🇧' },\n { iso: 'AU', name: 'Australia', dial: '+61', flag: '🇦🇺' },\n { iso: 'DE', name: 'Germany', dial: '+49', flag: '🇩🇪' },\n { iso: 'FR', name: 'France', dial: '+33', flag: '🇫🇷' },\n { iso: 'ES', name: 'Spain', dial: '+34', flag: '🇪🇸' },\n { iso: 'IT', name: 'Italy', dial: '+39', flag: '🇮🇹' },\n { iso: 'NL', name: 'Netherlands', dial: '+31', flag: '🇳🇱' },\n { iso: 'BE', name: 'Belgium', dial: '+32', flag: '🇧🇪' },\n { iso: 'CH', name: 'Switzerland', dial: '+41', flag: '🇨ðŸ‡' },\n { iso: 'AT', name: 'Austria', dial: '+43', flag: '🇦🇹' },\n { iso: 'SE', name: 'Sweden', dial: '+46', flag: '🇸🇪' },\n { iso: 'NO', name: 'Norway', dial: '+47', flag: '🇳🇴' },\n { iso: 'DK', name: 'Denmark', dial: '+45', flag: '🇩🇰' },\n { iso: 'FI', name: 'Finland', dial: '+358', flag: '🇫🇮' },\n { iso: 'IE', name: 'Ireland', dial: '+353', flag: '🇮🇪' },\n { iso: 'PT', name: 'Portugal', dial: '+351', flag: '🇵🇹' },\n { iso: 'PL', name: 'Poland', dial: '+48', flag: '🇵🇱' },\n { iso: 'CZ', name: 'Czechia', dial: '+420', flag: '🇨🇿' },\n { iso: 'GR', name: 'Greece', dial: '+30', flag: '🇬🇷' },\n { iso: 'TR', name: 'Türkiye', dial: '+90', flag: '🇹🇷' },\n { iso: 'RU', name: 'Russia', dial: '+7', flag: '🇷🇺' },\n { iso: 'UA', name: 'Ukraine', dial: '+380', flag: '🇺🇦' },\n { iso: 'UZ', name: 'Uzbekistan', dial: '+998', flag: '🇺🇿' },\n { iso: 'KZ', name: 'Kazakhstan', dial: '+7', flag: '🇰🇿' },\n { iso: 'KG', name: 'Kyrgyzstan', dial: '+996', flag: '🇰🇬' },\n { iso: 'TJ', name: 'Tajikistan', dial: '+992', flag: '🇹🇯' },\n { iso: 'TM', name: 'Turkmenistan', dial: '+993', flag: '🇹🇲' },\n { iso: 'IL', name: 'Israel', dial: '+972', flag: '🇮🇱' },\n { iso: 'AE', name: 'UAE', dial: '+971', flag: '🇦🇪' },\n { iso: 'SA', name: 'Saudi Arabia', dial: '+966', flag: '🇸🇦' },\n { iso: 'EG', name: 'Egypt', dial: '+20', flag: '🇪🇬' },\n { iso: 'ZA', name: 'South Africa', dial: '+27', flag: '🇿🇦' },\n { iso: 'NG', name: 'Nigeria', dial: '+234', flag: '🇳🇬' },\n { iso: 'KE', name: 'Kenya', dial: '+254', flag: '🇰🇪' },\n { iso: 'IN', name: 'India', dial: '+91', flag: '🇮🇳' },\n { iso: 'PK', name: 'Pakistan', dial: '+92', flag: '🇵🇰' },\n { iso: 'BD', name: 'Bangladesh', dial: '+880', flag: '🇧🇩' },\n { iso: 'SG', name: 'Singapore', dial: '+65', flag: '🇸🇬' },\n { iso: 'MY', name: 'Malaysia', dial: '+60', flag: '🇲🇾' },\n { iso: 'TH', name: 'Thailand', dial: '+66', flag: '🇹ðŸ‡' },\n { iso: 'VN', name: 'Vietnam', dial: '+84', flag: '🇻🇳' },\n { iso: 'PH', name: 'Philippines', dial: '+63', flag: '🇵ðŸ‡' },\n { iso: 'ID', name: 'Indonesia', dial: '+62', flag: '🇮🇩' },\n { iso: 'JP', name: 'Japan', dial: '+81', flag: '🇯🇵' },\n { iso: 'KR', name: 'South Korea', dial: '+82', flag: '🇰🇷' },\n { iso: 'CN', name: 'China', dial: '+86', flag: '🇨🇳' },\n { iso: 'HK', name: 'Hong Kong', dial: '+852', flag: 'ðŸ‡ðŸ‡°' },\n { iso: 'TW', name: 'Taiwan', dial: '+886', flag: '🇹🇼' },\n { iso: 'NZ', name: 'New Zealand', dial: '+64', flag: '🇳🇿' },\n { iso: 'BR', name: 'Brazil', dial: '+55', flag: '🇧🇷' },\n { iso: 'AR', name: 'Argentina', dial: '+54', flag: '🇦🇷' },\n { iso: 'CL', name: 'Chile', dial: '+56', flag: '🇨🇱' },\n { iso: 'CO', name: 'Colombia', dial: '+57', flag: '🇨🇴' },\n { iso: 'MX', name: 'Mexico', dial: '+52', flag: '🇲🇽' },\n]);\n"],"names":[],"mappings":"AAiBO,MAAM,iBAA8C,OAAO,OAAO;AAAA,EACvE,EAAE,KAAK,MAAM,MAAM,iBAAiB,MAAM,MAAM,MAAM,OAAA;AAAA,EACtD,EAAE,KAAK,MAAM,MAAM,UAAU,MAAM,MAAM,MAAM,OAAA;AAAA,EAC/C,EAAE,KAAK,MAAM,MAAM,kBAAkB,MAAM,OAAO,MAAM,OAAA;AAAA,EACxD,EAAE,KAAK,MAAM,MAAM,aAAa,MAAM,OAAO,MAAM,OAAA;AAAA,EACnD,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AAA;AAAA,EAClD,EAAE,KAAK,MAAM,MAAM,YAAY,MAAM,OAAO,MAAM,OAAA;AAAA,EAClD,EAAE,KAAK,MAAM,MAAM,WAAW,MAAM,OAAO,MAAM,OAAA;AAAA,EACjD,EAAE,KAAK,MAAM,MAAM,eAAe,MAAM,OAAO,MAAM,OAAA;AAAA,EACrD,EAAE,KAAK,MAAM,MAAM,aAAa,MAAM,OAAO,MAAM,OAAA;AAAA,EACnD,EAAE,KAAK,MAAM,MAAM,SAAS,MAAM,OAAO,MAAM,OAAA;AAAA,EAC/C,EAAE,KAAK,MAAM,MAAM,eAAe,MAAM,OAAO,MAAM,OAAA;AAAA,EACrD,EAAE,KAAK,MAAM,MAAM,SAAS,MAAM,OAAO,MAAM,OAAA;AAAA,EAC/C,EAAE,KAAK,MAAM,MAAM,aAAa,MAAM,QAAQ,MAAM,OAAA;AAAA,EACpD,EAAE,KAAK,MAAM,MAAM,UAAU,MAAM,QAAQ,MAAM,OAAA;AAAA,EACjD,EAAE,KAAK,MAAM,MAAM,eAAe,MAAM,OAAO,MAAM,OAAA;AAAA,EACrD,EAAE,KAAK,MAAM,MAAM,UAAU,MAAM,OAAO,MAAM,OAAA;AAAA,EAChD,EAAE,KAAK,MAAM,MAAM,aAAa,MAAM,OAAO,MAAM,OAAA;AAAA,EACnD,EAAE,KAAK,MAAM,MAAM,SAAS,MAAM,OAAO,MAAM,OAAA;AAAA,EAC/C,EAAE,KAAK,MAAM,MAAM,YAAY,MAAM,OAAO,MAAM,OAAA;AAAA,EAClD,EAAE,KAAK,MAAM,MAAM,UAAU,MAAM,OAAO,MAAM,OAAA;AAClD,CAAC;"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Describes how one country's national number displays and how many digits it holds. */
|
|
2
|
+
export interface PhoneFormat {
|
|
3
|
+
/**
|
|
4
|
+
* The display mask: each `X` takes one national digit, and any other character is a separator shown only once a
|
|
5
|
+
* digit follows it (`XX-XXX-XX-XX` shows `99-663`, never `99-663-`).
|
|
6
|
+
*/
|
|
7
|
+
readonly mask: string;
|
|
8
|
+
/** The national significant number's digit count: the dial code and any trunk prefix excluded. */
|
|
9
|
+
readonly length: number;
|
|
10
|
+
}
|
|
11
|
+
/** Maps an ISO 3166-1 alpha-2 code to its national-number format. */
|
|
12
|
+
export declare const PhoneFormats: Readonly<Record<string, PhoneFormat>>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const PhoneFormats = Object.freeze({
|
|
2
|
+
US: { mask: "(XXX) XXX-XXXX", length: 10 },
|
|
3
|
+
CA: { mask: "(XXX) XXX-XXXX", length: 10 },
|
|
4
|
+
AU: { mask: "XXX XXX XXX", length: 9 },
|
|
5
|
+
FR: { mask: "X XX XX XX XX", length: 9 },
|
|
6
|
+
ES: { mask: "XXX XX XX XX", length: 9 },
|
|
7
|
+
CH: { mask: "XX XXX XX XX", length: 9 },
|
|
8
|
+
NO: { mask: "XXX XX XXX", length: 8 },
|
|
9
|
+
DK: { mask: "XX XX XX XX", length: 8 },
|
|
10
|
+
PT: { mask: "XXX XXX XXX", length: 9 },
|
|
11
|
+
PL: { mask: "XXX XXX XXX", length: 9 },
|
|
12
|
+
CZ: { mask: "XXX XXX XXX", length: 9 },
|
|
13
|
+
GR: { mask: "XXX XXX XXXX", length: 10 },
|
|
14
|
+
TR: { mask: "(XXX) XXX XX XX", length: 10 },
|
|
15
|
+
RU: { mask: "(XXX) XXX-XX-XX", length: 10 },
|
|
16
|
+
UA: { mask: "XX XXX XX XX", length: 9 },
|
|
17
|
+
UZ: { mask: "XX-XXX-XX-XX", length: 9 },
|
|
18
|
+
KZ: { mask: "(XXX) XXX-XX-XX", length: 10 },
|
|
19
|
+
KG: { mask: "XXX XXX XXX", length: 9 },
|
|
20
|
+
ZA: { mask: "XX XXX XXXX", length: 9 },
|
|
21
|
+
KE: { mask: "XXX XXX XXX", length: 9 },
|
|
22
|
+
IN: { mask: "XXXXX XXXXX", length: 10 },
|
|
23
|
+
SG: { mask: "XXXX XXXX", length: 8 },
|
|
24
|
+
HK: { mask: "XXXX XXXX", length: 8 },
|
|
25
|
+
CL: { mask: "X XXXX XXXX", length: 9 },
|
|
26
|
+
CO: { mask: "XXX XXX XXXX", length: 10 },
|
|
27
|
+
MX: { mask: "XX XXXX XXXX", length: 10 }
|
|
28
|
+
});
|
|
29
|
+
export {
|
|
30
|
+
PhoneFormats
|
|
31
|
+
};
|
|
32
|
+
//# sourceMappingURL=PhoneFormats.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PhoneFormats.js","sources":["../../../src/foundation/phone/PhoneFormats.ts"],"sourcesContent":["/** Describes how one country's national number displays and how many digits it holds. */\nexport interface PhoneFormat {\n /**\n * The display mask: each `X` takes one national digit, and any other character is a separator shown only once a\n * digit follows it (`XX-XXX-XX-XX` shows `99-663`, never `99-663-`).\n */\n readonly mask: string;\n /** The national significant number's digit count: the dial code and any trunk prefix excluded. */\n readonly length: number;\n}\n\n/*\n * Only fixed-length numbering plans are listed, and only where the length and the common grouping are certain. A\n * country absent here (Germany, the UK, Italy, …) has national numbers of several lengths: it displays plain\n * three-digit groups and validates against the E.164 bound alone.\n */\n/** Maps an ISO 3166-1 alpha-2 code to its national-number format. */\nexport const PhoneFormats: Readonly<Record<string, PhoneFormat>> = Object.freeze({\n US: { mask: '(XXX) XXX-XXXX', length: 10 },\n CA: { mask: '(XXX) XXX-XXXX', length: 10 },\n AU: { mask: 'XXX XXX XXX', length: 9 },\n FR: { mask: 'X XX XX XX XX', length: 9 },\n ES: { mask: 'XXX XX XX XX', length: 9 },\n CH: { mask: 'XX XXX XX XX', length: 9 },\n NO: { mask: 'XXX XX XXX', length: 8 },\n DK: { mask: 'XX XX XX XX', length: 8 },\n PT: { mask: 'XXX XXX XXX', length: 9 },\n PL: { mask: 'XXX XXX XXX', length: 9 },\n CZ: { mask: 'XXX XXX XXX', length: 9 },\n GR: { mask: 'XXX XXX XXXX', length: 10 },\n TR: { mask: '(XXX) XXX XX XX', length: 10 },\n RU: { mask: '(XXX) XXX-XX-XX', length: 10 },\n UA: { mask: 'XX XXX XX XX', length: 9 },\n UZ: { mask: 'XX-XXX-XX-XX', length: 9 },\n KZ: { mask: '(XXX) XXX-XX-XX', length: 10 },\n KG: { mask: 'XXX XXX XXX', length: 9 },\n ZA: { mask: 'XX XXX XXXX', length: 9 },\n KE: { mask: 'XXX XXX XXX', length: 9 },\n IN: { mask: 'XXXXX XXXXX', length: 10 },\n SG: { mask: 'XXXX XXXX', length: 8 },\n HK: { mask: 'XXXX XXXX', length: 8 },\n CL: { mask: 'X XXXX XXXX', length: 9 },\n CO: { mask: 'XXX XXX XXXX', length: 10 },\n MX: { mask: 'XX XXXX XXXX', length: 10 },\n});\n"],"names":[],"mappings":"AAiBO,MAAM,eAAsD,OAAO,OAAO;AAAA,EAC/E,IAAI,EAAE,MAAM,kBAAkB,QAAQ,GAAA;AAAA,EACtC,IAAI,EAAE,MAAM,kBAAkB,QAAQ,GAAA;AAAA,EACtC,IAAI,EAAE,MAAM,eAAe,QAAQ,EAAA;AAAA,EACnC,IAAI,EAAE,MAAM,iBAAiB,QAAQ,EAAA;AAAA,EACrC,IAAI,EAAE,MAAM,gBAAgB,QAAQ,EAAA;AAAA,EACpC,IAAI,EAAE,MAAM,gBAAgB,QAAQ,EAAA;AAAA,EACpC,IAAI,EAAE,MAAM,cAAc,QAAQ,EAAA;AAAA,EAClC,IAAI,EAAE,MAAM,eAAe,QAAQ,EAAA;AAAA,EACnC,IAAI,EAAE,MAAM,eAAe,QAAQ,EAAA;AAAA,EACnC,IAAI,EAAE,MAAM,eAAe,QAAQ,EAAA;AAAA,EACnC,IAAI,EAAE,MAAM,eAAe,QAAQ,EAAA;AAAA,EACnC,IAAI,EAAE,MAAM,gBAAgB,QAAQ,GAAA;AAAA,EACpC,IAAI,EAAE,MAAM,mBAAmB,QAAQ,GAAA;AAAA,EACvC,IAAI,EAAE,MAAM,mBAAmB,QAAQ,GAAA;AAAA,EACvC,IAAI,EAAE,MAAM,gBAAgB,QAAQ,EAAA;AAAA,EACpC,IAAI,EAAE,MAAM,gBAAgB,QAAQ,EAAA;AAAA,EACpC,IAAI,EAAE,MAAM,mBAAmB,QAAQ,GAAA;AAAA,EACvC,IAAI,EAAE,MAAM,eAAe,QAAQ,EAAA;AAAA,EACnC,IAAI,EAAE,MAAM,eAAe,QAAQ,EAAA;AAAA,EACnC,IAAI,EAAE,MAAM,eAAe,QAAQ,EAAA;AAAA,EACnC,IAAI,EAAE,MAAM,eAAe,QAAQ,GAAA;AAAA,EACnC,IAAI,EAAE,MAAM,aAAa,QAAQ,EAAA;AAAA,EACjC,IAAI,EAAE,MAAM,aAAa,QAAQ,EAAA;AAAA,EACjC,IAAI,EAAE,MAAM,eAAe,QAAQ,EAAA;AAAA,EACnC,IAAI,EAAE,MAAM,gBAAgB,QAAQ,GAAA;AAAA,EACpC,IAAI,EAAE,MAAM,gBAAgB,QAAQ,GAAA;AACtC,CAAC;"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { PhoneCountry } from './PhoneCountries';
|
|
2
|
+
import { PhoneFormat } from './PhoneFormats';
|
|
3
|
+
/** Holds a value's country and national digits. */
|
|
4
|
+
export interface PhoneNumberParts {
|
|
5
|
+
/** The ISO code the value's dial prefix names, or the fallback when none of the countries matches. */
|
|
6
|
+
readonly iso: string;
|
|
7
|
+
/** The national digits: everything after the matched dial code, or every digit when none matched. */
|
|
8
|
+
readonly national: string;
|
|
9
|
+
}
|
|
10
|
+
/** Holds the national digits a country's numbering plan accepts. */
|
|
11
|
+
export interface PhoneLengthRange {
|
|
12
|
+
/** The fewest national digits. */
|
|
13
|
+
readonly min: number;
|
|
14
|
+
/** The most national digits. Equals `min` for a fixed-length plan. */
|
|
15
|
+
readonly max: number;
|
|
16
|
+
}
|
|
17
|
+
/** Describes one edit of the national-number field, as the browser left it. */
|
|
18
|
+
export interface PhoneNationalEditInput {
|
|
19
|
+
/** The field text after the browser applied the edit, separators and stray characters included. */
|
|
20
|
+
readonly text: string;
|
|
21
|
+
/** The caret offset in `text`. */
|
|
22
|
+
readonly caret: number;
|
|
23
|
+
/** The national digits before the edit. */
|
|
24
|
+
readonly previousDigits: string;
|
|
25
|
+
/** The `InputEvent.inputType`; `deleteContentBackward` / `deleteContentForward` step over a separator. */
|
|
26
|
+
readonly inputType?: string;
|
|
27
|
+
/** The country's format; its `length` caps the digits. */
|
|
28
|
+
readonly format: PhoneFormat;
|
|
29
|
+
}
|
|
30
|
+
/** Holds the result of one national-number edit. */
|
|
31
|
+
export interface PhoneNationalEdit {
|
|
32
|
+
/** The national digits after the edit. */
|
|
33
|
+
readonly digits: string;
|
|
34
|
+
/** The display text: the digits through the mask. */
|
|
35
|
+
readonly text: string;
|
|
36
|
+
/** The caret offset in `text`, after the same digits it followed before the edit. */
|
|
37
|
+
readonly caret: number;
|
|
38
|
+
}
|
|
39
|
+
/** Holds an international number read from pasted or autofilled text. */
|
|
40
|
+
export interface PhoneInternationalNumber {
|
|
41
|
+
/** The allowed country its dial prefix names, or `null` when none does. */
|
|
42
|
+
readonly country: PhoneCountry | null;
|
|
43
|
+
/** The E.164 value: `+` and every digit of the text. */
|
|
44
|
+
readonly e164: string;
|
|
45
|
+
}
|
|
46
|
+
/** Extends E.164 phone values and their national digits: country lookup, length rules, display and editing. */
|
|
47
|
+
export declare const PhoneNumberExtensions: {
|
|
48
|
+
/** @internal E.164 caps a number, dial code included, at 15 digits. */
|
|
49
|
+
readonly MaxDigits: 15;
|
|
50
|
+
/** @internal The fewest national digits a plan outside `PhoneFormats` accepts — Austria's shortest. */
|
|
51
|
+
readonly MinNationalDigits: 4;
|
|
52
|
+
/** @internal The group size of the fallback mask. */
|
|
53
|
+
readonly GroupSize: 3;
|
|
54
|
+
/** @internal The mask character that takes one digit. */
|
|
55
|
+
readonly DigitSlot: "X";
|
|
56
|
+
/** @internal Every non-digit. */
|
|
57
|
+
readonly NonDigit: RegExp;
|
|
58
|
+
/** @internal An E.164 value: `+`, then digits, the first not `0`. */
|
|
59
|
+
readonly E164Pattern: RegExp;
|
|
60
|
+
/** Removes every non-digit. */
|
|
61
|
+
readonly digitsOf: (text: string) => string;
|
|
62
|
+
/**
|
|
63
|
+
* Resolves ISO codes to known countries in the order given; unknown and repeated codes are dropped. A list with no
|
|
64
|
+
* known code reads as absent — every country — since a phone control with no country could hold no value.
|
|
65
|
+
*/
|
|
66
|
+
readonly resolveCountries: (isos: ReadonlyArray<string> | undefined) => ReadonlyArray<PhoneCountry>;
|
|
67
|
+
/** Resolves the national digits a country accepts: its fixed length, else up to the E.164 cap. */
|
|
68
|
+
readonly lengthOf: (country: PhoneCountry) => PhoneLengthRange;
|
|
69
|
+
/** Resolves a country's format: its `PhoneFormats` entry, else three-digit groups up to the E.164 cap. */
|
|
70
|
+
readonly formatOf: (country: PhoneCountry) => PhoneFormat;
|
|
71
|
+
/** Lays national digits into a mask; a separator shows only once a digit follows it, extra digits trail unmasked. */
|
|
72
|
+
readonly formatNational: (digits: string, format: PhoneFormat) => string;
|
|
73
|
+
/** Finds the offset just after the `count`-th digit of `text`; `0` for no digits, the end when it has fewer. */
|
|
74
|
+
readonly caretAfterDigits: (text: string, count: number) => number;
|
|
75
|
+
/**
|
|
76
|
+
* Applies one edit of the national-number field: drops non-digits, steps a deletion over a separator onto the digit
|
|
77
|
+
* beside it, drops typed or pasted digits past the format's length, and keeps the caret after the same digit.
|
|
78
|
+
*/
|
|
79
|
+
readonly editNational: (input: PhoneNationalEditInput) => PhoneNationalEdit;
|
|
80
|
+
/**
|
|
81
|
+
* Finds the country an E.164 value belongs to: `preferredIso` when its dial code matches (a shared `+1` or `+7` stays
|
|
82
|
+
* on the reader's pick), else the longest matching dial code, a shared one going to the earlier country.
|
|
83
|
+
*/
|
|
84
|
+
readonly countryOf: (value: string, countries: ReadonlyArray<PhoneCountry>, preferredIso?: string) => PhoneCountry | null;
|
|
85
|
+
/** Splits an E.164 value into its country and national digits; a value no country matches keeps every digit. */
|
|
86
|
+
readonly split: (value: string, countries: ReadonlyArray<PhoneCountry>, fallbackIso: string) => PhoneNumberParts;
|
|
87
|
+
/** Reads an international number from free text (`+998 99 663-25-82`): every digit after `+`, and its country. */
|
|
88
|
+
readonly parseInternational: (text: string, countries: ReadonlyArray<PhoneCountry>, preferredIso?: string) => PhoneInternationalNumber;
|
|
89
|
+
/**
|
|
90
|
+
* Strips a dial code typed without its `+` (`998996632582` for Uzbekistan): only digits that are exactly the
|
|
91
|
+
* country's dial code and a full fixed-length national number, a length no national number has on its own.
|
|
92
|
+
*/
|
|
93
|
+
readonly withoutDialCode: (digits: string, country: PhoneCountry) => string;
|
|
94
|
+
};
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { PhoneCountries } from "./PhoneCountries.js";
|
|
2
|
+
import { PhoneFormats } from "./PhoneFormats.js";
|
|
3
|
+
const PhoneNumberExtensions = {
|
|
4
|
+
/** @internal E.164 caps a number, dial code included, at 15 digits. */
|
|
5
|
+
MaxDigits: 15,
|
|
6
|
+
/** @internal The fewest national digits a plan outside `PhoneFormats` accepts — Austria's shortest. */
|
|
7
|
+
MinNationalDigits: 4,
|
|
8
|
+
/** @internal The group size of the fallback mask. */
|
|
9
|
+
GroupSize: 3,
|
|
10
|
+
/** @internal The mask character that takes one digit. */
|
|
11
|
+
DigitSlot: "X",
|
|
12
|
+
/** @internal Every non-digit. */
|
|
13
|
+
NonDigit: /\D/g,
|
|
14
|
+
/** @internal An E.164 value: `+`, then digits, the first not `0`. */
|
|
15
|
+
E164Pattern: /^\+[1-9]\d*$/,
|
|
16
|
+
/** Removes every non-digit. */
|
|
17
|
+
digitsOf(text) {
|
|
18
|
+
return text.replace(PhoneNumberExtensions.NonDigit, "");
|
|
19
|
+
},
|
|
20
|
+
/**
|
|
21
|
+
* Resolves ISO codes to known countries in the order given; unknown and repeated codes are dropped. A list with no
|
|
22
|
+
* known code reads as absent — every country — since a phone control with no country could hold no value.
|
|
23
|
+
*/
|
|
24
|
+
resolveCountries(isos) {
|
|
25
|
+
if (!isos) return PhoneCountries;
|
|
26
|
+
const known = [...new Set(isos)].map((iso) => PhoneCountries.find((c) => c.iso === iso)).filter((c) => c !== void 0);
|
|
27
|
+
return known.length > 0 ? known : PhoneCountries;
|
|
28
|
+
},
|
|
29
|
+
/** Resolves the national digits a country accepts: its fixed length, else up to the E.164 cap. */
|
|
30
|
+
lengthOf(country) {
|
|
31
|
+
const format = PhoneFormats[country.iso];
|
|
32
|
+
if (format) return { min: format.length, max: format.length };
|
|
33
|
+
return {
|
|
34
|
+
min: PhoneNumberExtensions.MinNationalDigits,
|
|
35
|
+
max: PhoneNumberExtensions.MaxDigits - PhoneNumberExtensions.digitsOf(country.dial).length
|
|
36
|
+
};
|
|
37
|
+
},
|
|
38
|
+
/** Resolves a country's format: its `PhoneFormats` entry, else three-digit groups up to the E.164 cap. */
|
|
39
|
+
formatOf(country) {
|
|
40
|
+
const format = PhoneFormats[country.iso];
|
|
41
|
+
if (format) return format;
|
|
42
|
+
const length = PhoneNumberExtensions.lengthOf(country).max;
|
|
43
|
+
const slots = Array.from(
|
|
44
|
+
{ length },
|
|
45
|
+
(_, index) => index > 0 && index % PhoneNumberExtensions.GroupSize === 0 ? ` ${PhoneNumberExtensions.DigitSlot}` : PhoneNumberExtensions.DigitSlot
|
|
46
|
+
);
|
|
47
|
+
return { mask: slots.join(""), length };
|
|
48
|
+
},
|
|
49
|
+
/** Lays national digits into a mask; a separator shows only once a digit follows it, extra digits trail unmasked. */
|
|
50
|
+
formatNational(digits, format) {
|
|
51
|
+
let text = "";
|
|
52
|
+
let next = 0;
|
|
53
|
+
for (const slot of format.mask) {
|
|
54
|
+
if (next >= digits.length) break;
|
|
55
|
+
if (slot === PhoneNumberExtensions.DigitSlot) {
|
|
56
|
+
text += digits[next];
|
|
57
|
+
next += 1;
|
|
58
|
+
} else {
|
|
59
|
+
text += slot;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return text + digits.slice(next);
|
|
63
|
+
},
|
|
64
|
+
/** Finds the offset just after the `count`-th digit of `text`; `0` for no digits, the end when it has fewer. */
|
|
65
|
+
caretAfterDigits(text, count) {
|
|
66
|
+
if (count <= 0) return 0;
|
|
67
|
+
let seen = 0;
|
|
68
|
+
for (let index = 0; index < text.length; index++) {
|
|
69
|
+
if (text[index] >= "0" && text[index] <= "9") seen += 1;
|
|
70
|
+
if (seen === count) return index + 1;
|
|
71
|
+
}
|
|
72
|
+
return text.length;
|
|
73
|
+
},
|
|
74
|
+
/**
|
|
75
|
+
* Applies one edit of the national-number field: drops non-digits, steps a deletion over a separator onto the digit
|
|
76
|
+
* beside it, drops typed or pasted digits past the format's length, and keeps the caret after the same digit.
|
|
77
|
+
*/
|
|
78
|
+
editNational(input) {
|
|
79
|
+
const { format, previousDigits } = input;
|
|
80
|
+
let digits = PhoneNumberExtensions.digitsOf(input.text);
|
|
81
|
+
let before = PhoneNumberExtensions.digitsOf(input.text.slice(0, input.caret)).length;
|
|
82
|
+
const previousText = PhoneNumberExtensions.formatNational(previousDigits, format);
|
|
83
|
+
if (digits === previousDigits && previousText.length - input.text.length === 1) {
|
|
84
|
+
if (input.inputType === "deleteContentBackward" && before > 0) {
|
|
85
|
+
digits = digits.slice(0, before - 1) + digits.slice(before);
|
|
86
|
+
before -= 1;
|
|
87
|
+
} else if (input.inputType === "deleteContentForward" && before < digits.length) {
|
|
88
|
+
digits = digits.slice(0, before) + digits.slice(before + 1);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const excess = digits.length - Math.max(format.length, previousDigits.length);
|
|
92
|
+
if (excess > 0) {
|
|
93
|
+
const dropped = Math.min(excess, before);
|
|
94
|
+
digits = digits.slice(0, before - dropped) + digits.slice(before);
|
|
95
|
+
before -= dropped;
|
|
96
|
+
}
|
|
97
|
+
const text = PhoneNumberExtensions.formatNational(digits, format);
|
|
98
|
+
return { digits, text, caret: PhoneNumberExtensions.caretAfterDigits(text, before) };
|
|
99
|
+
},
|
|
100
|
+
/**
|
|
101
|
+
* Finds the country an E.164 value belongs to: `preferredIso` when its dial code matches (a shared `+1` or `+7` stays
|
|
102
|
+
* on the reader's pick), else the longest matching dial code, a shared one going to the earlier country.
|
|
103
|
+
*/
|
|
104
|
+
countryOf(value, countries, preferredIso) {
|
|
105
|
+
if (!value.startsWith("+")) return null;
|
|
106
|
+
const preferred = preferredIso === void 0 ? void 0 : countries.find((c) => c.iso === preferredIso);
|
|
107
|
+
if (preferred && value.startsWith(preferred.dial)) return preferred;
|
|
108
|
+
const longestFirst = [...countries].sort((a, b) => b.dial.length - a.dial.length);
|
|
109
|
+
return longestFirst.find((c) => value.startsWith(c.dial)) ?? null;
|
|
110
|
+
},
|
|
111
|
+
/** Splits an E.164 value into its country and national digits; a value no country matches keeps every digit. */
|
|
112
|
+
split(value, countries, fallbackIso) {
|
|
113
|
+
if (!value) return { iso: fallbackIso, national: "" };
|
|
114
|
+
const country = PhoneNumberExtensions.countryOf(value, countries);
|
|
115
|
+
if (!country) return { iso: fallbackIso, national: PhoneNumberExtensions.digitsOf(value) };
|
|
116
|
+
return { iso: country.iso, national: PhoneNumberExtensions.digitsOf(value.slice(country.dial.length)) };
|
|
117
|
+
},
|
|
118
|
+
/** Reads an international number from free text (`+998 99 663-25-82`): every digit after `+`, and its country. */
|
|
119
|
+
parseInternational(text, countries, preferredIso) {
|
|
120
|
+
const e164 = `+${PhoneNumberExtensions.digitsOf(text)}`;
|
|
121
|
+
return { country: PhoneNumberExtensions.countryOf(e164, countries, preferredIso), e164 };
|
|
122
|
+
},
|
|
123
|
+
/**
|
|
124
|
+
* Strips a dial code typed without its `+` (`998996632582` for Uzbekistan): only digits that are exactly the
|
|
125
|
+
* country's dial code and a full fixed-length national number, a length no national number has on its own.
|
|
126
|
+
*/
|
|
127
|
+
withoutDialCode(digits, country) {
|
|
128
|
+
const format = PhoneFormats[country.iso];
|
|
129
|
+
const dial = PhoneNumberExtensions.digitsOf(country.dial);
|
|
130
|
+
if (!format || digits.length !== dial.length + format.length || !digits.startsWith(dial)) return digits;
|
|
131
|
+
return digits.slice(dial.length);
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
export {
|
|
135
|
+
PhoneNumberExtensions
|
|
136
|
+
};
|
|
137
|
+
//# sourceMappingURL=PhoneNumberExtensions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PhoneNumberExtensions.js","sources":["../../../src/foundation/phone/PhoneNumberExtensions.ts"],"sourcesContent":["import { PhoneCountries, type PhoneCountry } from './PhoneCountries';\nimport { PhoneFormats, type PhoneFormat } from './PhoneFormats';\n\n/** Holds a value's country and national digits. */\nexport interface PhoneNumberParts {\n /** The ISO code the value's dial prefix names, or the fallback when none of the countries matches. */\n readonly iso: string;\n /** The national digits: everything after the matched dial code, or every digit when none matched. */\n readonly national: string;\n}\n\n/** Holds the national digits a country's numbering plan accepts. */\nexport interface PhoneLengthRange {\n /** The fewest national digits. */\n readonly min: number;\n /** The most national digits. Equals `min` for a fixed-length plan. */\n readonly max: number;\n}\n\n/** Describes one edit of the national-number field, as the browser left it. */\nexport interface PhoneNationalEditInput {\n /** The field text after the browser applied the edit, separators and stray characters included. */\n readonly text: string;\n /** The caret offset in `text`. */\n readonly caret: number;\n /** The national digits before the edit. */\n readonly previousDigits: string;\n /** The `InputEvent.inputType`; `deleteContentBackward` / `deleteContentForward` step over a separator. */\n readonly inputType?: string;\n /** The country's format; its `length` caps the digits. */\n readonly format: PhoneFormat;\n}\n\n/** Holds the result of one national-number edit. */\nexport interface PhoneNationalEdit {\n /** The national digits after the edit. */\n readonly digits: string;\n /** The display text: the digits through the mask. */\n readonly text: string;\n /** The caret offset in `text`, after the same digits it followed before the edit. */\n readonly caret: number;\n}\n\n/** Holds an international number read from pasted or autofilled text. */\nexport interface PhoneInternationalNumber {\n /** The allowed country its dial prefix names, or `null` when none does. */\n readonly country: PhoneCountry | null;\n /** The E.164 value: `+` and every digit of the text. */\n readonly e164: string;\n}\n\n/** Extends E.164 phone values and their national digits: country lookup, length rules, display and editing. */\nexport const PhoneNumberExtensions = {\n /** @internal E.164 caps a number, dial code included, at 15 digits. */\n MaxDigits: 15,\n /** @internal The fewest national digits a plan outside `PhoneFormats` accepts — Austria's shortest. */\n MinNationalDigits: 4,\n /** @internal The group size of the fallback mask. */\n GroupSize: 3,\n /** @internal The mask character that takes one digit. */\n DigitSlot: 'X',\n /** @internal Every non-digit. */\n NonDigit: /\\D/g,\n /** @internal An E.164 value: `+`, then digits, the first not `0`. */\n E164Pattern: /^\\+[1-9]\\d*$/,\n\n /** Removes every non-digit. */\n digitsOf(text: string): string {\n return text.replace(PhoneNumberExtensions.NonDigit, '');\n },\n\n /**\n * Resolves ISO codes to known countries in the order given; unknown and repeated codes are dropped. A list with no\n * known code reads as absent — every country — since a phone control with no country could hold no value.\n */\n resolveCountries(isos: ReadonlyArray<string> | undefined): ReadonlyArray<PhoneCountry> {\n if (!isos) return PhoneCountries;\n\n const known = [...new Set(isos)]\n .map((iso) => PhoneCountries.find((c) => c.iso === iso))\n .filter((c): c is PhoneCountry => c !== undefined);\n return known.length > 0 ? known : PhoneCountries;\n },\n\n /** Resolves the national digits a country accepts: its fixed length, else up to the E.164 cap. */\n lengthOf(country: PhoneCountry): PhoneLengthRange {\n const format = PhoneFormats[country.iso];\n if (format) return { min: format.length, max: format.length };\n\n return {\n min: PhoneNumberExtensions.MinNationalDigits,\n max: PhoneNumberExtensions.MaxDigits - PhoneNumberExtensions.digitsOf(country.dial).length,\n };\n },\n\n /** Resolves a country's format: its `PhoneFormats` entry, else three-digit groups up to the E.164 cap. */\n formatOf(country: PhoneCountry): PhoneFormat {\n const format = PhoneFormats[country.iso];\n if (format) return format;\n\n const length = PhoneNumberExtensions.lengthOf(country).max;\n const slots = Array.from({ length }, (_, index) =>\n index > 0 && index % PhoneNumberExtensions.GroupSize === 0\n ? ` ${PhoneNumberExtensions.DigitSlot}`\n : PhoneNumberExtensions.DigitSlot,\n );\n return { mask: slots.join(''), length };\n },\n\n /** Lays national digits into a mask; a separator shows only once a digit follows it, extra digits trail unmasked. */\n formatNational(digits: string, format: PhoneFormat): string {\n let text = '';\n let next = 0;\n for (const slot of format.mask) {\n if (next >= digits.length) break;\n if (slot === PhoneNumberExtensions.DigitSlot) {\n text += digits[next];\n next += 1;\n } else {\n text += slot;\n }\n }\n\n return text + digits.slice(next);\n },\n\n /** Finds the offset just after the `count`-th digit of `text`; `0` for no digits, the end when it has fewer. */\n caretAfterDigits(text: string, count: number): number {\n if (count <= 0) return 0;\n\n let seen = 0;\n for (let index = 0; index < text.length; index++) {\n if (text[index]! >= '0' && text[index]! <= '9') seen += 1;\n if (seen === count) return index + 1;\n }\n return text.length;\n },\n\n /**\n * Applies one edit of the national-number field: drops non-digits, steps a deletion over a separator onto the digit\n * beside it, drops typed or pasted digits past the format's length, and keeps the caret after the same digit.\n */\n editNational(input: PhoneNationalEditInput): PhoneNationalEdit {\n const { format, previousDigits } = input;\n let digits = PhoneNumberExtensions.digitsOf(input.text);\n let before = PhoneNumberExtensions.digitsOf(input.text.slice(0, input.caret)).length;\n\n // A single deleted character that left the digits whole was a separator: the deletion moves on to the digit.\n const previousText = PhoneNumberExtensions.formatNational(previousDigits, format);\n if (digits === previousDigits && previousText.length - input.text.length === 1) {\n if (input.inputType === 'deleteContentBackward' && before > 0) {\n digits = digits.slice(0, before - 1) + digits.slice(before);\n before -= 1;\n } else if (input.inputType === 'deleteContentForward' && before < digits.length) {\n digits = digits.slice(0, before) + digits.slice(before + 1);\n }\n }\n\n // Digits past the cap are the ones just inserted, which sit right before the caret.\n const excess = digits.length - Math.max(format.length, previousDigits.length);\n if (excess > 0) {\n const dropped = Math.min(excess, before);\n digits = digits.slice(0, before - dropped) + digits.slice(before);\n before -= dropped;\n }\n\n const text = PhoneNumberExtensions.formatNational(digits, format);\n return { digits, text, caret: PhoneNumberExtensions.caretAfterDigits(text, before) };\n },\n\n /**\n * Finds the country an E.164 value belongs to: `preferredIso` when its dial code matches (a shared `+1` or `+7` stays\n * on the reader's pick), else the longest matching dial code, a shared one going to the earlier country.\n */\n countryOf(value: string, countries: ReadonlyArray<PhoneCountry>, preferredIso?: string): PhoneCountry | null {\n if (!value.startsWith('+')) return null;\n\n const preferred = preferredIso === undefined ? undefined : countries.find((c) => c.iso === preferredIso);\n if (preferred && value.startsWith(preferred.dial)) return preferred;\n\n // The stable sort leaves countries of one code length in list order.\n const longestFirst = [...countries].sort((a, b) => b.dial.length - a.dial.length);\n return longestFirst.find((c) => value.startsWith(c.dial)) ?? null;\n },\n\n /** Splits an E.164 value into its country and national digits; a value no country matches keeps every digit. */\n split(value: string, countries: ReadonlyArray<PhoneCountry>, fallbackIso: string): PhoneNumberParts {\n if (!value) return { iso: fallbackIso, national: '' };\n\n const country = PhoneNumberExtensions.countryOf(value, countries);\n if (!country) return { iso: fallbackIso, national: PhoneNumberExtensions.digitsOf(value) };\n\n return { iso: country.iso, national: PhoneNumberExtensions.digitsOf(value.slice(country.dial.length)) };\n },\n\n /** Reads an international number from free text (`+998 99 663-25-82`): every digit after `+`, and its country. */\n parseInternational(\n text: string,\n countries: ReadonlyArray<PhoneCountry>,\n preferredIso?: string,\n ): PhoneInternationalNumber {\n const e164 = `+${PhoneNumberExtensions.digitsOf(text)}`;\n return { country: PhoneNumberExtensions.countryOf(e164, countries, preferredIso), e164 };\n },\n\n /**\n * Strips a dial code typed without its `+` (`998996632582` for Uzbekistan): only digits that are exactly the\n * country's dial code and a full fixed-length national number, a length no national number has on its own.\n */\n withoutDialCode(digits: string, country: PhoneCountry): string {\n const format = PhoneFormats[country.iso];\n const dial = PhoneNumberExtensions.digitsOf(country.dial);\n if (!format || digits.length !== dial.length + format.length || !digits.startsWith(dial)) return digits;\n\n return digits.slice(dial.length);\n },\n} as const;\n"],"names":[],"mappings":";;AAoDO,MAAM,wBAAwB;AAAA;AAAA,EAEnC,WAAW;AAAA;AAAA,EAEX,mBAAmB;AAAA;AAAA,EAEnB,WAAW;AAAA;AAAA,EAEX,WAAW;AAAA;AAAA,EAEX,UAAU;AA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,IAAI;AACxD,QAAI,CAAC,UAAU,OAAO,WAAW,KAAK,SAAS,OAAO,UAAU,CAAC,OAAO,WAAW,IAAI,EAAG,QAAO;AAEjG,WAAO,OAAO,MAAM,KAAK,MAAM;AAAA,EACjC;AACF;"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { type PhoneCountry, PhoneCountries } from './PhoneCountries';
|
|
2
|
+
export { type PhoneFormat, PhoneFormats } from './PhoneFormats';
|
|
3
|
+
export { type PhoneNumberParts, type PhoneLengthRange, type PhoneNationalEditInput, type PhoneNationalEdit, type PhoneInternationalNumber, PhoneNumberExtensions, } from './PhoneNumberExtensions';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { PhoneCountries } from "./PhoneCountries.js";
|
|
2
|
+
import { PhoneFormats } from "./PhoneFormats.js";
|
|
3
|
+
import { PhoneNumberExtensions } from "./PhoneNumberExtensions.js";
|
|
4
|
+
export {
|
|
5
|
+
PhoneCountries,
|
|
6
|
+
PhoneFormats,
|
|
7
|
+
PhoneNumberExtensions
|
|
8
|
+
};
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;"}
|
|
@@ -7,3 +7,18 @@ export declare function url(message?: string): StringValidator;
|
|
|
7
7
|
export declare function uuid(message?: string): StringValidator;
|
|
8
8
|
/** Accepts a `YYYY-MM-DD` calendar date that actually exists. Not a full ISO 8601 timestamp. */
|
|
9
9
|
export declare function isoDate(message?: string): StringValidator;
|
|
10
|
+
/** Configures the {@link phone} rule. */
|
|
11
|
+
export interface PhoneRuleOptions {
|
|
12
|
+
/** The ISO codes a number may come from — the phone control's `countries`. Every known country when omitted. */
|
|
13
|
+
readonly countries?: ReadonlyArray<string>;
|
|
14
|
+
/** Accepts the empty string a cleared phone control emits. Default `false`: an empty value fails as `required`. */
|
|
15
|
+
readonly isOptional?: boolean;
|
|
16
|
+
/** Replaces the message of a `phone` failure; the form's catalogue still renders the code first. */
|
|
17
|
+
readonly message?: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Accepts an E.164 phone value whose national number has its country's digit count — `+998` takes 9 digits. Fails
|
|
21
|
+
* with code `phone` and params `{ country, dial, length }` (a fixed-length plan) or `{ country, dial, min, max }`;
|
|
22
|
+
* a value that is not `+` and digits, or whose dial code names none of `countries`, fails with `phone` alone.
|
|
23
|
+
*/
|
|
24
|
+
export declare function phone(options?: PhoneRuleOptions): StringValidator;
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PhoneNumberExtensions } from "../phone/PhoneNumberExtensions.js";
|
|
2
|
+
import { invalid } from "./ValidationOutcome.js";
|
|
3
|
+
import { string, StringValidator } from "./Primitives.js";
|
|
2
4
|
const EmailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
3
5
|
const UuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$(?![\s\S])/i;
|
|
4
6
|
const IsoDatePattern = /^\d{4}-\d{2}-\d{2}$(?![\s\S])/;
|
|
@@ -37,9 +39,38 @@ function isoDate(message = "must be a valid ISO date (YYYY-MM-DD)") {
|
|
|
37
39
|
"isoDate"
|
|
38
40
|
);
|
|
39
41
|
}
|
|
42
|
+
function phone(options = {}) {
|
|
43
|
+
const countries = PhoneNumberExtensions.resolveCountries(options.countries);
|
|
44
|
+
const text = string();
|
|
45
|
+
return new StringValidator("phone", (value, path) => {
|
|
46
|
+
const parsed = text.parseAt(value, path);
|
|
47
|
+
if (!parsed.ok) return parsed;
|
|
48
|
+
const number = parsed.value;
|
|
49
|
+
if (number === "") {
|
|
50
|
+
return options.isOptional ? parsed : invalid([{ path, message: "is required", code: "required" }]);
|
|
51
|
+
}
|
|
52
|
+
const country = PhoneNumberExtensions.E164Pattern.test(number) ? PhoneNumberExtensions.countryOf(number, countries) : null;
|
|
53
|
+
if (!country) {
|
|
54
|
+
return invalid([{ path, message: options.message ?? "must be a valid phone number", code: "phone" }]);
|
|
55
|
+
}
|
|
56
|
+
const digits = number.length - country.dial.length;
|
|
57
|
+
const { min, max } = PhoneNumberExtensions.lengthOf(country);
|
|
58
|
+
if (digits >= min && digits <= max) return parsed;
|
|
59
|
+
const fixed = min === max;
|
|
60
|
+
return invalid([
|
|
61
|
+
{
|
|
62
|
+
path,
|
|
63
|
+
message: options.message ?? (fixed ? `must have ${min} digits after ${country.dial}` : `must have ${min} to ${max} digits after ${country.dial}`),
|
|
64
|
+
code: "phone",
|
|
65
|
+
params: fixed ? { country: country.iso, dial: country.dial, length: min } : { country: country.iso, dial: country.dial, min, max }
|
|
66
|
+
}
|
|
67
|
+
]);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
40
70
|
export {
|
|
41
71
|
email,
|
|
42
72
|
isoDate,
|
|
73
|
+
phone,
|
|
43
74
|
url,
|
|
44
75
|
uuid
|
|
45
76
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Formats.js","sources":["../../../src/foundation/validators/Formats.ts"],"sourcesContent":["// Format validators for the four string shapes that show up at almost every boundary. Each returns a\n// `StringValidator`, so the type check, the format check, and any further refinement compose in one\n// chain: `email().max(254)`.\n//\n// THE REGEXES ARE DELIBERATELY CONSERVATIVE, AND THEY ARE NOT SPECIFICATIONS:\n//\n// - `email()` IS A PRAGMATIC CHECK, NOT RFC 5322. The real grammar admits quoted local parts, comments,\n// and bracketed IP literals; the widely-cited \"RFC-compliant\" regex runs thousands of characters and\n// still accepts addresses no mail server will deliver to. This checks the shape that catches real\n// typos — one `@`, no whitespace, a dotted domain — and nothing more. The only true validation of an\n// email address is sending mail to it, so anything stricter here buys false rejections, not safety.\n// - `url()` uses the `URL` constructor instead of a regex, because a hand-written URL regex is wrong in\n// ways that matter. It accepts ANY scheme, `mailto:` and `javascript:` included — restrict with\n// `.refine()` when the value reaches an `href`.\n// - `uuid()` requires the RFC 4122 layout with a version nibble of 1–8 and a variant nibble of 8/9/a/b.\n// NOTE: this REJECTS the nil UUID (`000…0`), which some systems use as a sentinel — chain a `union`\n// with `literal()` if a nil is meaningful in that field.\n// - `isoDate()` accepts a calendar date (`YYYY-MM-DD`) ONLY, not a full ISO 8601 timestamp, and confirms\n// the date exists by round-tripping it — `2025-02-30` matches the pattern but is not a day. Years are\n// compared after the round trip, including the four-digit years below 0100.\n\nimport { string,
|
|
1
|
+
{"version":3,"file":"Formats.js","sources":["../../../src/foundation/validators/Formats.ts"],"sourcesContent":["// Format validators for the four string shapes that show up at almost every boundary. Each returns a\n// `StringValidator`, so the type check, the format check, and any further refinement compose in one\n// chain: `email().max(254)`.\n//\n// THE REGEXES ARE DELIBERATELY CONSERVATIVE, AND THEY ARE NOT SPECIFICATIONS:\n//\n// - `email()` IS A PRAGMATIC CHECK, NOT RFC 5322. The real grammar admits quoted local parts, comments,\n// and bracketed IP literals; the widely-cited \"RFC-compliant\" regex runs thousands of characters and\n// still accepts addresses no mail server will deliver to. This checks the shape that catches real\n// typos — one `@`, no whitespace, a dotted domain — and nothing more. The only true validation of an\n// email address is sending mail to it, so anything stricter here buys false rejections, not safety.\n// - `url()` uses the `URL` constructor instead of a regex, because a hand-written URL regex is wrong in\n// ways that matter. It accepts ANY scheme, `mailto:` and `javascript:` included — restrict with\n// `.refine()` when the value reaches an `href`.\n// - `uuid()` requires the RFC 4122 layout with a version nibble of 1–8 and a variant nibble of 8/9/a/b.\n// NOTE: this REJECTS the nil UUID (`000…0`), which some systems use as a sentinel — chain a `union`\n// with `literal()` if a nil is meaningful in that field.\n// - `isoDate()` accepts a calendar date (`YYYY-MM-DD`) ONLY, not a full ISO 8601 timestamp, and confirms\n// the date exists by round-tripping it — `2025-02-30` matches the pattern but is not a day. Years are\n// compared after the round trip, including the four-digit years below 0100.\n// - `phone()` checks an E.164 value (`+998996632582`) against its country's national digit count from\n// `foundation/phone`: the exact length for a fixed-length plan, else the E.164 bound (4 to 15 digits less the dial\n// code). It is a length check by numbering plan, not `libphonenumber`: it cannot tell an unassigned number.\n\nimport { PhoneNumberExtensions } from '../phone';\nimport { invalid } from './ValidationOutcome';\nimport { string, StringValidator } from './Primitives';\n\n/** One `@`, no whitespace, and a dotted domain. See the header — pragmatic, not RFC 5322. */\nconst EmailPattern = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/;\n\n/** RFC 4122 layout: 8-4-4-4-12 hex, version nibble 1–8, variant nibble 8/9/a/b. */\nconst UuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$(?![\\s\\S])/i;\n\n/** Calendar date only — four-digit year, two-digit month, two-digit day. */\nconst IsoDatePattern = /^\\d{4}-\\d{2}-\\d{2}$(?![\\s\\S])/;\n\n/** Accepts an email-shaped string. Pragmatic check — see the header for what it deliberately does not do. */\nexport function email(message = 'must be a valid email address'): StringValidator {\n return string().pattern(EmailPattern, message, 'email');\n}\n\n/** Accepts any string the `URL` constructor parses. Any scheme passes — restrict further if it reaches an `href`. */\nexport function url(message = 'must be a valid URL'): StringValidator {\n return string().refine(\n (value) => {\n try {\n // Construct-and-discard: `URL` throws on a malformed value, and the caller keeps their string.\n void new URL(value);\n return true;\n } catch {\n return false;\n }\n },\n message,\n 'url',\n );\n}\n\n/** Accepts a canonical RFC 4122 UUID, any case. Rejects the nil UUID — see the header. */\nexport function uuid(message = 'must be a valid UUID'): StringValidator {\n return string().pattern(UuidPattern, message, 'uuid');\n}\n\n/** Accepts a `YYYY-MM-DD` calendar date that actually exists. Not a full ISO 8601 timestamp. */\nexport function isoDate(message = 'must be a valid ISO date (YYYY-MM-DD)'): StringValidator {\n return string()\n .pattern(IsoDatePattern, message, 'isoDate')\n .refine(\n (value) => {\n const parts = value.split('-');\n const year = Number(parts[0]);\n const month = Number(parts[1]);\n const day = Number(parts[2]);\n\n // Round-trip through UTC: an overflowing day (`2025-02-30`) lands on a different date, so\n // comparing the parts back out is what proves the calendar date is real.\n const parsed = new Date(0);\n parsed.setUTCFullYear(year, month - 1, day);\n return parsed.getUTCFullYear() === year && parsed.getUTCMonth() === month - 1 && parsed.getUTCDate() === day;\n },\n message,\n 'isoDate',\n );\n}\n\n/** Configures the {@link phone} rule. */\nexport interface PhoneRuleOptions {\n /** The ISO codes a number may come from — the phone control's `countries`. Every known country when omitted. */\n readonly countries?: ReadonlyArray<string>;\n /** Accepts the empty string a cleared phone control emits. Default `false`: an empty value fails as `required`. */\n readonly isOptional?: boolean;\n /** Replaces the message of a `phone` failure; the form's catalogue still renders the code first. */\n readonly message?: string;\n}\n\n/**\n * Accepts an E.164 phone value whose national number has its country's digit count — `+998` takes 9 digits. Fails\n * with code `phone` and params `{ country, dial, length }` (a fixed-length plan) or `{ country, dial, min, max }`;\n * a value that is not `+` and digits, or whose dial code names none of `countries`, fails with `phone` alone.\n */\nexport function phone(options: PhoneRuleOptions = {}): StringValidator {\n const countries = PhoneNumberExtensions.resolveCountries(options.countries);\n const text = string();\n\n return new StringValidator('phone', (value, path) => {\n const parsed = text.parseAt(value, path);\n if (!parsed.ok) return parsed;\n\n const number = parsed.value;\n if (number === '') {\n return options.isOptional ? parsed : invalid([{ path, message: 'is required', code: 'required' }]);\n }\n\n const country = PhoneNumberExtensions.E164Pattern.test(number)\n ? PhoneNumberExtensions.countryOf(number, countries)\n : null;\n if (!country) {\n return invalid([{ path, message: options.message ?? 'must be a valid phone number', code: 'phone' }]);\n }\n\n const digits = number.length - country.dial.length;\n const { min, max } = PhoneNumberExtensions.lengthOf(country);\n if (digits >= min && digits <= max) return parsed;\n\n const fixed = min === max;\n return invalid([\n {\n path,\n message:\n options.message ??\n (fixed\n ? `must have ${min} digits after ${country.dial}`\n : `must have ${min} to ${max} digits after ${country.dial}`),\n code: 'phone',\n params: fixed\n ? { country: country.iso, dial: country.dial, length: min }\n : { country: country.iso, dial: country.dial, min, max },\n },\n ]);\n });\n}\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ACtB,QAAI,WAAW,IAAI;AACjB,aAAO,QAAQ,aAAa,SAAS,QAAQ,CAAC,EAAE,MAAM,SAAS,eAAe,MAAM,WAAA,CAAY,CAAC;AAAA,IACnG;AAEA,UAAM,UAAU,sBAAsB,YAAY,KAAK,MAAM,IACzD,sBAAsB,UAAU,QAAQ,SAAS,IACjD;AACJ,QAAI,CAAC,SAAS;AACZ,aAAO,QAAQ,CAAC,EAAE,MAAM,SAAS,QAAQ,WAAW,gCAAgC,MAAM,QAAA,CAAS,CAAC;AAAA,IACtG;AAEA,UAAM,SAAS,OAAO,SAAS,QAAQ,KAAK;AAC5C,UAAM,EAAE,KAAK,IAAA,IAAQ,sBAAsB,SAAS,OAAO;AAC3D,QAAI,UAAU,OAAO,UAAU,IAAK,QAAO;AAE3C,UAAM,QAAQ,QAAQ;AACtB,WAAO,QAAQ;AAAA,MACb;AAAA,QACE;AAAA,QACA,SACE,QAAQ,YACP,QACG,aAAa,GAAG,iBAAiB,QAAQ,IAAI,KAC7C,aAAa,GAAG,OAAO,GAAG,iBAAiB,QAAQ,IAAI;AAAA,QAC7D,MAAM;AAAA,QACN,QAAQ,QACJ,EAAE,SAAS,QAAQ,KAAK,MAAM,QAAQ,MAAM,QAAQ,QACpD,EAAE,SAAS,QAAQ,KAAK,MAAM,QAAQ,MAAM,KAAK,IAAA;AAAA,MAAI;AAAA,IAC3D,CACD;AAAA,EACH,CAAC;AACH;"}
|
|
@@ -12,7 +12,7 @@ export interface FieldIssue {
|
|
|
12
12
|
* `pattern`, …); the rest arrive from the server. Kept as one flat union because a catalogue keys on it
|
|
13
13
|
* and a split would mean two lookups for one concept.
|
|
14
14
|
*/
|
|
15
|
-
export declare const ValidationCodes: readonly ["required", "type", "mustBeNull", "mustBeEmpty", "min", "max", "length", "integer", "precision", "greaterThan", "lessThan", "between", "betweenExclusive", "equal", "notEqual", "literal", "oneOf", "union", "pattern", "email", "url", "uuid", "isoDate", "creditCard", "custom", "transform", "internal"];
|
|
15
|
+
export declare const ValidationCodes: readonly ["required", "type", "mustBeNull", "mustBeEmpty", "min", "max", "length", "integer", "precision", "greaterThan", "lessThan", "between", "betweenExclusive", "equal", "notEqual", "literal", "oneOf", "union", "pattern", "email", "url", "uuid", "isoDate", "creditCard", "phone", "custom", "transform", "internal"];
|
|
16
16
|
/** A member of the shared rule-code vocabulary. */
|
|
17
17
|
export type ValidationCode = (typeof ValidationCodes)[number];
|
|
18
18
|
/**
|
|
@@ -29,6 +29,7 @@ const ValidationCodes = [
|
|
|
29
29
|
"uuid",
|
|
30
30
|
"isoDate",
|
|
31
31
|
"creditCard",
|
|
32
|
+
"phone",
|
|
32
33
|
// escape hatches
|
|
33
34
|
"custom",
|
|
34
35
|
"transform",
|
|
@@ -162,6 +163,15 @@ const defaultValidationMessages = Object.freeze({
|
|
|
162
163
|
uuid: () => "must be a valid UUID",
|
|
163
164
|
isoDate: () => "must be a valid ISO date (YYYY-MM-DD)",
|
|
164
165
|
creditCard: () => "must be a valid card number",
|
|
166
|
+
phone: (context) => {
|
|
167
|
+
const dial = operand(context.params, "dial");
|
|
168
|
+
const length = operand(context.params, "length");
|
|
169
|
+
if (dial !== null && length !== null) return `must have ${length} digits after ${dial}`;
|
|
170
|
+
const min = operand(context.params, "min");
|
|
171
|
+
const max = operand(context.params, "max");
|
|
172
|
+
if (dial !== null && min !== null && max !== null) return `must have ${min} to ${max} digits after ${dial}`;
|
|
173
|
+
return "must be a valid phone number";
|
|
174
|
+
},
|
|
165
175
|
custom: (context) => context.message,
|
|
166
176
|
transform: (context) => context.message,
|
|
167
177
|
internal: (context) => context.message
|