tele_number_utils 1.0.7 → 1.0.9

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.
@@ -1,9 +0,0 @@
1
- export type Country = {
2
- name: string;
3
- dialingCode: string;
4
- flagUrl: string;
5
- alphaTwoCode: string;
6
- };
7
- /** Main function */
8
- export declare const getCountry: (number: string) => Country;
9
- //# sourceMappingURL=getCountry.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"getCountry.d.ts","sourceRoot":"","sources":["../../src/country/getCountry.ts"],"names":[],"mappings":"AA6BA,MAAM,MAAM,OAAO,GAAG;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,MAAM,CAAC;CACtB,CAAC;AAuEF,oBAAoB;AACpB,eAAO,MAAM,UAAU,GAAI,QAAQ,MAAM,KAAG,OAuB3C,CAAC"}
@@ -1,91 +0,0 @@
1
- import { parsePhoneNumber } from "../parsed";
2
- import { country_list } from "./country";
3
- import { UK_COUNTRY_CODE, UK_DEPENDENCIES, CANADIAN_AREA_CODES, US_CANADA_COUNTRY_CODE, PUERTO_RICO_COUNTRY_CODE, AMERICAN_SAMOA_AREA_CODE, ANGUILLA_AREA_CODE, ANTIGUA_AND_BARBUDA_AREA_CODE, BAHAMAS_AREA_CODE, BARBADOS_AREA_CODE, BERMUDA_AREA_CODE, BRITISH_VIRGIN_ISLANDS_AREA_CODE, CAYMAN_ISLANDS_AREA_CODE, DOMINICAN_REPUBLIC_AREA_CODE, GRENADA_AREA_CODE, JAMAICA_AREA_CODE, MONTSERRAT_AREA_CODE, SAINT_KITTS_AND_NEVIS_AREA_CODE, SAINT_LUCIA_AREA_CODE, SAINT_VINCENT_AND_THE_GRENADINES_AREA_CODE, SINT_MAARTEN_AREA_CODE, TRINIDAD_AND_TOBAGO_AREA_CODE, TURKS_AND_CAICOS_ISLANDS_AREA_CODE, UNITED_STATES_VIRGIN_ISLANDS_AREA_CODE, } from "./util";
4
- const NOT_FOUND_RESULT = {
5
- name: "Not found",
6
- dialingCode: "Invalid dialing code",
7
- flagUrl: "Invalid Flag url",
8
- alphaTwoCode: "Invalid alpha tow code",
9
- };
10
- /** Get country by alphaTwoCode */
11
- const getCountryByAlpha = (alpha) => country_list.find((c) => c.alphaTwoCode === alpha) || null;
12
- /** Get country by dialing code */
13
- const getCountryByDialingCode = (code) => country_list.find((c) => c.dialingCode.replace("+", "") === code) || null;
14
- /** Handle US & Canada numbers */
15
- const handleUSCanada = (nationalNumber) => {
16
- const areaCode = nationalNumber.slice(0, 3);
17
- if (PUERTO_RICO_COUNTRY_CODE.includes(areaCode))
18
- return getCountryByAlpha("PR");
19
- if (AMERICAN_SAMOA_AREA_CODE.includes(areaCode))
20
- return getCountryByAlpha("AS");
21
- if (ANGUILLA_AREA_CODE.includes(areaCode))
22
- return getCountryByAlpha("AI");
23
- if (ANTIGUA_AND_BARBUDA_AREA_CODE.includes(areaCode))
24
- return getCountryByAlpha("AG");
25
- if (BAHAMAS_AREA_CODE.includes(areaCode))
26
- return getCountryByAlpha("BS");
27
- if (BARBADOS_AREA_CODE.includes(areaCode))
28
- return getCountryByAlpha("BB");
29
- if (BERMUDA_AREA_CODE.includes(areaCode))
30
- return getCountryByAlpha("BM");
31
- if (BRITISH_VIRGIN_ISLANDS_AREA_CODE.includes(areaCode))
32
- return getCountryByAlpha("VG");
33
- if (CAYMAN_ISLANDS_AREA_CODE.includes(areaCode))
34
- return getCountryByAlpha("KY");
35
- if (DOMINICAN_REPUBLIC_AREA_CODE.includes(areaCode))
36
- return getCountryByAlpha("DO");
37
- if (GRENADA_AREA_CODE.includes(areaCode))
38
- return getCountryByAlpha("GD");
39
- if (JAMAICA_AREA_CODE.includes(areaCode))
40
- return getCountryByAlpha("JM");
41
- if (MONTSERRAT_AREA_CODE.includes(areaCode))
42
- return getCountryByAlpha("MS");
43
- if (SAINT_KITTS_AND_NEVIS_AREA_CODE.includes(areaCode))
44
- return getCountryByAlpha("KN");
45
- if (SAINT_LUCIA_AREA_CODE.includes(areaCode))
46
- return getCountryByAlpha("LC");
47
- if (SAINT_VINCENT_AND_THE_GRENADINES_AREA_CODE.includes(areaCode))
48
- return getCountryByAlpha("VC");
49
- if (SINT_MAARTEN_AREA_CODE.includes(areaCode))
50
- return getCountryByAlpha("SX");
51
- if (TRINIDAD_AND_TOBAGO_AREA_CODE.includes(areaCode))
52
- return getCountryByAlpha("TT");
53
- if (TURKS_AND_CAICOS_ISLANDS_AREA_CODE.includes(areaCode))
54
- return getCountryByAlpha("TC");
55
- if (UNITED_STATES_VIRGIN_ISLANDS_AREA_CODE.includes(areaCode))
56
- return getCountryByAlpha("VI");
57
- if (CANADIAN_AREA_CODES.includes(areaCode))
58
- return getCountryByAlpha("CA");
59
- return getCountryByAlpha("US");
60
- };
61
- /** Handle UK numbers, including dependencies */
62
- const handleUK = (nationalNumber) => {
63
- const areaPrefix = nationalNumber.substring(0, 4);
64
- const dependency = UK_DEPENDENCIES.find((d) => areaPrefix.startsWith(d.PREFIX));
65
- return dependency
66
- ? getCountryByAlpha(dependency.CODE)
67
- : getCountryByAlpha("GB");
68
- };
69
- /** Main function */
70
- export const getCountry = (number) => {
71
- try {
72
- const parsed = parsePhoneNumber(number);
73
- let matched = null;
74
- switch (parsed.callingCode) {
75
- case US_CANADA_COUNTRY_CODE:
76
- matched = handleUSCanada(parsed.nationalNumber || "");
77
- break;
78
- case UK_COUNTRY_CODE:
79
- matched = handleUK(parsed.nationalNumber || "");
80
- break;
81
- default:
82
- matched =
83
- getCountryByAlpha(parsed.country || "") ||
84
- getCountryByDialingCode(parsed.callingCode || "");
85
- }
86
- return matched || NOT_FOUND_RESULT;
87
- }
88
- catch {
89
- return NOT_FOUND_RESULT;
90
- }
91
- };
@@ -1,48 +0,0 @@
1
- export declare const UK_COUNTRY_CODE = "44";
2
- export declare const UK_MAIN: {
3
- CODE: string;
4
- NAME: string;
5
- };
6
- export declare const ISLE_OF_MAN: {
7
- CODE: string;
8
- NAME: string;
9
- PREFIX: string;
10
- };
11
- export declare const JERSEY: {
12
- CODE: string;
13
- NAME: string;
14
- PREFIX: string;
15
- };
16
- export declare const GUERNSEY: {
17
- CODE: string;
18
- NAME: string;
19
- PREFIX: string;
20
- };
21
- export declare const CANADIAN_AREA_CODES: string[];
22
- export declare const UK_DEPENDENCIES: {
23
- CODE: string;
24
- NAME: string;
25
- PREFIX: string;
26
- }[];
27
- export declare const US_CANADA_COUNTRY_CODE = "1";
28
- export declare const PUERTO_RICO_COUNTRY_CODE: string[];
29
- export declare const AMERICAN_SAMOA_AREA_CODE: string[];
30
- export declare const ANGUILLA_AREA_CODE: string[];
31
- export declare const ANTIGUA_AND_BARBUDA_AREA_CODE: string[];
32
- export declare const BAHAMAS_AREA_CODE: string[];
33
- export declare const BARBADOS_AREA_CODE: string[];
34
- export declare const BERMUDA_AREA_CODE: string[];
35
- export declare const BRITISH_VIRGIN_ISLANDS_AREA_CODE: string[];
36
- export declare const CAYMAN_ISLANDS_AREA_CODE: string[];
37
- export declare const DOMINICAN_REPUBLIC_AREA_CODE: string[];
38
- export declare const GRENADA_AREA_CODE: string[];
39
- export declare const JAMAICA_AREA_CODE: string[];
40
- export declare const MONTSERRAT_AREA_CODE: string[];
41
- export declare const SAINT_KITTS_AND_NEVIS_AREA_CODE: string[];
42
- export declare const SAINT_LUCIA_AREA_CODE: string[];
43
- export declare const SAINT_VINCENT_AND_THE_GRENADINES_AREA_CODE: string[];
44
- export declare const SINT_MAARTEN_AREA_CODE: string[];
45
- export declare const TRINIDAD_AND_TOBAGO_AREA_CODE: string[];
46
- export declare const TURKS_AND_CAICOS_ISLANDS_AREA_CODE: string[];
47
- export declare const UNITED_STATES_VIRGIN_ISLANDS_AREA_CODE: string[];
48
- //# sourceMappingURL=util.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../src/country/util.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,eAAe,OAAO,CAAC;AAEpC,eAAO,MAAM,OAAO;;;CAGnB,CAAC;AAEF,eAAO,MAAM,WAAW;;;;CAIvB,CAAC;AAEF,eAAO,MAAM,MAAM;;;;CAIlB,CAAC;AAEF,eAAO,MAAM,QAAQ;;;;CAIpB,CAAC;AAEF,eAAO,MAAM,mBAAmB,UA4C/B,CAAC;AAEF,eAAO,MAAM,eAAe;;;;GAAkC,CAAC;AAC/D,eAAO,MAAM,sBAAsB,MAAM,CAAC;AAC1C,eAAO,MAAM,wBAAwB,UAAiB,CAAC;AACvD,eAAO,MAAM,wBAAwB,UAAU,CAAC;AAChD,eAAO,MAAM,kBAAkB,UAAU,CAAC;AAC1C,eAAO,MAAM,6BAA6B,UAAU,CAAC;AACrD,eAAO,MAAM,iBAAiB,UAAU,CAAC;AACzC,eAAO,MAAM,kBAAkB,UAAU,CAAC;AAC1C,eAAO,MAAM,iBAAiB,UAAU,CAAC;AACzC,eAAO,MAAM,gCAAgC,UAAU,CAAC;AACxD,eAAO,MAAM,wBAAwB,UAAU,CAAC;AAChD,eAAO,MAAM,4BAA4B,UAAwB,CAAC;AAClE,eAAO,MAAM,iBAAiB,UAAU,CAAC;AACzC,eAAO,MAAM,iBAAiB,UAAiB,CAAC;AAChD,eAAO,MAAM,oBAAoB,UAAU,CAAC;AAC5C,eAAO,MAAM,+BAA+B,UAAU,CAAC;AACvD,eAAO,MAAM,qBAAqB,UAAU,CAAC;AAC7C,eAAO,MAAM,0CAA0C,UAAU,CAAC;AAClE,eAAO,MAAM,sBAAsB,UAAU,CAAC;AAC9C,eAAO,MAAM,6BAA6B,UAAU,CAAC;AACrD,eAAO,MAAM,kCAAkC,UAAU,CAAC;AAC1D,eAAO,MAAM,sCAAsC,UAAU,CAAC"}
@@ -1,87 +0,0 @@
1
- export const UK_COUNTRY_CODE = "44";
2
- export const UK_MAIN = {
3
- CODE: "GB",
4
- NAME: "United Kingdom",
5
- };
6
- export const ISLE_OF_MAN = {
7
- CODE: "IM",
8
- NAME: "Isle of Man",
9
- PREFIX: "1624",
10
- };
11
- export const JERSEY = {
12
- CODE: "JE",
13
- NAME: "Jersey",
14
- PREFIX: "1534",
15
- };
16
- export const GUERNSEY = {
17
- CODE: "GG",
18
- NAME: "Guernsey",
19
- PREFIX: "1481",
20
- };
21
- export const CANADIAN_AREA_CODES = [
22
- "403",
23
- "587",
24
- "780",
25
- "825",
26
- "368",
27
- "236",
28
- "250",
29
- "604",
30
- "672",
31
- "778",
32
- "204",
33
- "431",
34
- "506",
35
- "709",
36
- "867",
37
- "782",
38
- "902",
39
- "226",
40
- "249",
41
- "289",
42
- "343",
43
- "365",
44
- "416",
45
- "437",
46
- "519",
47
- "548",
48
- "613",
49
- "647",
50
- "683",
51
- "705",
52
- "807",
53
- "905",
54
- "367",
55
- "418",
56
- "438",
57
- "450",
58
- "514",
59
- "579",
60
- "581",
61
- "873",
62
- "306",
63
- "639",
64
- "474",
65
- ];
66
- export const UK_DEPENDENCIES = [ISLE_OF_MAN, JERSEY, GUERNSEY];
67
- export const US_CANADA_COUNTRY_CODE = "1";
68
- export const PUERTO_RICO_COUNTRY_CODE = ["787", "939"];
69
- export const AMERICAN_SAMOA_AREA_CODE = ["684"];
70
- export const ANGUILLA_AREA_CODE = ["264"];
71
- export const ANTIGUA_AND_BARBUDA_AREA_CODE = ["268"];
72
- export const BAHAMAS_AREA_CODE = ["242"];
73
- export const BARBADOS_AREA_CODE = ["246"];
74
- export const BERMUDA_AREA_CODE = ["441"];
75
- export const BRITISH_VIRGIN_ISLANDS_AREA_CODE = ["284"];
76
- export const CAYMAN_ISLANDS_AREA_CODE = ["345"];
77
- export const DOMINICAN_REPUBLIC_AREA_CODE = ["809", "829", "849"];
78
- export const GRENADA_AREA_CODE = ["473"];
79
- export const JAMAICA_AREA_CODE = ["658", "876"];
80
- export const MONTSERRAT_AREA_CODE = ["664"];
81
- export const SAINT_KITTS_AND_NEVIS_AREA_CODE = ["869"];
82
- export const SAINT_LUCIA_AREA_CODE = ["758"];
83
- export const SAINT_VINCENT_AND_THE_GRENADINES_AREA_CODE = ["784"];
84
- export const SINT_MAARTEN_AREA_CODE = ["721"];
85
- export const TRINIDAD_AND_TOBAGO_AREA_CODE = ["868"];
86
- export const TURKS_AND_CAICOS_ISLANDS_AREA_CODE = ["649"];
87
- export const UNITED_STATES_VIRGIN_ISLANDS_AREA_CODE = ["340"];
@@ -1,4 +0,0 @@
1
- type Separator = " " | "-" | "()";
2
- export declare const formatter: (value?: string, groupSizes?: number[], separator?: Separator) => string;
3
- export {};
4
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/formatter/index.ts"],"names":[],"mappings":"AAEA,KAAK,SAAS,GAAG,GAAG,GAAG,GAAG,GAAG,IAAI,CAAC;AAuBlC,eAAO,MAAM,SAAS,GACpB,QAAO,MAAW,EAClB,aAAY,MAAM,EAAc,EAChC,YAAW,SAAe,KACzB,MA2BF,CAAC"}
@@ -1,35 +0,0 @@
1
- import { parsePhoneNumber } from "../parsed/index.js";
2
- const formatGroups = (number, groupSizes, separator) => {
3
- const groups = [];
4
- let start = 0;
5
- for (const size of groupSizes) {
6
- if (start >= number.length)
7
- break;
8
- groups.push(number.slice(start, start + size));
9
- start += size;
10
- }
11
- if (start < number.length) {
12
- groups.push(number.slice(start));
13
- }
14
- return groups.join(separator);
15
- };
16
- export const formatter = (value = "", groupSizes = [3, 3, 4], separator = " ") => {
17
- const trimmed = value.trim();
18
- if (!trimmed)
19
- return "";
20
- const digits = trimmed.replace(/\D/g, "");
21
- if (!digits)
22
- return "";
23
- try {
24
- // Always parse full number
25
- const parsed = parsePhoneNumber(trimmed.startsWith("+") ? trimmed : `+${digits}`);
26
- const formattedNational = formatGroups(parsed.nationalNumber || "", groupSizes, separator === "()" ? " " : separator);
27
- if (separator === "()") {
28
- return `(+${parsed.callingCode}) ${formattedNational}`;
29
- }
30
- return `+${parsed.callingCode} ${formattedNational}`;
31
- }
32
- catch {
33
- return value;
34
- }
35
- };
package/dist/index.d.ts DELETED
@@ -1,5 +0,0 @@
1
- export { parsePhoneNumber } from "./parsed";
2
- export { getCountry } from "./country/getCountry";
3
- export { formatter } from "./formatter";
4
- export { country_list } from "./country/country";
5
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC"}
package/dist/index.js DELETED
@@ -1,4 +0,0 @@
1
- export { parsePhoneNumber } from "./parsed";
2
- export { getCountry } from "./country/getCountry";
3
- export { formatter } from "./formatter";
4
- export { country_list } from "./country/country";
@@ -1,36 +0,0 @@
1
- export declare const countryCodeMap: Record<string, string>;
2
- export declare const NANPA_MAP: Record<string, string>;
3
- export declare const UK_COUNTRY_CODE = "44";
4
- export declare const UK_MAIN: {
5
- CODE: string;
6
- NAME: string;
7
- };
8
- export declare const ISLE_OF_MAN: {
9
- CODE: string;
10
- NAME: string;
11
- PREFIX: string;
12
- };
13
- export declare const JERSEY: {
14
- CODE: string;
15
- NAME: string;
16
- PREFIX: string;
17
- };
18
- export declare const GUERNSEY: {
19
- CODE: string;
20
- NAME: string;
21
- PREFIX: string;
22
- };
23
- export declare const UK_DEPENDENCIES: {
24
- CODE: string;
25
- NAME: string;
26
- PREFIX: string;
27
- }[];
28
- export declare const AUSTRALIAN_TERRITORIES: string[];
29
- export interface ParsedPhoneNumber {
30
- country?: string;
31
- callingCode?: string;
32
- nationalNumber?: string;
33
- formatted?: string;
34
- }
35
- export declare function parsePhoneNumber(phone: string): ParsedPhoneNumber;
36
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/parsed/index.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAgKjD,CAAC;AAEF,eAAO,MAAM,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAwD5C,CAAC;AAEF,eAAO,MAAM,eAAe,OAAO,CAAC;AACpC,eAAO,MAAM,OAAO;;;CAAyC,CAAC;AAC9D,eAAO,MAAM,WAAW;;;;CAAsD,CAAC;AAC/E,eAAO,MAAM,MAAM;;;;CAAiD,CAAC;AACrE,eAAO,MAAM,QAAQ;;;;CAAmD,CAAC;AACzE,eAAO,MAAM,eAAe;;;;GAAkC,CAAC;AAE/D,eAAO,MAAM,sBAAsB,UAAU,CAAC;AAE9C,MAAM,WAAW,iBAAiB;IAChC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,iBAAiB,CAoDjE"}
@@ -1,275 +0,0 @@
1
- export const countryCodeMap = {
2
- "93": "AF",
3
- "355": "AL",
4
- "213": "DZ",
5
- "1": "US",
6
- "376": "AD",
7
- "244": "AO",
8
- "54": "AR",
9
- "374": "AM",
10
- "297": "AW",
11
- "61": "AU",
12
- "43": "AT",
13
- "994": "AZ",
14
- "973": "BH",
15
- "880": "BD",
16
- "32": "BE",
17
- "501": "BZ",
18
- "229": "BJ",
19
- "975": "BT",
20
- "591": "BO",
21
- "387": "BA",
22
- "267": "BW",
23
- "55": "BR",
24
- "359": "BG",
25
- "226": "BF",
26
- "257": "BI",
27
- "855": "KH",
28
- "237": "CM",
29
- "238": "CV",
30
- "236": "CF",
31
- "235": "TD",
32
- "56": "CL",
33
- "86": "CN",
34
- "57": "CO",
35
- "269": "KM",
36
- "243": "CD",
37
- "242": "CG",
38
- "682": "CK",
39
- "506": "CR",
40
- "225": "CI",
41
- "385": "HR",
42
- "53": "CU",
43
- "357": "CY",
44
- "420": "CZ",
45
- "45": "DK",
46
- "253": "DJ",
47
- "593": "EC",
48
- "20": "EG",
49
- "503": "SV",
50
- "240": "GQ",
51
- "291": "ER",
52
- "372": "EE",
53
- "251": "ET",
54
- "298": "FO",
55
- "679": "FJ",
56
- "358": "FI",
57
- "33": "FR",
58
- "594": "GF",
59
- "689": "PF",
60
- "241": "GA",
61
- "220": "GM",
62
- "995": "GE",
63
- "49": "DE",
64
- "233": "GH",
65
- "350": "GI",
66
- "30": "GR",
67
- "502": "GT",
68
- "224": "GN",
69
- "245": "GW",
70
- "592": "GY",
71
- "509": "HT",
72
- "504": "HN",
73
- "852": "HK",
74
- "36": "HU",
75
- "354": "IS",
76
- "91": "IN",
77
- "62": "ID",
78
- "98": "IR",
79
- "964": "IQ",
80
- "353": "IE",
81
- "972": "IL",
82
- "39": "IT",
83
- "81": "JP",
84
- "962": "JO",
85
- "254": "KE",
86
- "686": "KI",
87
- "965": "KW",
88
- "996": "KG",
89
- "856": "LA",
90
- "371": "LV",
91
- "961": "LB",
92
- "266": "LS",
93
- "231": "LR",
94
- "218": "LY",
95
- "423": "LI",
96
- "370": "LT",
97
- "352": "LU",
98
- "261": "MG",
99
- "265": "MW",
100
- "60": "MY",
101
- "960": "MV",
102
- "223": "ML",
103
- "356": "MT",
104
- "692": "MH",
105
- "596": "MQ",
106
- "222": "MR",
107
- "230": "MU",
108
- "52": "MX",
109
- "691": "FM",
110
- "373": "MD",
111
- "377": "MC",
112
- "976": "MN",
113
- "382": "ME",
114
- "212": "MA",
115
- "258": "MZ",
116
- "95": "MM",
117
- "264": "NA",
118
- "674": "NR",
119
- "977": "NP",
120
- "31": "NL",
121
- "687": "NC",
122
- "64": "NZ",
123
- "505": "NI",
124
- "227": "NE",
125
- "234": "NG",
126
- "683": "NU",
127
- "672": "NF",
128
- "47": "NO",
129
- "968": "OM",
130
- "92": "PK",
131
- "680": "PW",
132
- "507": "PA",
133
- "675": "PG",
134
- "595": "PY",
135
- "51": "PE",
136
- "63": "PH",
137
- "48": "PL",
138
- "351": "PT",
139
- "974": "QA",
140
- "40": "RO",
141
- "7": "RU",
142
- "250": "RW",
143
- "290": "SH",
144
- "216": "TN",
145
- "90": "TR",
146
- "993": "TM",
147
- "688": "TV",
148
- "256": "UG",
149
- "380": "UA",
150
- "971": "AE",
151
- "44": "GB",
152
- "598": "UY",
153
- "998": "UZ",
154
- "678": "VU",
155
- "58": "VE",
156
- "84": "VN",
157
- "681": "WF",
158
- "967": "YE",
159
- "260": "ZM",
160
- "263": "ZW",
161
- };
162
- export const NANPA_MAP = {
163
- // Canada
164
- "204": "CA",
165
- "236": "CA",
166
- "249": "CA",
167
- "250": "CA",
168
- "289": "CA",
169
- "306": "CA",
170
- "343": "CA",
171
- "365": "CA",
172
- "367": "CA",
173
- "403": "CA",
174
- "416": "CA",
175
- "418": "CA",
176
- "431": "CA",
177
- "437": "CA",
178
- "438": "CA",
179
- "450": "CA",
180
- "506": "CA",
181
- "514": "CA",
182
- "519": "CA",
183
- "548": "CA",
184
- "579": "CA",
185
- "581": "CA",
186
- "587": "CA",
187
- "600": "CA",
188
- "604": "CA",
189
- "613": "CA",
190
- "639": "CA",
191
- "647": "CA",
192
- "672": "CA",
193
- "705": "CA",
194
- "709": "CA",
195
- "778": "CA",
196
- "780": "CA",
197
- "782": "CA",
198
- "807": "CA",
199
- "819": "CA",
200
- "825": "CA",
201
- "867": "CA",
202
- "873": "CA",
203
- "902": "CA",
204
- // Puerto Rico
205
- "787": "PR",
206
- "939": "PR",
207
- // US
208
- "201": "US",
209
- "202": "US",
210
- "203": "US",
211
- "212": "US",
212
- "213": "US",
213
- "312": "US",
214
- "305": "US",
215
- "415": "US",
216
- };
217
- export const UK_COUNTRY_CODE = "44";
218
- export const UK_MAIN = { CODE: "GB", NAME: "United Kingdom" };
219
- export const ISLE_OF_MAN = { CODE: "IM", NAME: "Isle of Man", PREFIX: "1624" };
220
- export const JERSEY = { CODE: "JE", NAME: "Jersey", PREFIX: "1534" };
221
- export const GUERNSEY = { CODE: "GG", NAME: "Guernsey", PREFIX: "1481" };
222
- export const UK_DEPENDENCIES = [ISLE_OF_MAN, JERSEY, GUERNSEY];
223
- export const AUSTRALIAN_TERRITORIES = ["672"]; // Norfolk, Christmas, Cocos
224
- export function parsePhoneNumber(phone) {
225
- if (!phone?.startsWith("+"))
226
- return { formatted: phone };
227
- const digits = phone.slice(1);
228
- let country;
229
- let callingCode;
230
- let nationalNumber = digits;
231
- // Match 1-3 digit country code
232
- for (let len = 3; len > 0; len--) {
233
- const code = digits.slice(0, len);
234
- if (countryCodeMap[code]) {
235
- callingCode = code;
236
- country = countryCodeMap[code];
237
- nationalNumber = digits.slice(len);
238
- break;
239
- }
240
- }
241
- if (!country)
242
- return { formatted: phone, nationalNumber: digits };
243
- // Handle +1 NANPA
244
- if (callingCode === "1" && nationalNumber.length >= 10) {
245
- const areaCode = nationalNumber.slice(0, 3);
246
- if (NANPA_MAP[areaCode])
247
- country = NANPA_MAP[areaCode];
248
- else
249
- country = "US";
250
- }
251
- // Handle +44 UK dependencies
252
- if (callingCode === UK_COUNTRY_CODE) {
253
- for (const dep of UK_DEPENDENCIES) {
254
- if (nationalNumber.startsWith(dep.PREFIX)) {
255
- country = dep.CODE;
256
- break;
257
- }
258
- }
259
- if (!country)
260
- country = UK_MAIN.CODE;
261
- }
262
- // Handle +672 Australian territories
263
- if (AUSTRALIAN_TERRITORIES.includes(callingCode || "")) {
264
- country = "AU";
265
- }
266
- // Handle +7 Russia/Kazakhstan
267
- if (callingCode === "7") {
268
- if (nationalNumber.startsWith("6"))
269
- country = "KZ";
270
- else
271
- country = "RU";
272
- }
273
- const formatted = `+${callingCode} ${nationalNumber}`;
274
- return { country, callingCode, nationalNumber, formatted };
275
- }
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"test.d.ts","sourceRoot":"","sources":["../../src/parsed/test.ts"],"names":[],"mappings":""}
@@ -1,30 +0,0 @@
1
- import { parsePhoneNumber } from ".";
2
- // Test numbers
3
- const testNumbers = [
4
- // ---- UK +44 ----
5
- "+442071234567", // UK main
6
- "+441632960123", // Isle of Man
7
- "+441534123456", // Jersey
8
- "+441481987654", // Guernsey
9
- // ---- NANPA +1 ----
10
- "+12015551234", // US
11
- "+14165551234", // Canada
12
- "+17875551234", // Puerto Rico
13
- // ---- Russia / Kazakhstan +7 ----
14
- "+79161234567", // Russia
15
- "+76171234567", // Kazakhstan
16
- // ---- Australian territories +672 ----
17
- "+67212345678", // Australian external territory
18
- // ---- Regular countries ----
19
- "+919876543210", // India
20
- "+4915123456789", // Germany
21
- // ---- Edge cases ----
22
- "4915123456789", // Missing +
23
- "+999123456789", // Unknown code
24
- ];
25
- // Run parser and log results
26
- console.log("===== Phone Number Parsing Test =====");
27
- for (const number of testNumbers) {
28
- const parsed = parsePhoneNumber(number);
29
- console.log(`${number} =>`, parsed);
30
- }