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
package/README.md
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
# 📞 Phone Utils
|
|
2
|
+
|
|
3
|
+
A lightweight and fully TypeScript-ready utility library for **phone number parsing, formatting, and country detection**.
|
|
4
|
+
Handles tricky scenarios like **shared country codes** (e.g., +1, +44, +7) and **dependencies** (UK Crown dependencies, US territories, Canadian area codes).
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- Parse phone numbers into **country, calling code, national number, and formatted string**.
|
|
11
|
+
- Detect countries with **shared dialing codes**:
|
|
12
|
+
- +1 → US, Canada, Puerto Rico, Caribbean nations
|
|
13
|
+
- +44 → UK, Isle of Man, Jersey, Guernsey
|
|
14
|
+
- +7 → Russia, Kazakhstan
|
|
15
|
+
- +672 → Australian territories
|
|
16
|
+
|
|
17
|
+
- Format phone numbers **dynamically** with custom group sizes and separators:
|
|
18
|
+
- Separators: space (` `), dash (`-`), slash (`/`), parentheses (`()`)
|
|
19
|
+
|
|
20
|
+
- Fully **TypeScript-ready** with types and interfaces
|
|
21
|
+
- Minimal **dependency-free** footprint
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npm install tele_number_utils
|
|
29
|
+
# or
|
|
30
|
+
yarn add tele_number_utils
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
### Parsing Phone Numbers
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { parsePhoneNumber } from "tele_number_utils";
|
|
41
|
+
|
|
42
|
+
const parsed = parsePhoneNumber("+14165551234");
|
|
43
|
+
console.log(parsed);
|
|
44
|
+
/*
|
|
45
|
+
{
|
|
46
|
+
country: "CA",
|
|
47
|
+
callingCode: "1",
|
|
48
|
+
nationalNumber: "4165551234",
|
|
49
|
+
formatted: "+1 4165551234"
|
|
50
|
+
}
|
|
51
|
+
*/
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Detecting Country from Number
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { getCountry } from "tele_number_utils";
|
|
58
|
+
|
|
59
|
+
console.log(getCountry("+12015551234")); // US
|
|
60
|
+
console.log(getCountry("+14165551234")); // Canada
|
|
61
|
+
console.log(getCountry("+17875551234")); // Puerto Rico
|
|
62
|
+
console.log(getCountry("+442071234567")); // United Kingdom
|
|
63
|
+
console.log(getCountry("+441632960123")); // Isle of Man
|
|
64
|
+
console.log(getCountry("+79161234567")); // Russia
|
|
65
|
+
console.log(getCountry("+76171234567")); // Kazakhstan
|
|
66
|
+
|
|
67
|
+
interface Country {
|
|
68
|
+
name: string;
|
|
69
|
+
dialingCode: string;
|
|
70
|
+
flagUrl: string;
|
|
71
|
+
alphaTwoCode: string;
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Formatting Numbers
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
import { formatter } from "tele_number_utils";
|
|
79
|
+
|
|
80
|
+
// Default grouping [3,3,4]
|
|
81
|
+
console.log(formatter("1234567890")); // "123 456 7890"
|
|
82
|
+
|
|
83
|
+
// Custom grouping with dash separator
|
|
84
|
+
console.log(formatter("123456789012345", [4, 4, 4, 4], "-")); // "1234-5678-9012-345"
|
|
85
|
+
|
|
86
|
+
// Parentheses style
|
|
87
|
+
console.log(formatter("1234567890", [3, 3, 4], "()")); // "(123) 456 7890"
|
|
88
|
+
|
|
89
|
+
// Short numbers <= 6 digits remain unchanged
|
|
90
|
+
console.log(formatter("12345")); // "12345"
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
## Live Console Demo
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
import { getCountry } from "tele_number_utils";
|
|
99
|
+
|
|
100
|
+
const testNumbers = [
|
|
101
|
+
"+12015551234", // US
|
|
102
|
+
"+14165551234", // Canada
|
|
103
|
+
"+17875551234", // Puerto Rico
|
|
104
|
+
"+442071234567", // UK main
|
|
105
|
+
"+441632960123", // Isle of Man
|
|
106
|
+
"+441534123456", // Jersey
|
|
107
|
+
"+441481987654", // Guernsey
|
|
108
|
+
"+79161234567", // Russia
|
|
109
|
+
"+76171234567", // Kazakhstan
|
|
110
|
+
"+67212345678", // Australian territory
|
|
111
|
+
"+919876543210", // India
|
|
112
|
+
"+4915123456789", // Germany
|
|
113
|
+
"4915123456789", // Missing +
|
|
114
|
+
"+999123456789", // Unknown
|
|
115
|
+
];
|
|
116
|
+
|
|
117
|
+
console.log("===== Phone Number Country Detection =====");
|
|
118
|
+
for (const number of testNumbers) {
|
|
119
|
+
const country = getCountry(number);
|
|
120
|
+
console.log(`${number} =>`, country);
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
**Sample Output:**
|
|
125
|
+
|
|
126
|
+
```
|
|
127
|
+
+12015551234 => United States
|
|
128
|
+
+14165551234 => Canada
|
|
129
|
+
+17875551234 => Puerto Rico
|
|
130
|
+
+442071234567 => United Kingdom
|
|
131
|
+
+441632960123 => Isle of Man
|
|
132
|
+
+441534123456 => Jersey
|
|
133
|
+
+441481987654 => Guernsey
|
|
134
|
+
+79161234567 => Russia
|
|
135
|
+
+76171234567 => Kazakhstan
|
|
136
|
+
+67212345678 => Australia
|
|
137
|
+
+919876543210 => India
|
|
138
|
+
+4915123456789 => Germany
|
|
139
|
+
4915123456789 => Invalid Phone number
|
|
140
|
+
+999123456789 => Country not found
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
## API
|
|
146
|
+
|
|
147
|
+
### `parsePhoneNumber(phone: string): ParsedPhoneNumber`
|
|
148
|
+
|
|
149
|
+
Parses a phone number string into:
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
interface ParsedPhoneNumber {
|
|
153
|
+
country?: string; // ISO Alpha-2 country code
|
|
154
|
+
callingCode?: string; // Dialing code
|
|
155
|
+
nationalNumber?: string;
|
|
156
|
+
formatted?: string; // Formatted string
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
---
|
|
161
|
+
|
|
162
|
+
### `getCountry(number: string): Country | string`
|
|
163
|
+
|
|
164
|
+
Returns country information for a given number:
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
interface Country {
|
|
168
|
+
name: string;
|
|
169
|
+
dialingCode: string;
|
|
170
|
+
flagUrl: string;
|
|
171
|
+
alphaTwoCode: string;
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Returns "Invalid Phone number" or "Country not found" for invalid/unknown numbers.
|
|
176
|
+
|
|
177
|
+
---
|
|
178
|
+
|
|
179
|
+
### `formatter(value: string, groupSizes?: number[], separator?: " " | "-" | "/" | "()"): string`
|
|
180
|
+
|
|
181
|
+
Formats digits into **custom groups**.
|
|
182
|
+
|
|
183
|
+
- `groupSizes` – array of numbers specifying group lengths, default `[3,3,4]`
|
|
184
|
+
- `separator` – separator between groups, default `" "`
|
|
185
|
+
|
|
186
|
+
---
|
|
187
|
+
|
|
188
|
+
## Contributing
|
|
189
|
+
|
|
190
|
+
Feel free to open an issue or PR to improve country detection, formatting options, or add more NANPA countries.
|
|
191
|
+
|
|
192
|
+
---
|
|
193
|
+
|
|
194
|
+
## License
|
|
195
|
+
|
|
196
|
+
MIT
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"country.d.ts","sourceRoot":"","sources":["../../src/country/country.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY;;;;;GA69CxB,CAAC"}
|