UNPKG

8.89 kBMarkdownView Raw
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
10This 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
12TypeScript typings are provided within the package.
13
14Uses libphonenumber v8.12.28
15
16## Comparison with other libraries
17
18Since 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
20Among 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
22Loading 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
24A test program loading a library, then parsing a phone number is called 100 times for each library, the mean values are:
25
26Action | awesome-phonenumber (7.5.2) | google-libphonenumber (7.6.1) | node-phonenumber (7.5.2)
27------------------------- | ------------------- | --------------------- | ----------------
28Load library first time | 20.84 ms | 60.99ms | 99.27 ms
29Parse first phone number | 5.79 ms | 6.51 ms | 8.15 ms
30Parse second phone number | 0.33 ms | 0.67 ms | 0.80 ms
31Increased memory usage | 7.3 M | 13.8 M | 22.5 M
32node_modules size | 248 K | 436 K | 57 M
33node_modules files | 7 | 7 | 4525
34time npm install | 667 ms | 700 ms | 4077 ms
35
36## Basic usage
37```js
38var PhoneNumber = require( 'awesome-phonenumber' );
39
40var pn = new PhoneNumber( '0707123456', 'SE' );
41pn.isValid( ); // -> true
42pn.isMobile( ); // -> true
43pn.canBeInternationallyDialled( ); // -> true
44pn.getNumber( ); // -> '+46707123456'
45pn.getNumber( 'e164' ); // -> '+46707123456' (default)
46pn.getNumber( 'international' ); // -> '+46 70 712 34 56'
47pn.getNumber( 'national' ); // -> '070-712 34 56'
48pn.getNumber( 'rfc3966' ); // -> 'tel:+46-70-712-34-56'
49pn.getNumber( 'significant' ); // -> '707123456'
50pn.getRegionCode( ); // -> 'SE'
51pn.getCountryCode( ); // -> 46
52
53pn.toJSON( ); // -> json blob, so that:
54JSON.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
75When constructed with a phone number on `e164` format (i.e. prefixed with a `+`), awesome-phonenumber will auto-detect the country:
76
77```js
78PhoneNumber( '+46707123456' ).getRegionCode( ); // -> 'SE'
79```
80
81## API types
82
83The 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
124var PhoneNumber = require( 'awesome-phonenumber' );
125```
126
127### Country codes
128
129There 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
132PhoneNumber.getCountryCodeForRegionCode( regionCode ); // -> countryCode
133PhoneNumber.getRegionCodeForCountryCode( countryCode ); // -> regionCode
134```
135
136#### Example
137
138```js
139PhoneNumber.getCountryCodeForRegionCode( 'SE' ); // -> 46
140PhoneNumber.getRegionCodeForCountryCode( 46 ); // -> 'SE'
141```
142
143### Supported calling codes
144
145```js
146PhoneNumber.getSupportedCallingCodes( ); // -> [ calling codes... ]
147```
148
149### Supported region codes
150
151```js
152PhoneNumber.getSupportedRegionCodes( ); // -> [ region codes... ]
153```
154
155### Phone numbers
156
157An instance of the `PhoneNumber` class will be created even if `PhoneNumber` is called as a function.
158
159```js
160var pn = PhoneNumber( number, regionCode );
161// is the same as
162var pn = new PhoneNumber( number, regionCode );
163```
164
165PhoneNumber objects can also be created using the `getExample( regionCode[, type ] )` function, see section [Example phone numbers for country](#example) below.
166
167```js
168pn.toJSON( ); // -> json blob as seen in "Basic usage" above
169pn.isValid( ); // -> Boolean
170pn.isPossible( ); // -> Boolean
171pn.getType( ); // -> Any of the "Phone number types" defined above
172pn.isMobile( ); // -> true if type is 'mobile' or 'fixed-line-or-mobile'
173pn.isFixedLine( ); // -> true if type is 'fixed-line' or 'fixed-line-or-mobile'
174pn.getNumber( [ format ] ); // -> Formatted number, see "Basic usage" for examples
175
176// Returns the number formatted to how to dial it from another region.
177pn.getNumberFrom( fromRegionCode );
178```
179
180#### Example
181
182```js
183// Calling the Swedish number 0707123456 from Japan:
184PhoneNumber( '0707123456', 'SE' ).getNumberFrom( 'JP' ); // '010 46 70 712 34 56'
185```
186
187### <a name="example"></a>Example phone numbers for country
188
189Sometimes 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
192PhoneNumber.getExample( regionCode[, phoneNumberType] ); // PhoneNumber object
193```
194
195The `phoneNumberType` is any of the [types defined above](#phone-number-types).
196
197#### Example
198
199```js
200PhoneNumber.getExample( 'SE' ).getNumber( ); // '+468123456'
201PhoneNumber.getExample( 'SE', 'mobile' ).getNumber( ); // '+46701234567'
202PhoneNumber.getExample( 'SE', 'mobile' ).getNumber( 'national' ); // '070 123 45 67'
203```
204
205### As-you-type formatting
206
207You 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
211ayt.addChar( nextChar );
212
213// Get the current formatted number
214ayt.number( );
215
216// Remove the last character
217ayt.removeChar( );
218
219// Replace the whole number with a new number (or an empty number if null)
220ayt.reset( [ number ] );
221
222// Get a PhoneNumber object representing the current number
223ayt.getPhoneNumber( );
224```
225
226All 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
231var ayt = PhoneNumber.getAsYouType( 'SE' );
232ayt.addChar( '0' ); // -> '0'
233ayt.addChar( '7' ); // -> '07'
234ayt.addChar( '0' ); // -> '070'
235ayt.addChar( '7' ); // -> '070 7'
236ayt.addChar( '1' ); // -> '070 71'
237ayt.addChar( '2' ); // -> '070 712'
238ayt.addChar( '3' ); // -> '070 712 3'
239ayt.addChar( '4' ); // -> '070 712 34'
240ayt.addChar( '5' ); // -> '070 712 34 5'
241ayt.addChar( '6' ); // -> '070 712 34 56'
242ayt.removeChar( ); // -> '070 712 34 5'
243ayt.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