tele_number_utils 1.0.0 → 1.0.2

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,73 +0,0 @@
1
- import { parsePhoneNumber } from "../parsed";
2
- import { country_list } from "./country";
3
- import {
4
- UK_COUNTRY_CODE,
5
- UK_DEPENDENCIES,
6
- CANADIAN_AREA_CODES,
7
- US_CANADA_COUNTRY_CODE,
8
- PUERTO_RICO_COUNTRY_CODE,
9
- } from "./util";
10
-
11
- export type Country = {
12
- name: string;
13
- dialingCode: string;
14
- flagUrl: string;
15
- alphaTwoCode: string;
16
- };
17
-
18
- /** Get country by alphaTwoCode */
19
- const getCountryByAlpha = (alpha: string): Country | null =>
20
- country_list.find((c) => c.alphaTwoCode === alpha) || null;
21
-
22
- /** Get country by dialing code */
23
- const getCountryByDialingCode = (code: string): Country | null =>
24
- country_list.find((c) => c.dialingCode.replace("+", "") === code) || null;
25
-
26
- /** Handle US & Canada numbers */
27
- const handleUSCanada = (nationalNumber: string): Country | null => {
28
- const areaCode = nationalNumber.slice(0, 3);
29
-
30
- if (PUERTO_RICO_COUNTRY_CODE.includes(areaCode))
31
- return getCountryByAlpha("PR");
32
- if (CANADIAN_AREA_CODES.includes(areaCode)) return getCountryByAlpha("CA");
33
- return getCountryByAlpha("US");
34
- };
35
-
36
- /** Handle UK numbers, including dependencies */
37
- const handleUK = (nationalNumber: string): Country | null => {
38
- const areaPrefix = nationalNumber.substring(0, 4);
39
- const dependency = UK_DEPENDENCIES.find((d) =>
40
- areaPrefix.startsWith(d.PREFIX)
41
- );
42
-
43
- return dependency
44
- ? getCountryByAlpha(dependency.CODE)
45
- : getCountryByAlpha("GB");
46
- };
47
-
48
- /** Main function */
49
- export const getCountry = (number: string): Country | string => {
50
- try {
51
- const parsed = parsePhoneNumber(number);
52
- if (!parsed) return "Invalid Phone number";
53
-
54
- let matched: Country | null = null;
55
-
56
- switch (parsed.callingCode) {
57
- case US_CANADA_COUNTRY_CODE:
58
- matched = handleUSCanada(parsed.nationalNumber || "");
59
- break;
60
- case UK_COUNTRY_CODE:
61
- matched = handleUK(parsed.nationalNumber || "");
62
- break;
63
- default:
64
- matched =
65
- getCountryByAlpha(parsed.country || "") ||
66
- getCountryByDialingCode(parsed.callingCode || "");
67
- }
68
-
69
- return matched || "Country not found";
70
- } catch {
71
- return "Invalid Phone number";
72
- }
73
- };
@@ -1,75 +0,0 @@
1
- export const UK_COUNTRY_CODE = "44";
2
-
3
- export const UK_MAIN = {
4
- CODE: "GB",
5
- NAME: "United Kingdom",
6
- };
7
-
8
- export const ISLE_OF_MAN = {
9
- CODE: "IM",
10
- NAME: "Isle of Man",
11
- PREFIX: "1624",
12
- };
13
-
14
- export const JERSEY = {
15
- CODE: "JE",
16
- NAME: "Jersey",
17
- PREFIX: "1534",
18
- };
19
-
20
- export const GUERNSEY = {
21
- CODE: "GG",
22
- NAME: "Guernsey",
23
- PREFIX: "1481",
24
- };
25
-
26
- export const CANADIAN_AREA_CODES = [
27
- "403",
28
- "587",
29
- "780",
30
- "825",
31
- "368",
32
- "236",
33
- "250",
34
- "604",
35
- "672",
36
- "778",
37
- "204",
38
- "431",
39
- "506",
40
- "709",
41
- "867",
42
- "782",
43
- "902",
44
- "226",
45
- "249",
46
- "289",
47
- "343",
48
- "365",
49
- "416",
50
- "437",
51
- "519",
52
- "548",
53
- "613",
54
- "647",
55
- "683",
56
- "705",
57
- "807",
58
- "905",
59
- "367",
60
- "418",
61
- "438",
62
- "450",
63
- "514",
64
- "579",
65
- "581",
66
- "873",
67
- "306",
68
- "639",
69
- "474",
70
- ];
71
-
72
- export const UK_DEPENDENCIES = [ISLE_OF_MAN, JERSEY, GUERNSEY];
73
-
74
- export const US_CANADA_COUNTRY_CODE = "1";
75
- export const PUERTO_RICO_COUNTRY_CODE = ["787", "939"];
@@ -1,41 +0,0 @@
1
- type Separator = " " | "-" | "()";
2
-
3
- /**
4
- * Formats a number string into groups dynamically
5
- * @param value The input string (digits)
6
- * @param groupSizes Array of group sizes, e.g., [3,3,4]
7
- * @param separator Separator between groups: " ", "-", "/", "()" (default: " ")
8
- */
9
- export const formatter = (
10
- value: string = "",
11
- groupSizes: number[] = [3, 3, 4],
12
- separator: Separator = " "
13
- ): string => {
14
- const digits = value.replace(/\D/g, ""); // remove non-digits
15
- if (!digits) return "";
16
-
17
- // If length <= 6, return as-is
18
- if (digits.length <= 6) return digits;
19
-
20
- const groups: string[] = [];
21
- let start = 0;
22
-
23
- for (let size of groupSizes) {
24
- if (start >= digits.length) break;
25
- groups.push(digits.slice(start, start + size));
26
- start += size;
27
- }
28
-
29
- // Remaining digits
30
- if (start < digits.length) {
31
- groups.push(digits.slice(start));
32
- }
33
-
34
- // Handle parentheses specially: wrap first group
35
- if (separator === "()") {
36
- const [first, ...rest] = groups;
37
- return `(${first}) ${rest.join(" ")}`;
38
- }
39
-
40
- return groups.join(separator);
41
- };
package/src/index.ts 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,289 +0,0 @@
1
- export const countryCodeMap: Record<string, string> = {
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
-
163
- export const NANPA_MAP: Record<string, string> = {
164
- // Canada
165
- "204": "CA",
166
- "236": "CA",
167
- "249": "CA",
168
- "250": "CA",
169
- "289": "CA",
170
- "306": "CA",
171
- "343": "CA",
172
- "365": "CA",
173
- "367": "CA",
174
- "403": "CA",
175
- "416": "CA",
176
- "418": "CA",
177
- "431": "CA",
178
- "437": "CA",
179
- "438": "CA",
180
- "450": "CA",
181
- "506": "CA",
182
- "514": "CA",
183
- "519": "CA",
184
- "548": "CA",
185
- "579": "CA",
186
- "581": "CA",
187
- "587": "CA",
188
- "600": "CA",
189
- "604": "CA",
190
- "613": "CA",
191
- "639": "CA",
192
- "647": "CA",
193
- "672": "CA",
194
- "705": "CA",
195
- "709": "CA",
196
- "778": "CA",
197
- "780": "CA",
198
- "782": "CA",
199
- "807": "CA",
200
- "819": "CA",
201
- "825": "CA",
202
- "867": "CA",
203
- "873": "CA",
204
- "902": "CA",
205
-
206
- // Puerto Rico
207
- "787": "PR",
208
- "939": "PR",
209
-
210
- // US
211
- "201": "US",
212
- "202": "US",
213
- "203": "US",
214
- "212": "US",
215
- "213": "US",
216
- "312": "US",
217
- "305": "US",
218
- "415": "US",
219
- };
220
-
221
- export const UK_COUNTRY_CODE = "44";
222
- export const UK_MAIN = { CODE: "GB", NAME: "United Kingdom" };
223
- export const ISLE_OF_MAN = { CODE: "IM", NAME: "Isle of Man", PREFIX: "1624" };
224
- export const JERSEY = { CODE: "JE", NAME: "Jersey", PREFIX: "1534" };
225
- export const GUERNSEY = { CODE: "GG", NAME: "Guernsey", PREFIX: "1481" };
226
- export const UK_DEPENDENCIES = [ISLE_OF_MAN, JERSEY, GUERNSEY];
227
-
228
- export const AUSTRALIAN_TERRITORIES = ["672"]; // Norfolk, Christmas, Cocos
229
-
230
- export interface ParsedPhoneNumber {
231
- country?: string;
232
- callingCode?: string;
233
- nationalNumber?: string;
234
- formatted?: string;
235
- }
236
-
237
- export function parsePhoneNumber(phone: string): ParsedPhoneNumber {
238
- if (!phone?.startsWith("+")) return { formatted: phone };
239
-
240
- const digits = phone.slice(1);
241
- let country: string | undefined;
242
- let callingCode: string | undefined;
243
- let nationalNumber = digits;
244
-
245
- // Match 1-3 digit country code
246
- for (let len = 3; len > 0; len--) {
247
- const code = digits.slice(0, len);
248
- if (countryCodeMap[code]) {
249
- callingCode = code;
250
- country = countryCodeMap[code];
251
- nationalNumber = digits.slice(len);
252
- break;
253
- }
254
- }
255
-
256
- if (!country) return { formatted: phone, nationalNumber: digits };
257
-
258
- // Handle +1 NANPA
259
- if (callingCode === "1" && nationalNumber.length >= 10) {
260
- const areaCode = nationalNumber.slice(0, 3);
261
- if (NANPA_MAP[areaCode]) country = NANPA_MAP[areaCode];
262
- else country = "US";
263
- }
264
-
265
- // Handle +44 UK dependencies
266
- if (callingCode === UK_COUNTRY_CODE) {
267
- for (const dep of UK_DEPENDENCIES) {
268
- if (nationalNumber.startsWith(dep.PREFIX)) {
269
- country = dep.CODE;
270
- break;
271
- }
272
- }
273
- if (!country) country = UK_MAIN.CODE;
274
- }
275
-
276
- // Handle +672 Australian territories
277
- if (AUSTRALIAN_TERRITORIES.includes(callingCode || "")) {
278
- country = "AU";
279
- }
280
-
281
- // Handle +7 Russia/Kazakhstan
282
- if (callingCode === "7") {
283
- if (nationalNumber.startsWith("6")) country = "KZ";
284
- else country = "RU";
285
- }
286
-
287
- const formatted = `+${callingCode} ${nationalNumber}`;
288
- return { country, callingCode, nationalNumber, formatted };
289
- }
@@ -1,37 +0,0 @@
1
- import { parsePhoneNumber } from ".";
2
-
3
- // Test numbers
4
- const testNumbers = [
5
- // ---- UK +44 ----
6
- "+442071234567", // UK main
7
- "+441632960123", // Isle of Man
8
- "+441534123456", // Jersey
9
- "+441481987654", // Guernsey
10
-
11
- // ---- NANPA +1 ----
12
- "+12015551234", // US
13
- "+14165551234", // Canada
14
- "+17875551234", // Puerto Rico
15
-
16
- // ---- Russia / Kazakhstan +7 ----
17
- "+79161234567", // Russia
18
- "+76171234567", // Kazakhstan
19
-
20
- // ---- Australian territories +672 ----
21
- "+67212345678", // Australian external territory
22
-
23
- // ---- Regular countries ----
24
- "+919876543210", // India
25
- "+4915123456789", // Germany
26
-
27
- // ---- Edge cases ----
28
- "4915123456789", // Missing +
29
- "+999123456789", // Unknown code
30
- ];
31
-
32
- // Run parser and log results
33
- console.log("===== Phone Number Parsing Test =====");
34
- for (const number of testNumbers) {
35
- const parsed = parsePhoneNumber(number);
36
- console.log(`${number} =>`, parsed);
37
- }