tele_number_utils 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +196 -0
- package/dist/country/country.d.ts +7 -0
- package/dist/country/country.d.ts.map +1 -0
- package/dist/country/country.js +1502 -0
- package/dist/country/getCountry.d.ts +9 -0
- package/dist/country/getCountry.d.ts.map +1 -0
- package/dist/country/getCountry.js +49 -0
- package/dist/country/util.d.ts +29 -0
- package/dist/country/util.d.ts.map +1 -0
- package/dist/country/util.js +68 -0
- package/dist/formatter/index.d.ts +10 -0
- package/dist/formatter/index.d.ts.map +1 -0
- package/dist/formatter/index.js +32 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/parsed/index.d.ts +36 -0
- package/dist/parsed/index.d.ts.map +1 -0
- package/dist/parsed/index.js +275 -0
- package/dist/parsed/test.d.ts +2 -0
- package/dist/parsed/test.d.ts.map +1 -0
- package/dist/parsed/test.js +30 -0
- package/package.json +30 -0
- package/src/country/country.ts +1502 -0
- package/src/country/getCountry.ts +73 -0
- package/src/country/util.ts +75 -0
- package/src/formatter/index.ts +41 -0
- package/src/index.ts +4 -0
- package/src/parsed/index.ts +289 -0
- package/src/parsed/test.ts +37 -0
- package/tsconfig.json +17 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getCountry.d.ts","sourceRoot":"","sources":["../../src/country/getCountry.ts"],"names":[],"mappings":"AAUA,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;AAgCF,oBAAoB;AACpB,eAAO,MAAM,UAAU,GAAI,QAAQ,MAAM,KAAG,OAAO,GAAG,MAwBrD,CAAC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
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, } from "./util";
|
|
4
|
+
/** Get country by alphaTwoCode */
|
|
5
|
+
const getCountryByAlpha = (alpha) => country_list.find((c) => c.alphaTwoCode === alpha) || null;
|
|
6
|
+
/** Get country by dialing code */
|
|
7
|
+
const getCountryByDialingCode = (code) => country_list.find((c) => c.dialingCode.replace("+", "") === code) || null;
|
|
8
|
+
/** Handle US & Canada numbers */
|
|
9
|
+
const handleUSCanada = (nationalNumber) => {
|
|
10
|
+
const areaCode = nationalNumber.slice(0, 3);
|
|
11
|
+
if (PUERTO_RICO_COUNTRY_CODE.includes(areaCode))
|
|
12
|
+
return getCountryByAlpha("PR");
|
|
13
|
+
if (CANADIAN_AREA_CODES.includes(areaCode))
|
|
14
|
+
return getCountryByAlpha("CA");
|
|
15
|
+
return getCountryByAlpha("US");
|
|
16
|
+
};
|
|
17
|
+
/** Handle UK numbers, including dependencies */
|
|
18
|
+
const handleUK = (nationalNumber) => {
|
|
19
|
+
const areaPrefix = nationalNumber.substring(0, 4);
|
|
20
|
+
const dependency = UK_DEPENDENCIES.find((d) => areaPrefix.startsWith(d.PREFIX));
|
|
21
|
+
return dependency
|
|
22
|
+
? getCountryByAlpha(dependency.CODE)
|
|
23
|
+
: getCountryByAlpha("GB");
|
|
24
|
+
};
|
|
25
|
+
/** Main function */
|
|
26
|
+
export const getCountry = (number) => {
|
|
27
|
+
try {
|
|
28
|
+
const parsed = parsePhoneNumber(number);
|
|
29
|
+
if (!parsed)
|
|
30
|
+
return "Invalid Phone number";
|
|
31
|
+
let matched = null;
|
|
32
|
+
switch (parsed.callingCode) {
|
|
33
|
+
case US_CANADA_COUNTRY_CODE:
|
|
34
|
+
matched = handleUSCanada(parsed.nationalNumber || "");
|
|
35
|
+
break;
|
|
36
|
+
case UK_COUNTRY_CODE:
|
|
37
|
+
matched = handleUK(parsed.nationalNumber || "");
|
|
38
|
+
break;
|
|
39
|
+
default:
|
|
40
|
+
matched =
|
|
41
|
+
getCountryByAlpha(parsed.country || "") ||
|
|
42
|
+
getCountryByDialingCode(parsed.callingCode || "");
|
|
43
|
+
}
|
|
44
|
+
return matched || "Country not found";
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
return "Invalid Phone number";
|
|
48
|
+
}
|
|
49
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
//# sourceMappingURL=util.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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;AAE/D,eAAO,MAAM,sBAAsB,MAAM,CAAC;AAC1C,eAAO,MAAM,wBAAwB,UAAiB,CAAC"}
|
|
@@ -0,0 +1,68 @@
|
|
|
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"];
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
type Separator = " " | "-" | "()";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a number string into groups dynamically
|
|
4
|
+
* @param value The input string (digits)
|
|
5
|
+
* @param groupSizes Array of group sizes, e.g., [3,3,4]
|
|
6
|
+
* @param separator Separator between groups: " ", "-", "/", "()" (default: " ")
|
|
7
|
+
*/
|
|
8
|
+
export declare const formatter: (value?: string, groupSizes?: number[], separator?: Separator) => string;
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/formatter/index.ts"],"names":[],"mappings":"AAAA,KAAK,SAAS,GAAG,GAAG,GAAG,GAAG,GAAG,IAAI,CAAC;AAElC;;;;;GAKG;AACH,eAAO,MAAM,SAAS,GACpB,QAAO,MAAW,EAClB,aAAY,MAAM,EAAc,EAChC,YAAW,SAAe,KACzB,MA4BF,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a number string into groups dynamically
|
|
3
|
+
* @param value The input string (digits)
|
|
4
|
+
* @param groupSizes Array of group sizes, e.g., [3,3,4]
|
|
5
|
+
* @param separator Separator between groups: " ", "-", "/", "()" (default: " ")
|
|
6
|
+
*/
|
|
7
|
+
export const formatter = (value = "", groupSizes = [3, 3, 4], separator = " ") => {
|
|
8
|
+
const digits = value.replace(/\D/g, ""); // remove non-digits
|
|
9
|
+
if (!digits)
|
|
10
|
+
return "";
|
|
11
|
+
// If length <= 6, return as-is
|
|
12
|
+
if (digits.length <= 6)
|
|
13
|
+
return digits;
|
|
14
|
+
const groups = [];
|
|
15
|
+
let start = 0;
|
|
16
|
+
for (let size of groupSizes) {
|
|
17
|
+
if (start >= digits.length)
|
|
18
|
+
break;
|
|
19
|
+
groups.push(digits.slice(start, start + size));
|
|
20
|
+
start += size;
|
|
21
|
+
}
|
|
22
|
+
// Remaining digits
|
|
23
|
+
if (start < digits.length) {
|
|
24
|
+
groups.push(digits.slice(start));
|
|
25
|
+
}
|
|
26
|
+
// Handle parentheses specially: wrap first group
|
|
27
|
+
if (separator === "()") {
|
|
28
|
+
const [first, ...rest] = groups;
|
|
29
|
+
return `(${first}) ${rest.join(" ")}`;
|
|
30
|
+
}
|
|
31
|
+
return groups.join(separator);
|
|
32
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1,275 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test.d.ts","sourceRoot":"","sources":["../../src/parsed/test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,30 @@
|
|
|
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
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "tele_number_utils",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"dev": "ts-node src/index.ts",
|
|
11
|
+
"start": "node dist/index.js",
|
|
12
|
+
"clean": "rm -rf dist",
|
|
13
|
+
"lint": "eslint 'src/**/*.{ts,tsx}'",
|
|
14
|
+
"format": "prettier --write 'src/**/*.{ts,tsx,js,json,md}'"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [],
|
|
17
|
+
"author": "",
|
|
18
|
+
"license": "ISC",
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/node": "^25.0.7",
|
|
21
|
+
"@typescript-eslint/eslint-plugin": "^8.53.0",
|
|
22
|
+
"@typescript-eslint/parser": "^8.53.0",
|
|
23
|
+
"eslint": "^9.39.2",
|
|
24
|
+
"eslint-config-prettier": "^10.1.8",
|
|
25
|
+
"eslint-plugin-prettier": "^5.5.4",
|
|
26
|
+
"prettier": "^3.7.4",
|
|
27
|
+
"ts-node": "^10.9.2",
|
|
28
|
+
"typescript": "^5.9.3"
|
|
29
|
+
}
|
|
30
|
+
}
|