1 | [![npm version][npm-image]][npm-url]
|
2 | [![downloads][downloads-image]][npm-url]
|
3 | [![build status][build-image]][build-url]
|
4 | [![Language grade: JavaScript][lgtm-image]][lgtm-url]
|
5 | [![Install size][packagephobia-image]][packagephobia-url]
|
6 |
|
7 |
|
8 | # Awesome phonenumber parser
|
9 |
|
10 | This library is a pre-compiled version of Google's `libphonenumber`, with a slightly simpler interface. It has a minimal footprint - is by far the smallest libphonenumber-based library available on npmjs, and has no dependencies.
|
11 |
|
12 | TypeScript typings are provided within the package.
|
13 |
|
14 | Uses libphonenumber v8.12.28
|
15 |
|
16 | ## Comparison with other libraries
|
17 |
|
18 | Since this library is pre-compiled, it doesn't depend on the closure compiler, and needs not load it on start. This makes the library faster and saves you a lot of space. It also means this library is trivial to use in any `webpack` project (or using any other means to run in the browser).
|
19 |
|
20 | Among all the phone number libraries using Google's `libphonenumber`, only this one, `google-libphonenumber` (2.0.0) and `node-phonenumber` (0.2.2) had decent README's with examples. Other libraries embedding the closure compiler should get comparable figures.
|
21 |
|
22 | Loading the closure compiler also adds to the application memory usage (RSS is measured here). The library footprints are also bigger, making `npm install` slower and increasing deploy times.
|
23 |
|
24 | A test program loading a library, then parsing a phone number is called 100 times for each library, the mean values are:
|
25 |
|
26 | Action | awesome-phonenumber (7.5.2) | google-libphonenumber (7.6.1) | node-phonenumber (7.5.2)
|
27 | ------------------------- | ------------------- | --------------------- | ----------------
|
28 | Load library first time | 20.84 ms | 60.99ms | 99.27 ms
|
29 | Parse first phone number | 5.79 ms | 6.51 ms | 8.15 ms
|
30 | Parse second phone number | 0.33 ms | 0.67 ms | 0.80 ms
|
31 | Increased memory usage | 7.3 M | 13.8 M | 22.5 M
|
32 | node_modules size | 248 K | 436 K | 57 M
|
33 | node_modules files | 7 | 7 | 4525
|
34 | time npm install | 667 ms | 700 ms | 4077 ms
|
35 |
|
36 | ## Basic usage
|
37 | ```js
|
38 | var PhoneNumber = require( 'awesome-phonenumber' );
|
39 |
|
40 | var pn = new PhoneNumber( '0707123456', 'SE' );
|
41 | pn.isValid( ); // -> true
|
42 | pn.isMobile( ); // -> true
|
43 | pn.canBeInternationallyDialled( ); // -> true
|
44 | pn.getNumber( ); // -> '+46707123456'
|
45 | pn.getNumber( 'e164' ); // -> '+46707123456' (default)
|
46 | pn.getNumber( 'international' ); // -> '+46 70 712 34 56'
|
47 | pn.getNumber( 'national' ); // -> '070-712 34 56'
|
48 | pn.getNumber( 'rfc3966' ); // -> 'tel:+46-70-712-34-56'
|
49 | pn.getNumber( 'significant' ); // -> '707123456'
|
50 | pn.getRegionCode( ); // -> 'SE'
|
51 | pn.getCountryCode( ); // -> 46
|
52 |
|
53 | pn.toJSON( ); // -> json blob, so that:
|
54 | JSON.stringify( pn, null, 4 ); // -> This:
|
55 | // {
|
56 | // "canBeInternationallyDialled": true,
|
57 | // "number": {
|
58 | // "input": "0707123456",
|
59 | // "international": "+46 70 712 34 56",
|
60 | // "national": "070-712 34 56",
|
61 | // "e164": "+46707123456",
|
62 | // "rfc3966": "tel:+46-70-712-34-56",
|
63 | // "significant": "707123456"
|
64 | // },
|
65 | // "regionCode": "SE",
|
66 | // "valid": true,
|
67 | // "possible": true,
|
68 | // "type": "mobile",
|
69 | // "possibility": "is-possible"
|
70 | // }
|
71 | ```
|
72 |
|
73 | ### Detect country
|
74 |
|
75 | When constructed with a phone number on `e164` format (i.e. prefixed with a `+`), awesome-phonenumber will auto-detect the country:
|
76 |
|
77 | ```js
|
78 | PhoneNumber( '+46707123456' ).getRegionCode( ); // -> 'SE'
|
79 | ```
|
80 |
|
81 | ## API types
|
82 |
|
83 | The API consists of the `PhoneNumber` class which sometimes uses *enums*. These are:
|
84 |
|
85 | ### <a name="phone-number-types"></a>Phone number types
|
86 | ```js
|
87 | 'fixed-line'
|
88 | 'fixed-line-or-mobile'
|
89 | 'mobile'
|
90 | 'pager'
|
91 | 'personal-number'
|
92 | 'premium-rate'
|
93 | 'shared-cost'
|
94 | 'toll-free'
|
95 | 'uan'
|
96 | 'voip'
|
97 | 'unknown'
|
98 | ```
|
99 |
|
100 | ### Phone number possibilities
|
101 |
|
102 | ```js
|
103 | 'is-possible'
|
104 | 'invalid-country-code'
|
105 | 'too-long'
|
106 | 'too-short'
|
107 | 'unknown'
|
108 | ```
|
109 |
|
110 | ### Phone number formats
|
111 |
|
112 | ```js
|
113 | 'international'
|
114 | 'national'
|
115 | 'e164'
|
116 | 'rfc3966'
|
117 | 'significant'
|
118 | ```
|
119 |
|
120 | ## API functions
|
121 |
|
122 | ### Library
|
123 | ```js
|
124 | var PhoneNumber = require( 'awesome-phonenumber' );
|
125 | ```
|
126 |
|
127 | ### Country codes
|
128 |
|
129 | There are conversion functions between the 2-character ISO 3166-1 region codes (e.g. 'SE' for Sweden) and the corresponding country calling codes.
|
130 |
|
131 | ```js
|
132 | PhoneNumber.getCountryCodeForRegionCode( regionCode ); // -> countryCode
|
133 | PhoneNumber.getRegionCodeForCountryCode( countryCode ); // -> regionCode
|
134 | ```
|
135 |
|
136 | #### Example
|
137 |
|
138 | ```js
|
139 | PhoneNumber.getCountryCodeForRegionCode( 'SE' ); // -> 46
|
140 | PhoneNumber.getRegionCodeForCountryCode( 46 ); // -> 'SE'
|
141 | ```
|
142 |
|
143 | ### Supported calling codes
|
144 |
|
145 | ```js
|
146 | PhoneNumber.getSupportedCallingCodes( ); // -> [ calling codes... ]
|
147 | ```
|
148 |
|
149 | ### Supported region codes
|
150 |
|
151 | ```js
|
152 | PhoneNumber.getSupportedRegionCodes( ); // -> [ region codes... ]
|
153 | ```
|
154 |
|
155 | ### Phone numbers
|
156 |
|
157 | An instance of the `PhoneNumber` class will be created even if `PhoneNumber` is called as a function.
|
158 |
|
159 | ```js
|
160 | var pn = PhoneNumber( number, regionCode );
|
161 | // is the same as
|
162 | var pn = new PhoneNumber( number, regionCode );
|
163 | ```
|
164 |
|
165 | PhoneNumber objects can also be created using the `getExample( regionCode[, type ] )` function, see section [Example phone numbers for country](#example) below.
|
166 |
|
167 | ```js
|
168 | pn.toJSON( ); // -> json blob as seen in "Basic usage" above
|
169 | pn.isValid( ); // -> Boolean
|
170 | pn.isPossible( ); // -> Boolean
|
171 | pn.getType( ); // -> Any of the "Phone number types" defined above
|
172 | pn.isMobile( ); // -> true if type is 'mobile' or 'fixed-line-or-mobile'
|
173 | pn.isFixedLine( ); // -> true if type is 'fixed-line' or 'fixed-line-or-mobile'
|
174 | pn.getNumber( [ format ] ); // -> Formatted number, see "Basic usage" for examples
|
175 |
|
176 | // Returns the number formatted to how to dial it from another region.
|
177 | pn.getNumberFrom( fromRegionCode );
|
178 | ```
|
179 |
|
180 | #### Example
|
181 |
|
182 | ```js
|
183 | // Calling the Swedish number 0707123456 from Japan:
|
184 | PhoneNumber( '0707123456', 'SE' ).getNumberFrom( 'JP' ); // '010 46 70 712 34 56'
|
185 | ```
|
186 |
|
187 | ### <a name="example"></a>Example phone numbers for country
|
188 |
|
189 | Sometimes you want to display a formatted example phone number for a certain country (and maybe also a certain type of phone number). The `getExample` function is used for this.
|
190 |
|
191 | ```js
|
192 | PhoneNumber.getExample( regionCode[, phoneNumberType] ); // PhoneNumber object
|
193 | ```
|
194 |
|
195 | The `phoneNumberType` is any of the [types defined above](#phone-number-types).
|
196 |
|
197 | #### Example
|
198 |
|
199 | ```js
|
200 | PhoneNumber.getExample( 'SE' ).getNumber( ); // '+468123456'
|
201 | PhoneNumber.getExample( 'SE', 'mobile' ).getNumber( ); // '+46701234567'
|
202 | PhoneNumber.getExample( 'SE', 'mobile' ).getNumber( 'national' ); // '070 123 45 67'
|
203 | ```
|
204 |
|
205 | ### As-you-type formatting
|
206 |
|
207 | You can use an `AsYouType` class to format a phone number as it is being typed. An instance of this class is retrieved by `var ayt = PhoneNumber.getAsYouType( regionCode )`, and has the following methods:
|
208 |
|
209 | ```js
|
210 | // Add a character to the end of the number
|
211 | ayt.addChar( nextChar );
|
212 |
|
213 | // Get the current formatted number
|
214 | ayt.number( );
|
215 |
|
216 | // Remove the last character
|
217 | ayt.removeChar( );
|
218 |
|
219 | // Replace the whole number with a new number (or an empty number if null)
|
220 | ayt.reset( [ number ] );
|
221 |
|
222 | // Get a PhoneNumber object representing the current number
|
223 | ayt.getPhoneNumber( );
|
224 | ```
|
225 |
|
226 | All the functions above except `getPhoneNumber( )` return the current formatted number (as a String of course, as it may include spaces and other characters).
|
227 |
|
228 | #### Example
|
229 |
|
230 | ```js
|
231 | var ayt = PhoneNumber.getAsYouType( 'SE' );
|
232 | ayt.addChar( '0' ); // -> '0'
|
233 | ayt.addChar( '7' ); // -> '07'
|
234 | ayt.addChar( '0' ); // -> '070'
|
235 | ayt.addChar( '7' ); // -> '070 7'
|
236 | ayt.addChar( '1' ); // -> '070 71'
|
237 | ayt.addChar( '2' ); // -> '070 712'
|
238 | ayt.addChar( '3' ); // -> '070 712 3'
|
239 | ayt.addChar( '4' ); // -> '070 712 34'
|
240 | ayt.addChar( '5' ); // -> '070 712 34 5'
|
241 | ayt.addChar( '6' ); // -> '070 712 34 56'
|
242 | ayt.removeChar( ); // -> '070 712 34 5'
|
243 | ayt.addChar( '7' ); // -> '070 712 34 57'
|
244 | ```
|
245 |
|
246 | [npm-image]: https://img.shields.io/npm/v/awesome-phonenumber.svg
|
247 | [npm-url]: https://npmjs.org/package/awesome-phonenumber
|
248 | [downloads-image]: https://img.shields.io/npm/dm/awesome-phonenumber.svg
|
249 | [build-image]: https://img.shields.io/github/workflow/status/grantila/awesome-phonenumber/Master.svg
|
250 | [build-url]: https://github.com/grantila/awesome-phonenumber/actions?query=workflow%3AMaster
|
251 | [lgtm-image]: https://img.shields.io/lgtm/grade/javascript/g/grantila/awesome-phonenumber.svg?logo=lgtm&logoWidth=18
|
252 | [lgtm-url]: https://lgtm.com/projects/g/grantila/awesome-phonenumber/context:javascript
|
253 | [packagephobia-image]: https://packagephobia.now.sh/badge?p=awesome-phonenumber
|
254 | [packagephobia-url]: https://packagephobia.now.sh/result?p=awesome-phonenumber
|