libphonenumber-js 1.13.0 → 1.13.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.
- package/README.md +114 -80
- package/build/isPossible.js +16 -15
- package/build/isPossible.js.map +1 -1
- package/bundle/libphonenumber-js.min.js.map +1 -1
- package/bundle/libphonenumber-max.js.map +1 -1
- package/bundle/libphonenumber-min.js.map +1 -1
- package/bundle/libphonenumber-mobile.js.map +1 -1
- package/es6/isPossible.js +16 -15
- package/es6/isPossible.js.map +1 -1
- package/es6-modern/isPossible.js +16 -15
- package/es6-modern/isPossible.js.map +1 -1
- package/package.json +1 -1
- package/source/isPossible.js +16 -15
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
[](https://www.npmjs.com/package/libphonenumber-js)
|
|
5
5
|
[](https://coveralls.io/r/catamphetamine/libphonenumber-js?branch=master)
|
|
6
6
|
|
|
7
|
-
A simpler and smaller rewrite of Google Android's [`libphonenumber`](https://github.com/google/libphonenumber/) library in javascript/
|
|
7
|
+
A simpler and smaller rewrite of Google Android's [`libphonenumber`](https://github.com/google/libphonenumber/) library in javascript/typescript.
|
|
8
8
|
|
|
9
9
|
Parse and format personal phone numbers.
|
|
10
10
|
|
|
@@ -12,11 +12,11 @@ Parse and format personal phone numbers.
|
|
|
12
12
|
|
|
13
13
|
If you’re trying to build a React component with it, take a look at [`react-phone-number-input`](https://www.npmjs.com/package/react-phone-number-input).
|
|
14
14
|
|
|
15
|
-
##
|
|
15
|
+
## libphonenumber
|
|
16
16
|
|
|
17
|
-
Google's [`libphonenumber`](https://github.com/googlei18n/libphonenumber) is an ultimate phone number formatting and parsing library developed by Google for [Android](https://en.wikipedia.org/wiki/Android_(operating_system)) phones. It is written in C++ and Java, and, while it has an [official autogenerated javascript port](https://github.com/googlei18n/libphonenumber/tree/master/javascript), that port is tightly coupled to Google's `closure` javascript framework, and
|
|
17
|
+
Google's [`libphonenumber`](https://github.com/googlei18n/libphonenumber) is an ultimate phone number formatting and parsing library developed by Google for [Android](https://en.wikipedia.org/wiki/Android_(operating_system)) phones. It is written in C++ and Java, and, while it has an [official autogenerated javascript port](https://github.com/googlei18n/libphonenumber/tree/master/javascript), that port is tightly coupled to Google's `closure` javascript framework, and when compiled into a [bundle](https://github.com/ruimarinho/google-libphonenumber), the total weight becomes about 550 kB (350 kB code + 200 kB metadata).
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
`libphonenumber-js` works around the bundle size issue by stripping down both code and metadata: the code was rewritten in javascript/typescript from scratch, and a developer can now choose which parts of metadata to include.
|
|
20
20
|
|
|
21
21
|
<!-- That's what `libphonenumber-js` is about: I started off from scratch, decyphering Google's code and figuring out how this whole machinery works, through trial and error, with several years of hacking around, receiving bug reports from other developers — eventually, it has reached a state when it can be assumed production-ready. -->
|
|
22
22
|
|
|
@@ -24,39 +24,44 @@ With many websites today asking for user's phone number, the internet could bene
|
|
|
24
24
|
One part of me was curious about how all this phone number parsing and formatting machinery worked, and another part of me was curious if there was a way to reduce those 530 kilobytes to something more reasonable while also getting rid of all the unnecessary bulk and rewriting it all in pure javascript. The resulting library does everything a modern web application needs while maintaining a much smaller size of about 130 kilobytes.
|
|
25
25
|
-->
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
<details>
|
|
28
|
+
|
|
29
|
+
<summary>Here's a full comparison to Google's <code>libphonenumber</code> for those who're interested.</summary>
|
|
28
30
|
|
|
29
|
-
|
|
31
|
+
######
|
|
30
32
|
|
|
31
|
-
|
|
33
|
+
* Smaller footprint: `145 kB` (65 kB code + 80 kB sufficient metadata) vs the original Google's `550 kB` (350 kB code + 200 kB full metadata).
|
|
32
34
|
|
|
33
|
-
|
|
35
|
+
* Comes with TypeScript definitions.
|
|
34
36
|
|
|
35
|
-
|
|
37
|
+
* Can search for phone numbers in text. Google's autogenerated javascript port doesn't provide such feature for some reason.
|
|
36
38
|
|
|
37
|
-
|
|
39
|
+
* Focuses on parsing and formatting personal phone numbers while skipping any other "special" cases like:
|
|
38
40
|
|
|
39
|
-
|
|
41
|
+
* Emergency phone numbers like `911`.
|
|
40
42
|
|
|
41
|
-
|
|
43
|
+
* ["Short codes"](https://www.tatango.com/blog/sms-short-codes-what-every-business-needs-to-know/) — short SMS-only numbers like `12345`.
|
|
42
44
|
|
|
43
|
-
|
|
45
|
+
* Numbers starting with an `*`. For example, `*555` is used in New Zeland to report non-urgent traffic incidents. Or, in Israel, certain advertising numbers start with a `*`.
|
|
44
46
|
|
|
45
|
-
|
|
47
|
+
* Australian [`13`-smart](https://github.com/catamphetamine/libphonenumber-js/issues/400) numbers, which are a "catchy" "short" form of regular "landline" numbers and are mainly used in [advertisement](https://www.youtube.com/watch?v=Y0HchzE-7rM).
|
|
46
48
|
|
|
47
|
-
|
|
49
|
+
* Alphabetic phone numbers like `1-800-GOT-MILK`. People don't input their phone numbers like that. It was only used in advertisement in the days of push-button telephones.
|
|
48
50
|
|
|
49
|
-
|
|
51
|
+
* "Two-in-one" phone numbers with "combined" extensions like `(530) 583-6985 x302/x2303`. Phone numbers like that actually represent two separate phone numbers, so it's not clear which one to pick or how to return both at the same time.
|
|
50
52
|
|
|
51
|
-
*
|
|
53
|
+
* Local numbers with the "area code" omitted. For example, when dialing phone numbers within the same "area", people sometimes skip the "area code", and dial, say, just `456-789` instead of proper `(123) 456-789`. This all is considered a relic of the past. In the modern world, there're no "local areas" and anyone could call everyone else around the world.
|
|
52
54
|
|
|
53
|
-
|
|
55
|
+
* Doesn't provide "geolocation" feature when it can tell a city by a phone number.
|
|
54
56
|
|
|
55
|
-
|
|
57
|
+
* Doesn't use hyphens or brackets when formatting phone numbers in international format. Instead, whitespace is used. The rationale is that brackets aren't relevant in international context because there're no "local areas", and hyphens aren't used because whitespace just looks cleaner.
|
|
56
58
|
|
|
57
|
-
|
|
59
|
+
* Doesn't set `.country` to `"001"` when parsing ["non-geographic"](#non-geographic) phone numbers, like mobile satellite communications services. Instead, `.country` is set to `undefined` in those cases, and instead a developer can call `.isNonGeographic()` method of the `PhoneNumber` instance to find out whether the parsed phone number is a "non-geographic" one.
|
|
58
60
|
|
|
59
|
-
|
|
61
|
+
* Doesn't provide the equivalent of `libphonenumber`'s `formatNumberForMobileDialing()` function that formats a number for dialing from a mobile phone within the same country. This feature may be required for dialing local numbers from a mobile phone in some countries like Brazil or Colombia where they require adding ["carrier codes"](https://www.bandwidth.com/glossary/carrier-identification-code-cic/) when making such calls. Since `libphonenumber-js` is not a dialing library (we're not Android phone operaing system), it doesn't prepend any "carrier codes" when formatting such phone numbers, though it does parse such "carrier codes" correctly.
|
|
62
|
+
|
|
63
|
+
* Fixed a small [bug](https://issuetracker.google.com/issues/335892662) when Canadian numbers `+1310xxxx` wheren't considered possible.
|
|
64
|
+
</details>
|
|
60
65
|
|
|
61
66
|
<!-- (Australia, Bolivia, Brazil, China, Colombia, Croatia, Faroe Islands, South Korea, Liechtenstein, Luxembourg, Venezuela) -->
|
|
62
67
|
|
|
@@ -76,16 +81,8 @@ One part of me was curious about how all this phone number parsing and formattin
|
|
|
76
81
|
|
|
77
82
|
## Install
|
|
78
83
|
|
|
79
|
-
via [npm](https://npmjs.org/)
|
|
80
|
-
|
|
81
84
|
```sh
|
|
82
|
-
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
via [yarn](https://yarnpkg.com)
|
|
86
|
-
|
|
87
|
-
```sh
|
|
88
|
-
$ yarn add libphonenumber-js
|
|
85
|
+
npm install libphonenumber-js --save
|
|
89
86
|
```
|
|
90
87
|
|
|
91
88
|
Alternatively, one could include it on a web page [directly](#cdn) via a `<script/>` tag.
|
|
@@ -161,7 +158,7 @@ if (phoneNumber) {
|
|
|
161
158
|
phoneNumber.number === '+78005553535'
|
|
162
159
|
phoneNumber.isPossible() === true
|
|
163
160
|
phoneNumber.isValid() === true
|
|
164
|
-
//
|
|
161
|
+
// `.getType()` only works when import from 'libphonenumber-js/max'
|
|
165
162
|
phoneNumber.getType() === 'TOLL_FREE'
|
|
166
163
|
}
|
|
167
164
|
```
|
|
@@ -223,16 +220,16 @@ formatNumber({ country: 'US', phone: '2133734253' }, 'NATIONAL')
|
|
|
223
220
|
|
|
224
221
|
### "As You Type" formatter
|
|
225
222
|
|
|
226
|
-
Parses and formats
|
|
223
|
+
Parses and formats a complete or incomplete phone number.
|
|
227
224
|
|
|
228
225
|
```js
|
|
229
226
|
import { AsYouType } from 'libphonenumber-js'
|
|
230
227
|
|
|
231
|
-
|
|
232
|
-
|
|
228
|
+
// Country not specified — use international format
|
|
229
|
+
new AsYouType().input('+12133734') === '+1 213 373 4'
|
|
233
230
|
|
|
234
|
-
|
|
235
|
-
|
|
231
|
+
// Country specified — use national format
|
|
232
|
+
new AsYouType('US').input('2133734') === '(213) 373-4'
|
|
236
233
|
```
|
|
237
234
|
|
|
238
235
|
### Validate phone number
|
|
@@ -249,13 +246,17 @@ import {
|
|
|
249
246
|
isPossiblePhoneNumber('8 (800) 555-35-35', 'RU') === true
|
|
250
247
|
isValidPhoneNumber('8 (800) 555-35-35', 'RU') === true
|
|
251
248
|
|
|
252
|
-
validatePhoneNumberLength('8 (800) 555', 'RU') === 'TOO_SHORT'
|
|
253
|
-
validatePhoneNumberLength('8 (800) 555-35-35', 'RU') === undefined // Length is valid
|
|
249
|
+
validatePhoneNumberLength('8 (800) 555', 'RU') === 'TOO_SHORT' // Length is invalid
|
|
250
|
+
validatePhoneNumberLength('8 (800) 555-35-35', 'RU') === undefined // Length is valid
|
|
254
251
|
```
|
|
255
252
|
|
|
256
|
-
`isPossiblePhoneNumber()`
|
|
253
|
+
`isPossiblePhoneNumber()` validates phone number length.
|
|
254
|
+
|
|
255
|
+
`isValidPhoneNumber()` validates both phone number length and phone number digits.
|
|
257
256
|
|
|
258
|
-
`
|
|
257
|
+
It may seem counter-intuitive but when [choosing between the two](#using-phone-number-validation-feature), I'd personally prefer `isPossiblePhoneNumber()` because its strength is in its weakness. `isValidPhoneNumber()` is a double-edged sword in terms of how strict it is, and when not updated regularly, it could get stale over time and start rejecting freshly-assigned phone number ranges.
|
|
258
|
+
|
|
259
|
+
As for `validatePhoneNumberLength()`, is just a more detailed version of `isPossiblePhoneNumber()` for those who need it — when the phone number length is invalid, instead of returning just `false`, it returns the actual reason why the phone number length is incorrect: `TOO_SHORT`, `TOO_LONG`, etc.
|
|
259
260
|
|
|
260
261
|
### Full-text search
|
|
261
262
|
|
|
@@ -271,37 +272,37 @@ import { findPhoneNumbersInText } from 'libphonenumber-js'
|
|
|
271
272
|
findPhoneNumbersInText(`
|
|
272
273
|
For tech support call +7 (800) 555-35-35 internationally
|
|
273
274
|
or reach a local US branch at (213) 373-4253 ext. 1234.
|
|
274
|
-
`, 'US')
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
275
|
+
`, 'US') ===
|
|
276
|
+
[
|
|
277
|
+
{
|
|
278
|
+
number: PhoneNumber {
|
|
279
|
+
country: 'RU',
|
|
280
|
+
countryCallingCode: '7',
|
|
281
|
+
number: '+78005553535',
|
|
282
|
+
nationalNumber: '8005553535'
|
|
283
|
+
},
|
|
284
|
+
startsAt: 22,
|
|
285
|
+
endsAt: 40
|
|
286
|
+
},
|
|
287
|
+
{
|
|
288
|
+
number: PhoneNumber {
|
|
289
|
+
country: 'US',
|
|
290
|
+
countryCallingCode: '1',
|
|
291
|
+
number: '+12133734253',
|
|
292
|
+
nationalNumber: '2133734253',
|
|
293
|
+
ext: '1234'
|
|
294
|
+
},
|
|
295
|
+
startsAt: 86,
|
|
296
|
+
endsAt: 110
|
|
297
|
+
}
|
|
298
|
+
]
|
|
298
299
|
```
|
|
299
300
|
|
|
300
301
|
## "min" vs "max" vs "mobile" vs "core"
|
|
301
302
|
|
|
302
|
-
This
|
|
303
|
+
This package lets a developer choose from different "metadata" sets, where a "metadata" set is a complete list of phone number parsing and formatting rules for all possible countries.
|
|
303
304
|
|
|
304
|
-
As one may guess, the complete list of those rules is huge, so this
|
|
305
|
+
As one may guess, the complete list of those rules is huge, so this package provides a way to optimize the bundle size by choosing between `max`, `min`, `mobile` or "custom" metadata, depending on the project's needs.
|
|
305
306
|
|
|
306
307
|
* `min` — (default) The smallest metadata set
|
|
307
308
|
* Is about `80 kilobytes` in size (`libphonenumber-js/metadata.min.json` file)
|
|
@@ -346,11 +347,13 @@ As for "custom" metadata, it could be used in those rare cases when not all coun
|
|
|
346
347
|
|
|
347
348
|
## Definitions
|
|
348
349
|
|
|
350
|
+
This package introduces a few new terms in order describe its behavior more precisely.
|
|
351
|
+
|
|
349
352
|
### National (Significant) Number
|
|
350
353
|
|
|
351
|
-
"National (significant) number" is all national phone number digits
|
|
354
|
+
"National (significant) number" is all national phone number digits excluding the "national prefix". Examples:
|
|
352
355
|
* International number: `+1 213 373 4253`. Country: `"US"`. National number: `(213) 373-4253`. National (significant) number: `213 373 4253`
|
|
353
|
-
* Inetrnational number: `+33 1 45 45 32 45`. Country: `"FR"`. National number: `01 45 45 32 45`. Notice the `0` at the start of the national number — it's because in France they [add](https://en.wikipedia.org/wiki/Telephone_numbers_in_France)
|
|
356
|
+
* Inetrnational number: `+33 1 45 45 32 45`. Country: `"FR"`. National number: `01 45 45 32 45`. Notice the `0` at the start of the national number — it's because in France they [add](https://en.wikipedia.org/wiki/Telephone_numbers_in_France) it when writing phone numbers in national format. National (significant) number: `1 45 45 32 45` (doesn't include the "national prefix")
|
|
354
357
|
|
|
355
358
|
### Country Calling Code
|
|
356
359
|
|
|
@@ -362,7 +365,12 @@ In some cases, multiple countries could share the same "country calling code". F
|
|
|
362
365
|
|
|
363
366
|
### Non-Geographic
|
|
364
367
|
|
|
365
|
-
|
|
368
|
+
Some "country calling codes" don't belong to any given country or territory and are by-design international. Such phone numbers are called "non-geographic", and their `country` property is `undefined`.
|
|
369
|
+
|
|
370
|
+
<details>
|
|
371
|
+
<summary>Here's a full list of "non-geographic" calling codes.</summary>
|
|
372
|
+
|
|
373
|
+
######
|
|
366
374
|
|
|
367
375
|
* `+800` — [Universal International Toll Free Number](https://en.wikipedia.org/wiki/Toll-free_telephone_number)
|
|
368
376
|
* `+808` — [Universal International Shared Cost Number](https://en.wikipedia.org/wiki/Shared-cost_service)
|
|
@@ -372,8 +380,7 @@ The following "calling codes" don't belong to any given country or territory and
|
|
|
372
380
|
* `+882` and `+883` — [International Networks](https://en.wikipedia.org/wiki/International_Networks_(country_code))
|
|
373
381
|
* `+888` — [United Nations Office for the Coordination of Humanitarian Affairs](https://en.wikipedia.org/wiki/United_Nations_Office_for_the_Coordination_of_Humanitarian_Affairs#International_Dialing_Code)
|
|
374
382
|
* `+979` — [International Premium Rate Service](https://en.wikipedia.org/wiki/International_Premium_Rate_Service)
|
|
375
|
-
|
|
376
|
-
Such phone numbers are called "non-geographic", and their `country` is `undefined`.
|
|
383
|
+
</details>
|
|
377
384
|
|
|
378
385
|
### Country Code
|
|
379
386
|
|
|
@@ -383,7 +390,7 @@ However, this library uses the term "country code" rather broadly and it include
|
|
|
383
390
|
|
|
384
391
|
For that reason, a developer should use a "country code" returned from this library with caution in an application that only expects the official ISO "country codes" to exist. For example, such application will likely not have a label or a flag for such an unofficial "country code".
|
|
385
392
|
|
|
386
|
-
A developer could manually transform any unofficial "country code" returned from this library to a most-suitable official one.
|
|
393
|
+
A developer could manually transform any unofficial "country code" returned from this library to a most-suitable official one. Because there's no obvious answer in such case, it's up to developers themselves to decide which country name or flag to show.
|
|
387
394
|
|
|
388
395
|
To get a list of all supported "country codes", both official and unofficial, use [`getCountries()`](#getcountries-string) function.
|
|
389
396
|
|
|
@@ -391,7 +398,17 @@ To check if a certain two-letter "country code" is supported by this library, us
|
|
|
391
398
|
|
|
392
399
|
### Country Code (additions)
|
|
393
400
|
|
|
394
|
-
Here's a list of the unofficial "country codes"
|
|
401
|
+
Here's a list of the unofficial "country codes". These codes are used for certain "regions" that're usually a subject of a [territorial dispute](https://en.wikipedia.org/wiki/Territorial_dispute).
|
|
402
|
+
|
|
403
|
+
* `AC` — [Ascension Island](https://en.wikipedia.org/wiki/Ascension_Island)
|
|
404
|
+
* `TA` — [Tristan da Cunha](https://en.wikipedia.org/wiki/Tristan_da_Cunha)
|
|
405
|
+
* `SH` — [Saint Helena](https://en.wikipedia.org/wiki/Saint_Helena)
|
|
406
|
+
* `XK` — [Kosovo](https://en.wikipedia.org/wiki/Kosovo)
|
|
407
|
+
|
|
408
|
+
<details>
|
|
409
|
+
<summary>Here're some more details on each one of them for those who're interested.</summary>
|
|
410
|
+
|
|
411
|
+
######
|
|
395
412
|
|
|
396
413
|
* `AC` — [Ascension Island](https://en.wikipedia.org/wiki/Ascension_Island)
|
|
397
414
|
* (history) The region of Ascension Island is officially part of [Saint Helena, Ascension and Tristan da Cunha
|
|
@@ -418,25 +435,42 @@ Here's a list of the unofficial "country codes" — these codes are used for cer
|
|
|
418
435
|
* An unofficial code `XK` is used for Kosovo region in Google's `libphonenumber`.
|
|
419
436
|
* `XK` is a ["user-assigned"](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#User-assigned_code_elements) code in the ISO-3166-1 standard meaning that it can be freely assigned to any region by any application or organization for its own purposes.
|
|
420
437
|
* The last official ISO-3166-2 ["subdivision code"](https://en.wikipedia.org/wiki/ISO_3166-2) assigned to Kosovo region [is](https://en.wikipedia.org/wiki/Autonomous_Province_of_Kosovo_and_Metohija) `RS-KM`.
|
|
438
|
+
</details>
|
|
439
|
+
|
|
440
|
+
######
|
|
441
|
+
|
|
442
|
+
There's also a list of territories that have a disputed status of an independent state. For obvious reasons, they dropped the use of their original parent territory's telephone numbering plan and instead joined another state's telephone numbering plan.
|
|
421
443
|
|
|
422
|
-
|
|
423
|
-
|
|
444
|
+
* [Abkhazia](https://en.wikipedia.org/wiki/Abkhazia) — used to be part of [Georgia](https://en.wikipedia.org/wiki/Georgia_(country)) (`GE`) but currently uses the telephone numbering plan of [Russia](https://en.wikipedia.org/wiki/Russia) (`RU`)
|
|
445
|
+
* [South Ossetia](https://en.wikipedia.org/wiki/South_Ossetia) — used to be part of [Georgia](https://en.wikipedia.org/wiki/Georgia_(country)) (`GE`) but currently uses the telephone numbering plan of [Russia](https://en.wikipedia.org/wiki/Russia) (`RU`)
|
|
446
|
+
* [Northern Cyprus](https://en.wikipedia.org/wiki/Northern_Cyprus) — used to be part of [Cyprus](https://en.wikipedia.org/wiki/Cyprus) (`CY`) but currently uses the telephone numbering plan of [Turkey](https://en.wikipedia.org/wiki/Turkey) (`TR`)
|
|
447
|
+
|
|
448
|
+
<details>
|
|
449
|
+
<summary>Here're some more details on each one of them for those who're interested.</summary>
|
|
450
|
+
|
|
451
|
+
######
|
|
452
|
+
|
|
453
|
+
These territories are currently not implemented in this library but they could potentially be implemented in some future:
|
|
454
|
+
* [Abkhazia](https://en.wikipedia.org/wiki/Abkhazia)
|
|
424
455
|
* (history) The region of Abkhazia separated from [Georgia](https://en.wikipedia.org/wiki/Georgia_(country)) (`GE`) during a civil war in 1992-1994. The government of Abkhazia officially (unilaterally) declared independence from Georgia in 1994 when the civil war was put to an end. The first state to officially recognize Abkhazia's independence was Russia in 2008.
|
|
425
456
|
* Abkhazia currently shares a country calling code of `+7` with [Russia](https://en.wikipedia.org/wiki/Russia) (`RU`) where it has [unique prefixes](https://en.wikipedia.org/wiki/Telephone_numbers_in_Russia#Codes_assigned_to_Abkhazia) of `840` and `940`, so the "country code" is currently parsed as `RU`.
|
|
426
457
|
* For the purpose of differentiating Abkhazia phone numbers from Russia phone numbers, an unofficial code like `XA` could be used for it in Google's `libphonenumber`.
|
|
427
458
|
* `XA` is a ["user-assigned"](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#User-assigned_code_elements) code in the ISO-3166-1 standard meaning that it can be freely assigned to any region by any application or organization for its own purposes.
|
|
428
459
|
* The last official ISO-3166-2 ["subdivision code"](https://en.wikipedia.org/wiki/ISO_3166-2) assigned to Abkhazia region [is](https://en.wikipedia.org/wiki/ISO_3166-2:GE) `GE-AB`.
|
|
429
|
-
|
|
460
|
+
|
|
461
|
+
* [South Ossetia](https://en.wikipedia.org/wiki/South_Ossetia)
|
|
430
462
|
* (history) The region of South Ossetia separated from [Georgia](https://en.wikipedia.org/wiki/Georgia_(country)) (`GE`) during a civil war 1991–1992. The government of South Ossetia officially (unilaterally) declared independence from Georgia in 1992 when the civil war was put to an end. The first state to officially recognize South Ossetia's independence was Russia in 2008.
|
|
431
463
|
* South Ossetia currently shares a country calling code of `+7` with [Russia](https://en.wikipedia.org/wiki/Russia) (`RU`) where it has [unique prefixes](https://en.wikipedia.org/wiki/Telephone_numbers_in_Russia#Codes_assigned_to_South_Ossetia) of `850` and `929`, so the "country code" is currently parsed as `RU`.
|
|
432
464
|
* For the purpose of differentiating South Ossetia phone numbers from Russia phone numbers, an unofficial code like `XO` could be used for it in Google's `libphonenumber`.
|
|
433
465
|
* `XO` is a ["user-assigned"](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#User-assigned_code_elements) code in the ISO-3166-1 standard meaning that it can be freely assigned to any region by any application or organization for its own purposes.
|
|
434
466
|
* South Ossetia region has [not](https://en.wikipedia.org/wiki/ISO_3166-2:GE) ever been assigned any official ISO-3166-2 ["subdivision code"](https://en.wikipedia.org/wiki/ISO_3166-2).
|
|
435
|
-
|
|
467
|
+
|
|
468
|
+
* [Northern Cyprus](https://en.wikipedia.org/wiki/Northern_Cyprus)
|
|
436
469
|
* (history) The region of Northern Cyprus separated from [Cyprus](https://en.wikipedia.org/wiki/Cyprus) (`CY`) during [Turkey](https://en.wikipedia.org/wiki/Turkey)'s military invasion in 1974. The government of Northern Cyprus officially (unilaterally) declared independence from Cyprus in 1983. The first state to officially recognize Northern Cyprus' independence was Turkey in 1983.
|
|
437
470
|
* Northern Cyprus currently shares a country calling code of `+90` with Turkey (`TR`) where it has [unique prefixes](https://en.wikipedia.org/wiki/Telephone_numbers_in_Northern_Cyprus) of `392`, `533` and `542`, so the "country code" is currently parsed as `TR`.
|
|
438
471
|
* For the purpose of differentiating Northern Cyprus phone numbers from Turkey phone numbers, some unofficial code like `XC` could be used for it in Google's `libphonenumber`.
|
|
439
472
|
* Northern Cyprus region has [not](https://en.wikipedia.org/wiki/ISO_3166-2:CY) ever been assigned any official ISO-3166-2 ["subdivision code"](https://en.wikipedia.org/wiki/ISO_3166-2).
|
|
473
|
+
</details>
|
|
440
474
|
|
|
441
475
|
## API
|
|
442
476
|
|
|
@@ -630,7 +664,7 @@ Checks if the phone number is "possible". Only checks the phone number length. D
|
|
|
630
664
|
|
|
631
665
|
Checks if the phone number is "valid". First checks the phone number length and then checks the phone number digits against all available regular expressions.
|
|
632
666
|
|
|
633
|
-
By default, this
|
|
667
|
+
By default, this package uses [`min`](#min-vs-max-vs-mobile-vs-core) ("minimal") metadata which is only `80 kB` in size but also doesn't include the precise regular expressions for validating phone number digits, i.e. it only validates phone number length, effectively causing `isValid()` to behave same way as if it was `isPossible()`. So if you need to validate phone number digits themselves and you don't mind the extra `65 kB` of metadata then use [`max`](#min-vs-max-vs-mobile-vs-core) metadata instead of the default (`min`) one. For reference, Google's original `libphonenumber` always uses `max` metadata and doesn't have a concept of `min` metadata.
|
|
634
668
|
|
|
635
669
|
<details>
|
|
636
670
|
<summary>See an example illustrating different results when using <code>/min</code> vs <code>/max</code> vs <code>/mobile</code> metadata</summary>
|
|
@@ -1550,9 +1584,9 @@ isValidNumberForRegion('07624369230', 'IM') === true
|
|
|
1550
1584
|
|
|
1551
1585
|
## Using phone number validation feature
|
|
1552
1586
|
|
|
1553
|
-
|
|
1587
|
+
When choosing between [`.isValid()`](#isvalid-boolean) and [`.isPossible()`](#ispossible-boolean), I'd personally prefer the latter. The rationale is that telephone numbering plans can and sometimes do change, meaning that [`.isValid()`](#isvalid-boolean) function may one day become outdated on a website that isn't actively maintained anymore. Imagine a "promo-site" or a "personal website" being deployed once and then running for years without any maintenance, where a client may be unable to submit a simple "Contact Us" form just because this newly-allocated pool of mobile phone numbers wasn't present in that old version of `libphonenumber-js` that was used when building the website.
|
|
1554
1588
|
|
|
1555
|
-
Whenever there's a "business requirement" to validate phone number input, I prefer using [
|
|
1589
|
+
Whenever there's a "business requirement" to validate phone number input, I prefer using [`.isPossible()`](#ispossible-boolean) instead of [`.isValid()`](#isvalid-boolean), so that it just validates the phone number length and doesn't validate the actual phone number digits. But it doesn't mean that you shouldn't use [`.isValid()`](#isvalid-boolean) — maybe in your case it would make sense. Just be warned that by using [`.isValid()`](#isvalid-boolean) in an application or on a website, you volunteer to keep it updated for the rest of its lifespan.
|
|
1556
1590
|
|
|
1557
1591
|
## React
|
|
1558
1592
|
|
package/build/isPossible.js
CHANGED
|
@@ -49,24 +49,25 @@ function isPossiblePhoneNumber(input, options, metadata) {
|
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
// Old metadata (< 1.0.18) had no "possible length" data.
|
|
52
|
+
// Old (legacy) metadata (< 1.0.18) had no "possible length" data.
|
|
53
|
+
// So `isPossibleNumber()` function is supported only for non-legacy metadata.
|
|
53
54
|
if (metadata.possibleLengths()) {
|
|
54
55
|
return isPossibleNumber(input.phone || input.nationalNumber, input.country, metadata);
|
|
55
|
-
} else {
|
|
56
|
-
// There was a bug between `1.7.35` and `1.7.37` where "possible_lengths"
|
|
57
|
-
// were missing for "non-geographical" numbering plans.
|
|
58
|
-
// Just assume the number is possible in such cases:
|
|
59
|
-
// it's unlikely that anyone generated their custom metadata
|
|
60
|
-
// in that short period of time (one day).
|
|
61
|
-
// This code can be removed in some future major version update.
|
|
62
|
-
if (input.countryCallingCode && metadata.isNonGeographicCallingCode(input.countryCallingCode)) {
|
|
63
|
-
// "Non-geographic entities" did't have `possibleLengths`
|
|
64
|
-
// due to a bug in metadata generation process.
|
|
65
|
-
return true;
|
|
66
|
-
} else {
|
|
67
|
-
throw new Error('Missing "possibleLengths" in metadata. Perhaps the metadata has been generated before v1.0.18.');
|
|
68
|
-
}
|
|
69
56
|
}
|
|
57
|
+
|
|
58
|
+
// There was a bug in versions from `1.7.35` to `1.7.37` of `libphonenumber-js`
|
|
59
|
+
// where "possible_lengths" property was missing from "non-geographical" numbering plans' metadata.
|
|
60
|
+
// After that, the bug was noticed and fixed.
|
|
61
|
+
// So for versions from `1.7.35` to `1.7.37`, just assume that all "non=geotraphical" numbers are possible.
|
|
62
|
+
// The reason is that the bug was noticed relatively quickly (within a day or so)
|
|
63
|
+
// and it's unlikely that anyone generated their custom metadata in that short time span.
|
|
64
|
+
// And if they didn't generate any custom metadata then a follow-up package update would've silently fixed the bug.
|
|
65
|
+
if (input.countryCallingCode && metadata.isNonGeographicCallingCode(input.countryCallingCode)) {
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// `isPossibleNumber()` function is not supported.
|
|
70
|
+
throw new Error('Missing "possibleLengths" in metadata. Perhaps the metadata has been generated before v1.0.18.');
|
|
70
71
|
}
|
|
71
72
|
function isPossibleNumber(nationalNumber, country, metadata) {
|
|
72
73
|
//, isInternational) {
|
package/build/isPossible.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"isPossible.js","names":["_metadata","_interopRequireDefault","require","_checkNumberLength","e","__esModule","isPossiblePhoneNumber","input","options","metadata","undefined","Metadata","v2","countryCallingCode","Error","selectNumberingPlan","phone","country","hasCountry","concat","possibleLengths","isPossibleNumber","nationalNumber","isNonGeographicCallingCode","checkNumberLength"],"sources":["../source/isPossible.js"],"sourcesContent":["import Metadata from './metadata.js'\r\nimport checkNumberLength from './helpers/checkNumberLength.js'\r\n\r\n/**\r\n * Checks if a phone number is \"possible\" (basically just checks its length).\r\n *\r\n * isPossible(phoneNumberInstance, { ..., v2: true }, metadata)\r\n *\r\n * isPossible({ phone: '8005553535', country: 'RU' }, { ... }, metadata)\r\n * isPossible({ phone: '8005553535', country: 'RU' }, undefined, metadata)\r\n *\r\n * @param {object|PhoneNumber} input — If `options.v2: true` flag is passed, the `input` should be a `PhoneNumber` instance. Otherwise, it should be an object of shape `{ phone: '...', country: '...' }`.\r\n * @param {object} [options]\r\n * @param {object} metadata\r\n * @return {string}\r\n */\r\nexport default function isPossiblePhoneNumber(input, options, metadata) {\r\n\t/* istanbul ignore if */\r\n\tif (options === undefined) {\r\n\t\toptions = {}\r\n\t}\r\n\r\n\tmetadata = new Metadata(metadata)\r\n\r\n\tif (options.v2) {\r\n\t\tif (!input.countryCallingCode) {\r\n\t\t\tthrow new Error('Invalid phone number object passed')\r\n\t\t}\r\n\t\tmetadata.selectNumberingPlan(input.countryCallingCode)\r\n\t} else {\r\n\t\tif (!input.phone) {\r\n\t\t\treturn false\r\n\t\t}\r\n\t\tif (input.country) {\r\n\t\t\tif (!metadata.hasCountry(input.country)) {\r\n\t\t\t\tthrow new Error(`Unknown country: ${input.country}`)\r\n\t\t\t}\r\n\t\t\tmetadata.selectNumberingPlan(input.country)\r\n\t\t} else {\r\n\t\t\tif (!input.countryCallingCode) {\r\n\t\t\t\tthrow new Error('Invalid phone number object passed')\r\n\t\t\t}\r\n\t\t\tmetadata.selectNumberingPlan(input.countryCallingCode)\r\n\t\t}\r\n\t}\r\n\r\n\t// Old metadata (< 1.0.18) had no \"possible length\" data.\r\n\tif (metadata.possibleLengths()) {\r\n\t\treturn isPossibleNumber(input.phone || input.nationalNumber, input.country, metadata)\r\n\t}
|
|
1
|
+
{"version":3,"file":"isPossible.js","names":["_metadata","_interopRequireDefault","require","_checkNumberLength","e","__esModule","isPossiblePhoneNumber","input","options","metadata","undefined","Metadata","v2","countryCallingCode","Error","selectNumberingPlan","phone","country","hasCountry","concat","possibleLengths","isPossibleNumber","nationalNumber","isNonGeographicCallingCode","checkNumberLength"],"sources":["../source/isPossible.js"],"sourcesContent":["import Metadata from './metadata.js'\r\nimport checkNumberLength from './helpers/checkNumberLength.js'\r\n\r\n/**\r\n * Checks if a phone number is \"possible\" (basically just checks its length).\r\n *\r\n * isPossible(phoneNumberInstance, { ..., v2: true }, metadata)\r\n *\r\n * isPossible({ phone: '8005553535', country: 'RU' }, { ... }, metadata)\r\n * isPossible({ phone: '8005553535', country: 'RU' }, undefined, metadata)\r\n *\r\n * @param {object|PhoneNumber} input — If `options.v2: true` flag is passed, the `input` should be a `PhoneNumber` instance. Otherwise, it should be an object of shape `{ phone: '...', country: '...' }`.\r\n * @param {object} [options]\r\n * @param {object} metadata\r\n * @return {string}\r\n */\r\nexport default function isPossiblePhoneNumber(input, options, metadata) {\r\n\t/* istanbul ignore if */\r\n\tif (options === undefined) {\r\n\t\toptions = {}\r\n\t}\r\n\r\n\tmetadata = new Metadata(metadata)\r\n\r\n\tif (options.v2) {\r\n\t\tif (!input.countryCallingCode) {\r\n\t\t\tthrow new Error('Invalid phone number object passed')\r\n\t\t}\r\n\t\tmetadata.selectNumberingPlan(input.countryCallingCode)\r\n\t} else {\r\n\t\tif (!input.phone) {\r\n\t\t\treturn false\r\n\t\t}\r\n\t\tif (input.country) {\r\n\t\t\tif (!metadata.hasCountry(input.country)) {\r\n\t\t\t\tthrow new Error(`Unknown country: ${input.country}`)\r\n\t\t\t}\r\n\t\t\tmetadata.selectNumberingPlan(input.country)\r\n\t\t} else {\r\n\t\t\tif (!input.countryCallingCode) {\r\n\t\t\t\tthrow new Error('Invalid phone number object passed')\r\n\t\t\t}\r\n\t\t\tmetadata.selectNumberingPlan(input.countryCallingCode)\r\n\t\t}\r\n\t}\r\n\r\n\t// Old (legacy) metadata (< 1.0.18) had no \"possible length\" data.\r\n\t// So `isPossibleNumber()` function is supported only for non-legacy metadata.\r\n\tif (metadata.possibleLengths()) {\r\n\t\treturn isPossibleNumber(input.phone || input.nationalNumber, input.country, metadata)\r\n\t}\r\n\r\n\t// There was a bug in versions from `1.7.35` to `1.7.37` of `libphonenumber-js`\r\n\t// where \"possible_lengths\" property was missing from \"non-geographical\" numbering plans' metadata.\r\n\t// After that, the bug was noticed and fixed.\r\n\t// So for versions from `1.7.35` to `1.7.37`, just assume that all \"non=geotraphical\" numbers are possible.\r\n\t// The reason is that the bug was noticed relatively quickly (within a day or so)\r\n\t// and it's unlikely that anyone generated their custom metadata in that short time span.\r\n\t// And if they didn't generate any custom metadata then a follow-up package update would've silently fixed the bug.\r\n\tif (input.countryCallingCode && metadata.isNonGeographicCallingCode(input.countryCallingCode)) {\r\n\t\treturn true\r\n\t}\r\n\r\n\t// `isPossibleNumber()` function is not supported.\r\n\tthrow new Error('Missing \"possibleLengths\" in metadata. Perhaps the metadata has been generated before v1.0.18.');\r\n}\r\n\r\nexport function isPossibleNumber(nationalNumber, country, metadata) { //, isInternational) {\r\n\tswitch (checkNumberLength(nationalNumber, country, metadata)) {\r\n\t\tcase 'IS_POSSIBLE':\r\n\t\t\treturn true\r\n\t\t// This library ignores \"local-only\" phone numbers (for simplicity).\r\n\t\t// See the readme for more info on what are \"local-only\" phone numbers.\r\n\t\t// case 'IS_POSSIBLE_LOCAL_ONLY':\r\n\t\t// \treturn !isInternational\r\n\t\tdefault:\r\n\t\t\treturn false\r\n\t}\r\n}"],"mappings":";;;;;;;AAAA,IAAAA,SAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,kBAAA,GAAAF,sBAAA,CAAAC,OAAA;AAA8D,SAAAD,uBAAAG,CAAA,WAAAA,CAAA,IAAAA,CAAA,CAAAC,UAAA,GAAAD,CAAA,gBAAAA,CAAA;AAE9D;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASE,qBAAqBA,CAACC,KAAK,EAAEC,OAAO,EAAEC,QAAQ,EAAE;EACvE;EACA,IAAID,OAAO,KAAKE,SAAS,EAAE;IAC1BF,OAAO,GAAG,CAAC,CAAC;EACb;EAEAC,QAAQ,GAAG,IAAIE,oBAAQ,CAACF,QAAQ,CAAC;EAEjC,IAAID,OAAO,CAACI,EAAE,EAAE;IACf,IAAI,CAACL,KAAK,CAACM,kBAAkB,EAAE;MAC9B,MAAM,IAAIC,KAAK,CAAC,oCAAoC,CAAC;IACtD;IACAL,QAAQ,CAACM,mBAAmB,CAACR,KAAK,CAACM,kBAAkB,CAAC;EACvD,CAAC,MAAM;IACN,IAAI,CAACN,KAAK,CAACS,KAAK,EAAE;MACjB,OAAO,KAAK;IACb;IACA,IAAIT,KAAK,CAACU,OAAO,EAAE;MAClB,IAAI,CAACR,QAAQ,CAACS,UAAU,CAACX,KAAK,CAACU,OAAO,CAAC,EAAE;QACxC,MAAM,IAAIH,KAAK,qBAAAK,MAAA,CAAqBZ,KAAK,CAACU,OAAO,CAAE,CAAC;MACrD;MACAR,QAAQ,CAACM,mBAAmB,CAACR,KAAK,CAACU,OAAO,CAAC;IAC5C,CAAC,MAAM;MACN,IAAI,CAACV,KAAK,CAACM,kBAAkB,EAAE;QAC9B,MAAM,IAAIC,KAAK,CAAC,oCAAoC,CAAC;MACtD;MACAL,QAAQ,CAACM,mBAAmB,CAACR,KAAK,CAACM,kBAAkB,CAAC;IACvD;EACD;;EAEA;EACA;EACA,IAAIJ,QAAQ,CAACW,eAAe,CAAC,CAAC,EAAE;IAC/B,OAAOC,gBAAgB,CAACd,KAAK,CAACS,KAAK,IAAIT,KAAK,CAACe,cAAc,EAAEf,KAAK,CAACU,OAAO,EAAER,QAAQ,CAAC;EACtF;;EAEA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,IAAIF,KAAK,CAACM,kBAAkB,IAAIJ,QAAQ,CAACc,0BAA0B,CAAChB,KAAK,CAACM,kBAAkB,CAAC,EAAE;IAC9F,OAAO,IAAI;EACZ;;EAEA;EACA,MAAM,IAAIC,KAAK,CAAC,gGAAgG,CAAC;AAClH;AAEO,SAASO,gBAAgBA,CAACC,cAAc,EAAEL,OAAO,EAAER,QAAQ,EAAE;EAAE;EACrE,QAAQ,IAAAe,6BAAiB,EAACF,cAAc,EAAEL,OAAO,EAAER,QAAQ,CAAC;IAC3D,KAAK,aAAa;MACjB,OAAO,IAAI;IACZ;IACA;IACA;IACA;IACA;MACC,OAAO,KAAK;EACd;AACD","ignoreList":[]}
|