libphonenumber-js 1.12.16 → 1.12.18
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/CHANGELOG.md +40 -35
- package/METADATA.md +2 -0
- package/README.md +209 -158
- package/build/AsYouType.js +6 -8
- package/build/AsYouType.js.map +1 -1
- package/build/AsYouType.test.js +13 -14
- package/build/AsYouType.test.js.map +1 -1
- package/build/AsYouTypeFormatter.complete.js +4 -4
- package/build/AsYouTypeFormatter.complete.js.map +1 -1
- package/build/AsYouTypeFormatter.js +4 -4
- package/build/AsYouTypeFormatter.js.map +1 -1
- package/build/AsYouTypeParser.js +42 -29
- package/build/AsYouTypeParser.js.map +1 -1
- package/build/AsYouTypeState.js +21 -17
- package/build/AsYouTypeState.js.map +1 -1
- package/build/PhoneNumber.js +2 -2
- package/build/PhoneNumber.js.map +1 -1
- package/build/findNumbers/Leniency.js +0 -1
- package/build/findNumbers/Leniency.js.map +1 -1
- package/build/format.js +4 -4
- package/build/format.js.map +1 -1
- package/build/formatPhoneNumberForMobileDialing.js +2 -2
- package/build/formatPhoneNumberForMobileDialing.js.map +1 -1
- package/build/helpers/checkNumberLength.js +39 -7
- package/build/helpers/checkNumberLength.js.map +1 -1
- package/build/helpers/checkNumberLength.test.js +51 -12
- package/build/helpers/checkNumberLength.test.js.map +1 -1
- package/build/helpers/extractCountryCallingCode.js +13 -10
- package/build/helpers/extractCountryCallingCode.js.map +1 -1
- package/build/helpers/extractCountryCallingCode.test.js +3 -3
- package/build/helpers/extractCountryCallingCode.test.js.map +1 -1
- package/build/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +10 -8
- package/build/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map +1 -1
- package/build/helpers/extractNationalNumber.js +15 -5
- package/build/helpers/extractNationalNumber.js.map +1 -1
- package/build/helpers/extractNationalNumber.test.js +10 -2
- package/build/helpers/extractNationalNumber.test.js.map +1 -1
- package/build/helpers/getCountryByCallingCode.js +3 -2
- package/build/helpers/getCountryByCallingCode.js.map +1 -1
- package/build/helpers/getCountryByNationalNumber.js +27 -6
- package/build/helpers/getCountryByNationalNumber.js.map +1 -1
- package/build/helpers/matchesEntirely.js +4 -4
- package/build/helpers/matchesEntirely.js.map +1 -1
- package/build/isPossible.js +4 -4
- package/build/isPossible.js.map +1 -1
- package/build/isPossible.test.js +7 -0
- package/build/isPossible.test.js.map +1 -1
- package/build/isPossiblePhoneNumber.test.js +4 -0
- package/build/isPossiblePhoneNumber.test.js.map +1 -1
- package/build/metadata.js +2 -2
- package/build/metadata.js.map +1 -1
- package/build/parse.js +9 -7
- package/build/parse.js.map +1 -1
- package/build/parseIncompletePhoneNumber.js +7 -7
- package/build/parseIncompletePhoneNumber.js.map +1 -1
- package/build/parsePhoneNumber.test.js +12 -0
- package/build/parsePhoneNumber.test.js.map +1 -1
- package/build/validatePhoneNumberLength.js +1 -1
- package/build/validatePhoneNumberLength.js.map +1 -1
- package/build-scripts/check-for-metadata-changes-and-create-pull-request.js +87 -0
- package/{runnable/metadata-update-and-release.js → build-scripts/check-for-metadata-changes-and-release.js} +10 -4
- package/{runnable/json-to-js.js → build-scripts/create-js-file-for-json-file.js} +2 -2
- package/{runnable/metadata-branch.js → build-scripts/create-metadata-update-branch.js} +6 -1
- package/{runnable/generate-country-codes.js → build-scripts/generate-country-codes-list-in-typescript.js} +1 -0
- package/{runnable/generate.js → build-scripts/generate-metadata-from-google-metadata.js} +8 -14
- package/{runnable/modules → build-scripts/helpers}/commit.js +1 -0
- package/{runnable/modules → build-scripts/helpers}/exec.js +1 -0
- package/{runnable/modules/update-metadata.js → build-scripts/helpers/update-metadata-from-google-metadata.js} +9 -7
- package/{runnable/download.js → build-scripts/pull-google-metadata.js} +12 -4
- package/bundle/libphonenumber-js.min.js +1 -1
- package/bundle/libphonenumber-js.min.js.map +1 -1
- package/bundle/libphonenumber-max.js +1 -1
- package/bundle/libphonenumber-max.js.map +1 -1
- package/bundle/libphonenumber-min.js +1 -1
- package/bundle/libphonenumber-min.js.map +1 -1
- package/bundle/libphonenumber-mobile.js +1 -1
- package/bundle/libphonenumber-mobile.js.map +1 -1
- package/core/index.d.cts +1 -1
- package/core/index.d.ts +1 -1
- package/custom.d.ts +1 -1
- package/es6/AsYouType.js +6 -8
- package/es6/AsYouType.js.map +1 -1
- package/es6/AsYouType.test.js +13 -14
- package/es6/AsYouType.test.js.map +1 -1
- package/es6/AsYouTypeFormatter.complete.js +4 -4
- package/es6/AsYouTypeFormatter.complete.js.map +1 -1
- package/es6/AsYouTypeFormatter.js +4 -4
- package/es6/AsYouTypeFormatter.js.map +1 -1
- package/es6/AsYouTypeParser.js +42 -29
- package/es6/AsYouTypeParser.js.map +1 -1
- package/es6/AsYouTypeState.js +21 -17
- package/es6/AsYouTypeState.js.map +1 -1
- package/es6/PhoneNumber.js +1 -2
- package/es6/PhoneNumber.js.map +1 -1
- package/es6/findNumbers/Leniency.js +0 -1
- package/es6/findNumbers/Leniency.js.map +1 -1
- package/es6/format.js +4 -4
- package/es6/format.js.map +1 -1
- package/es6/formatPhoneNumberForMobileDialing.js +2 -2
- package/es6/formatPhoneNumberForMobileDialing.js.map +1 -1
- package/es6/helpers/checkNumberLength.js +39 -7
- package/es6/helpers/checkNumberLength.js.map +1 -1
- package/es6/helpers/checkNumberLength.test.js +49 -12
- package/es6/helpers/checkNumberLength.test.js.map +1 -1
- package/es6/helpers/extractCountryCallingCode.js +13 -10
- package/es6/helpers/extractCountryCallingCode.js.map +1 -1
- package/es6/helpers/extractCountryCallingCode.test.js +3 -3
- package/es6/helpers/extractCountryCallingCode.test.js.map +1 -1
- package/es6/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +10 -8
- package/es6/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map +1 -1
- package/es6/helpers/extractNationalNumber.js +15 -5
- package/es6/helpers/extractNationalNumber.js.map +1 -1
- package/es6/helpers/extractNationalNumber.test.js +10 -2
- package/es6/helpers/extractNationalNumber.test.js.map +1 -1
- package/es6/helpers/getCountryByCallingCode.js +3 -2
- package/es6/helpers/getCountryByCallingCode.js.map +1 -1
- package/es6/helpers/getCountryByNationalNumber.js +28 -6
- package/es6/helpers/getCountryByNationalNumber.js.map +1 -1
- package/es6/helpers/matchesEntirely.js +4 -4
- package/es6/helpers/matchesEntirely.js.map +1 -1
- package/es6/isPossible.js +4 -4
- package/es6/isPossible.js.map +1 -1
- package/es6/isPossible.test.js +7 -0
- package/es6/isPossible.test.js.map +1 -1
- package/es6/isPossiblePhoneNumber.test.js +4 -0
- package/es6/isPossiblePhoneNumber.test.js.map +1 -1
- package/es6/metadata.js +2 -2
- package/es6/metadata.js.map +1 -1
- package/es6/parse.js +9 -7
- package/es6/parse.js.map +1 -1
- package/es6/parseIncompletePhoneNumber.js +7 -7
- package/es6/parseIncompletePhoneNumber.js.map +1 -1
- package/es6/parsePhoneNumber.test.js +12 -0
- package/es6/parsePhoneNumber.test.js.map +1 -1
- package/es6/validatePhoneNumberLength.js +1 -1
- package/es6/validatePhoneNumberLength.js.map +1 -1
- package/index.d.cts +1 -1
- package/index.d.ts +1 -1
- package/max/index.d.cts +1 -1
- package/max/index.d.ts +1 -1
- package/min/index.d.cts +1 -1
- package/min/index.d.ts +1 -1
- package/mobile/index.d.cts +1 -1
- package/mobile/index.d.ts +1 -1
- package/package.json +16 -23
- package/source/AsYouType.js +8 -10
- package/source/AsYouType.test.js +13 -14
- package/source/AsYouTypeFormatter.complete.js +4 -4
- package/source/AsYouTypeFormatter.js +4 -4
- package/source/AsYouTypeParser.js +45 -28
- package/source/AsYouTypeState.js +21 -17
- package/source/PhoneNumber.js +1 -2
- package/source/findNumbers/Leniency.js +0 -1
- package/source/format.js +4 -4
- package/source/formatPhoneNumberForMobileDialing.js +2 -2
- package/source/helpers/checkNumberLength.js +40 -7
- package/source/helpers/checkNumberLength.test.js +54 -12
- package/source/helpers/extractCountryCallingCode.js +15 -10
- package/source/helpers/extractCountryCallingCode.test.js +3 -3
- package/source/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +19 -8
- package/source/helpers/extractNationalNumber.js +15 -5
- package/source/helpers/extractNationalNumber.test.js +11 -2
- package/source/helpers/getCountryByCallingCode.js +2 -2
- package/source/helpers/getCountryByNationalNumber.js +27 -6
- package/source/helpers/matchesEntirely.js +4 -4
- package/source/isPossible.js +4 -4
- package/source/isPossible.test.js +9 -0
- package/source/isPossiblePhoneNumber.test.js +5 -0
- package/source/metadata.js +2 -2
- package/source/parse.js +9 -5
- package/source/parseIncompletePhoneNumber.js +7 -7
- package/source/parsePhoneNumber.test.js +14 -0
- package/source/validatePhoneNumberLength.js +1 -1
- package/runnable/metadata-update-and-push-and-pull-request.js +0 -47
- package/runnable/metadata-update-and-push.js +0 -30
- /package/{runnable/create-commonjs-package-json.js → build-scripts/create-commonjs-package-json-file.js} +0 -0
package/README.md
CHANGED
|
@@ -4,7 +4,9 @@
|
|
|
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
|
+
|
|
9
|
+
Parse and format personal phone numbers.
|
|
8
10
|
|
|
9
11
|
[See Demo](https://catamphetamine.gitlab.io/libphonenumber-js/)
|
|
10
12
|
|
|
@@ -14,7 +16,7 @@ If you’re trying to build a React component with it, take a look at [`react-ph
|
|
|
14
16
|
|
|
15
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), weighs about 550 kB (350 kB code + 200 kB metadata).
|
|
16
18
|
|
|
17
|
-
With many websites today asking for user's phone number, the internet could benefit from a simpler and smaller library that would just
|
|
19
|
+
With many websites today asking for user's phone number, the internet could benefit from a simpler and smaller library that would just deal with parsing or formatting personal phone numbers, and that's what `libphonenumber-js` is.
|
|
18
20
|
|
|
19
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. -->
|
|
20
22
|
|
|
@@ -22,35 +24,37 @@ With many websites today asking for user's phone number, the internet could bene
|
|
|
22
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.
|
|
23
25
|
-->
|
|
24
26
|
|
|
25
|
-
##
|
|
27
|
+
## Differences from Google's `libphonenumber`
|
|
26
28
|
|
|
27
29
|
* 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).
|
|
28
30
|
|
|
29
|
-
* Can search for phone numbers in text
|
|
31
|
+
* Can search for phone numbers in text. Google's autogenerated javascript port doesn't provide such feature for some reason.
|
|
30
32
|
|
|
31
|
-
*
|
|
33
|
+
* Focuses on parsing and formatting personal phone numbers while skipping any other "special" cases like:
|
|
32
34
|
|
|
33
35
|
* Emergency phone numbers like `911`.
|
|
34
36
|
|
|
35
|
-
* ["Short codes"](https://www.tatango.com/blog/sms-short-codes-what-every-business-needs-to-know/)
|
|
37
|
+
* ["Short codes"](https://www.tatango.com/blog/sms-short-codes-what-every-business-needs-to-know/) — short SMS-only numbers like `12345`.
|
|
38
|
+
|
|
39
|
+
* 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 `*`.
|
|
36
40
|
|
|
37
|
-
*
|
|
41
|
+
* 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).
|
|
38
42
|
|
|
39
|
-
* Alphabetic phone numbers like `1-800-GOT-MILK
|
|
43
|
+
* 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.
|
|
40
44
|
|
|
41
|
-
* "Two-in-one" phone numbers with "combined" extensions like `(530) 583-6985 x302/x2303
|
|
45
|
+
* "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.
|
|
42
46
|
|
|
43
|
-
*
|
|
47
|
+
* 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.
|
|
44
48
|
|
|
45
|
-
|
|
49
|
+
* Doesn't provide "geolocation" feature when it can tell a city by a phone number.
|
|
46
50
|
|
|
47
|
-
* Doesn't
|
|
51
|
+
* 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.
|
|
48
52
|
|
|
49
|
-
* Doesn't
|
|
53
|
+
* 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.
|
|
50
54
|
|
|
51
|
-
* Doesn't
|
|
55
|
+
* 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.
|
|
52
56
|
|
|
53
|
-
*
|
|
57
|
+
* Fixed a small [bug](https://issuetracker.google.com/issues/335892662) when Canadian numbers `+1310xxxx` wheren't considered possible.
|
|
54
58
|
|
|
55
59
|
<!-- (Australia, Bolivia, Brazil, China, Colombia, Croatia, Faroe Islands, South Korea, Liechtenstein, Luxembourg, Venezuela) -->
|
|
56
60
|
|
|
@@ -70,7 +74,9 @@ One part of me was curious about how all this phone number parsing and formattin
|
|
|
70
74
|
|
|
71
75
|
## GitHub
|
|
72
76
|
|
|
73
|
-
On March 9th, 2020, GitHub, Inc. silently [banned](https://medium.com/@catamphetamine/how-github-blocked-me-and-all-my-libraries-c32c61f061d3) my account
|
|
77
|
+
On March 9th, 2020, GitHub, Inc. silently [banned](https://medium.com/@catamphetamine/how-github-blocked-me-and-all-my-libraries-c32c61f061d3) my account — erasing all my repos, issues and comments, even in my employer's private repos — without any notice or explanation. Because of that, all source codes had to be promptly moved to GitLab. The [GitHub repo](https://github.com/catamphetamine/libphonenumber-js) is now only used as a backup (although you can still "star" it), and the primary repo is now the [GitLab one](https://gitlab.com/catamphetamine/libphonenumber-js). For users' convenience, issues can be reported in both places.
|
|
78
|
+
|
|
79
|
+
If you're about to report an issue, see [bug reporting](#bug-reporting) instructions.
|
|
74
80
|
|
|
75
81
|
## Install
|
|
76
82
|
|
|
@@ -86,7 +92,7 @@ via [yarn](https://yarnpkg.com)
|
|
|
86
92
|
$ yarn add libphonenumber-js
|
|
87
93
|
```
|
|
88
94
|
|
|
89
|
-
|
|
95
|
+
Alternatively, one could include it on a web page [directly](#cdn) via a `<script/>` tag.
|
|
90
96
|
|
|
91
97
|
## Use
|
|
92
98
|
|
|
@@ -145,10 +151,13 @@ https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/42
|
|
|
145
151
|
_(new API)_
|
|
146
152
|
-->
|
|
147
153
|
|
|
154
|
+
Parses a complete phone number.
|
|
155
|
+
|
|
148
156
|
```js
|
|
149
157
|
import parsePhoneNumber from 'libphonenumber-js'
|
|
150
158
|
|
|
151
159
|
const phoneNumber = parsePhoneNumber(' 8 (800) 555-35-35 ', 'RU')
|
|
160
|
+
|
|
152
161
|
if (phoneNumber) {
|
|
153
162
|
phoneNumber.country === 'RU'
|
|
154
163
|
phoneNumber.number === '+78005553535'
|
|
@@ -172,28 +181,10 @@ parseNumber('Phone: 8 (800) 555 35 35.', 'RU')
|
|
|
172
181
|
</details>
|
|
173
182
|
-->
|
|
174
183
|
|
|
175
|
-
### Validate phone number
|
|
176
|
-
|
|
177
|
-
```js
|
|
178
|
-
import {
|
|
179
|
-
isPossiblePhoneNumber,
|
|
180
|
-
isValidPhoneNumber,
|
|
181
|
-
validatePhoneNumberLength
|
|
182
|
-
} from 'libphonenumber-js'
|
|
183
|
-
|
|
184
|
-
isPossiblePhoneNumber('8 (800) 555-35-35', 'RU') === true
|
|
185
|
-
isValidPhoneNumber('8 (800) 555-35-35', 'RU') === true
|
|
186
|
-
|
|
187
|
-
validatePhoneNumberLength('8 (800) 555', 'RU') === 'TOO_SHORT'
|
|
188
|
-
validatePhoneNumberLength('8 (800) 555-35-35', 'RU') === undefined // Length is valid.
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
`isPossiblePhoneNumber()` only validates phone number length, while `isValidPhoneNumber()` validates both phone number length and the actual phone number digits.
|
|
192
|
-
|
|
193
|
-
`validatePhoneNumberLength()` is just a more detailed version of `isPossiblePhoneNumber()` — if the phone number length is invalid, it returns the actual reason: `TOO_SHORT`, `TOO_LONG`, etc.
|
|
194
|
-
|
|
195
184
|
### Format phone number
|
|
196
185
|
|
|
186
|
+
Formats a complete phone number.
|
|
187
|
+
|
|
197
188
|
<!--
|
|
198
189
|
_(new API)_
|
|
199
190
|
-->
|
|
@@ -203,9 +194,11 @@ import parsePhoneNumber from 'libphonenumber-js'
|
|
|
203
194
|
|
|
204
195
|
const phoneNumber = parsePhoneNumber('+12133734253')
|
|
205
196
|
|
|
206
|
-
phoneNumber
|
|
207
|
-
phoneNumber.
|
|
208
|
-
phoneNumber.
|
|
197
|
+
if (phoneNumber) {
|
|
198
|
+
phoneNumber.formatInternational() === '+1 213 373 4253'
|
|
199
|
+
phoneNumber.formatNational() === '(213) 373-4253'
|
|
200
|
+
phoneNumber.getURI() === 'tel:+12133734253'
|
|
201
|
+
}
|
|
209
202
|
```
|
|
210
203
|
|
|
211
204
|
<!--
|
|
@@ -232,18 +225,44 @@ formatNumber({ country: 'US', phone: '2133734253' }, 'NATIONAL')
|
|
|
232
225
|
|
|
233
226
|
### "As You Type" formatter
|
|
234
227
|
|
|
228
|
+
Parses and formats an incomplete phone number.
|
|
229
|
+
|
|
235
230
|
```js
|
|
236
231
|
import { AsYouType } from 'libphonenumber-js'
|
|
237
232
|
|
|
238
233
|
new AsYouType().input('+12133734')
|
|
239
|
-
//
|
|
234
|
+
// Returns: '+1 213 373 4'
|
|
240
235
|
|
|
241
236
|
new AsYouType('US').input('2133734')
|
|
242
|
-
//
|
|
237
|
+
// Returns: '(213) 373-4'
|
|
243
238
|
```
|
|
244
239
|
|
|
240
|
+
### Validate phone number
|
|
241
|
+
|
|
242
|
+
Validates a complete phone number.
|
|
243
|
+
|
|
244
|
+
```js
|
|
245
|
+
import {
|
|
246
|
+
isPossiblePhoneNumber,
|
|
247
|
+
isValidPhoneNumber,
|
|
248
|
+
validatePhoneNumberLength
|
|
249
|
+
} from 'libphonenumber-js'
|
|
250
|
+
|
|
251
|
+
isPossiblePhoneNumber('8 (800) 555-35-35', 'RU') === true
|
|
252
|
+
isValidPhoneNumber('8 (800) 555-35-35', 'RU') === true
|
|
253
|
+
|
|
254
|
+
validatePhoneNumberLength('8 (800) 555', 'RU') === 'TOO_SHORT'
|
|
255
|
+
validatePhoneNumberLength('8 (800) 555-35-35', 'RU') === undefined // Length is valid.
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
`isPossiblePhoneNumber()` only validates phone number length, while `isValidPhoneNumber()` validates both phone number length and phone number digits.
|
|
259
|
+
|
|
260
|
+
`validatePhoneNumberLength()` is just a more detailed version of `isPossiblePhoneNumber()` — if the phone number length is invalid, instead of just returning `false`, it returns the actual reason why the phone number length is incorrect: `TOO_SHORT`, `TOO_LONG`, etc.
|
|
261
|
+
|
|
245
262
|
### Full-text search
|
|
246
263
|
|
|
264
|
+
Finds complete phone numbers in text.
|
|
265
|
+
|
|
247
266
|
<!--
|
|
248
267
|
_(new API)_
|
|
249
268
|
-->
|
|
@@ -282,31 +301,45 @@ findPhoneNumbersInText(`
|
|
|
282
301
|
|
|
283
302
|
## "min" vs "max" vs "mobile" vs "core"
|
|
284
303
|
|
|
285
|
-
This library provides different "metadata" sets, "metadata"
|
|
304
|
+
This library provides different "metadata" sets, where a "metadata" set is a complete list of phone number parsing and formatting rules for all possible countries.
|
|
286
305
|
|
|
287
|
-
|
|
306
|
+
As one may guess, the complete list of those rules is huge, so this library provides a way to optimize bundle size by choosing between `max`, `min`, `mobile` or "custom" metadata:
|
|
288
307
|
|
|
289
|
-
* `min` — (default) The smallest metadata set
|
|
308
|
+
* `min` — (default) The smallest metadata set
|
|
309
|
+
* Is about `80 kilobytes` in size (`libphonenumber-js/metadata.min.json` file)
|
|
310
|
+
* Choose this when:
|
|
311
|
+
* You don't need to detect phone number type — "fixed line", "mobile", etc — via [`.getType()`](#gettype-string)
|
|
312
|
+
<!-- * Because the `min` metadata doesn't include (for most countries anyway) the regular expressions that're required for phone number type detection. -->
|
|
313
|
+
* You're fine with just validating phone number length via [`.isPossible()`](#ispossible-boolean) and you don't need to strictly validate phone number digits via [`.isValid()`](#isvalid-boolean)
|
|
314
|
+
<!-- * Because the `min` metadata doesn't include (for most countries anyway) the regular expressions that're required for phone number digits validation. -->
|
|
290
315
|
|
|
291
|
-
* `
|
|
316
|
+
* `max` — The complete metadata set
|
|
317
|
+
* Is about `145 kilobytes` in size (`libphonenumber-js/metadata.max.json` file)
|
|
318
|
+
* Choose this when:
|
|
319
|
+
* The basic [`.isPossible()`](#ispossible-boolean) phone number length check is not enough for you and you need the strict phone number digits validation via [`.isValid()`](#isvalid-boolean)
|
|
320
|
+
* You need to detect phone number type — "fixed line", "mobile", etc — via [`.getType()`](#gettype-string)
|
|
292
321
|
|
|
293
|
-
|
|
322
|
+
* `mobile` — The complete metadata set for dealing with mobile numbers _only_
|
|
323
|
+
* Is about `95 kilobytes` in size (`libphonenumber-js/metadata.mobile.json` file)
|
|
324
|
+
* Choose this when:
|
|
325
|
+
* You need `max` metadata capabilities and you _only_ accept mobile numbers
|
|
326
|
+
* It will still be able to handle non-mobile numbers just fine, with the only difference that `.isValid()` or `.isPossible()` might potentially return `false` for them, or `.getType()` might potentially return `undefined`.
|
|
294
327
|
|
|
295
|
-
|
|
296
|
-
* `libphonenumber-js/min`
|
|
297
|
-
* `libphonenumber-js/mobile`
|
|
328
|
+
Choose one from the above and then simply `import` the functions from the relevant sub-package:
|
|
298
329
|
|
|
299
|
-
|
|
330
|
+
* `min` — `libphonenumber-js/min`, or just `libphonenumber-js`, since it's the default.
|
|
331
|
+
* `max` — `libphonenumber-js/max`
|
|
332
|
+
* `mobile` — `libphonenumber-js/mobile`
|
|
300
333
|
|
|
301
|
-
|
|
334
|
+
As for "custom" metadata, it could be used in those rare cases when not all countries are needed and a developer would really prefer to reduce the bundle size to a minimum. In that case, one could [generate](#customizing-metadata) their own "custom" metadata set and then `import` the functions from `libphonenumber-js/core` sub-package which doesn't come pre-packaged with any metadata and instead accepts `metadata` as the last argument of each exported function.
|
|
302
335
|
|
|
303
336
|
## Definitions
|
|
304
337
|
|
|
305
338
|
### Country code
|
|
306
339
|
|
|
307
|
-
A "country code" is a [two-letter ISO country code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) (like `US`). <!-- or a special `001` country code used for ["non-geographic entities"](#non-geographic) (as per [Google's libphonenumber library](https://github.com/googlei18n/libphonenumber/blob/0068d861a68d3d4612f7bf8646ab844dd3cefce5/java/libphonenumber/test/com/google/i18n/phonenumbers/RegionCode.java#L23-L24)). -->
|
|
340
|
+
A "country code" is a [two-letter ISO country code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) (like `"US"`). <!-- or a special `001` country code used for ["non-geographic entities"](#non-geographic) (as per [Google's libphonenumber library](https://github.com/googlei18n/libphonenumber/blob/0068d861a68d3d4612f7bf8646ab844dd3cefce5/java/libphonenumber/test/com/google/i18n/phonenumbers/RegionCode.java#L23-L24)). -->
|
|
308
341
|
|
|
309
|
-
This library supports all [officially assigned](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements) ISO alpha-2 country codes, plus a few extra ones
|
|
342
|
+
This library supports all [officially assigned](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements) ISO alpha-2 country codes, plus a few extra ones: `AC` ([Ascension Island](https://en.wikipedia.org/wiki/Ascension_Island)), `TA` ([Tristan da Cunha](https://en.wikipedia.org/wiki/Tristan_da_Cunha)), `XK` ([Kosovo](https://en.wikipedia.org/wiki/Kosovo)).
|
|
310
343
|
|
|
311
344
|
<!--
|
|
312
345
|
var countries = []
|
|
@@ -331,27 +364,33 @@ There're several calling codes that don't belong to any country:
|
|
|
331
364
|
* `+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)
|
|
332
365
|
* `+979` — [International Premium Rate Service](https://en.wikipedia.org/wiki/International_Premium_Rate_Service)
|
|
333
366
|
|
|
334
|
-
Such phone numbering plans are called "non-geographic", and their phone numbers
|
|
367
|
+
Such phone numbering plans are called "non-geographic", and their phone numbers' `country` is `undefined`.
|
|
335
368
|
|
|
336
369
|
### National (significant) number
|
|
337
370
|
|
|
338
|
-
"National (significant) number"
|
|
371
|
+
"National (significant) number" is all national phone number digits, excluding the "national prefix". Examples:
|
|
372
|
+
* International number: `+1 213 373 4253`. Country: `"US"`. National number: `(213) 373-4253`. National (significant) number: `213 373 4253`
|
|
373
|
+
* 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) `0` "national prefix" when writing phone numbers in national format. National (significant) number: `1 45 45 32 45` (doesn't include the "national prefix").
|
|
339
374
|
|
|
340
375
|
### Country calling code
|
|
341
376
|
|
|
342
|
-
"Country calling code"
|
|
377
|
+
"Country calling code" is all digits between the `+` and the [national (significant) number](#national-significant-number) in a number that is written in international format. Examples:
|
|
378
|
+
* International number: `+1 213 373 4253`. Country: `"US"`. National (significant) number: `213 373 4253`. Country calling code: `1`
|
|
379
|
+
* Inetrnational number: `+33 1 45 45 32 45`. Country: `"FR"`. National (significant) number: `1 45 45 32 45`. Country calling code: `33`
|
|
380
|
+
|
|
381
|
+
Several countries could share the same "country calling code". For example, [NANPA](https://en.wikipedia.org/wiki/North_American_Numbering_Plan) countries like USA and Canada share the same `1` country calling code.
|
|
343
382
|
|
|
344
383
|
## API
|
|
345
384
|
|
|
346
385
|
### parsePhoneNumber(string, defaultCountry?: string | options?: object): PhoneNumber
|
|
347
386
|
|
|
348
|
-
Parses a phone number from `string`.
|
|
387
|
+
Parses a complete phone number from `string`.
|
|
349
388
|
|
|
350
|
-
Can be imported
|
|
389
|
+
Can be imported either as a "default" export or as a "named" export called `parsePhoneNumberFromString`.
|
|
351
390
|
|
|
352
391
|
```js
|
|
353
392
|
import parsePhoneNumber from 'libphonenumber-js'
|
|
354
|
-
//
|
|
393
|
+
// import { parsePhoneNumberFromString as parsePhoneNumber } from 'libphonenumber-js'
|
|
355
394
|
|
|
356
395
|
const phoneNumber = parsePhoneNumber('(213) 373-42-53 ext. 1234', 'US')
|
|
357
396
|
if (phoneNumber) {
|
|
@@ -359,7 +398,7 @@ if (phoneNumber) {
|
|
|
359
398
|
}
|
|
360
399
|
```
|
|
361
400
|
|
|
362
|
-
Returns an instance of [`PhoneNumber`](#phonenumber) class, or `undefined` if no phone number could be parsed
|
|
401
|
+
Returns an instance of [`PhoneNumber`](#phonenumber) class, or `undefined` if no phone number could be parsed from the input string. That could be for a number of reasons. For example, the phone number in the input string could be incomplete, or it could contain a phone number that has an invalid [country calling code](#country-calling-code), etc.
|
|
363
402
|
|
|
364
403
|
Available `options`:
|
|
365
404
|
|
|
@@ -367,9 +406,9 @@ Available `options`:
|
|
|
367
406
|
|
|
368
407
|
* `defaultCallingCode: string` — Default calling code for parsing numbers written in non-international form (without a `+` sign). Will be ignored when parsing numbers written in international form (with a `+` sign). It could be specified when parsing phone numbers belonging to ["non-geographic numbering plans"](#non-geographic) which by nature don't have a country code, making the `defaultCountry` option unusable.
|
|
369
408
|
|
|
370
|
-
* `extract: boolean` — Defines the ["strictness"](#strictness) of parsing a phone number. By default, the `extract` flag is `true` meaning that it will attempt to extract the phone number from an input string like `"My phone number is (213) 373-4253 and my hair is blue"`. This could be thought of as "less strict" parsing. To make it "more strict", one could pass `extract: false` flag, in which case the function will attempt to parse the input string as if the whole string was a phone number. Applied to the example above, it would return `undefined` because the entire string is not a phone number, but for input string `"(213) 373-4253"` it would return a parsed `PhoneNumber`.
|
|
409
|
+
* `extract: boolean` — Defines the ["strictness"](#strictness) of parsing a phone number. By default, the `extract` flag is `true` meaning that it will attempt to extract the phone number from an input string like `"My phone number is (213) 373-4253 and my hair is blue"`. This could be thought of as "less strict" parsing. To make it "more strict", one could pass `extract: false` flag, in which case the function will attempt to parse the input string as if the whole string was a phone number. Applied to the example above, it would return `undefined` because the entire string is not a phone number, but for an input string `"(213) 373-4253"` it would return a parsed `PhoneNumber`.
|
|
371
410
|
|
|
372
|
-
If a developer wants to know the exact reason why the phone number couldn't be parsed then they can use `parsePhoneNumberWithError()` function which throws
|
|
411
|
+
If a developer wants to know the exact reason why the phone number couldn't be parsed then they can use `parsePhoneNumberWithError()` function which throws an exact error:
|
|
373
412
|
|
|
374
413
|
```js
|
|
375
414
|
import { parsePhoneNumberWithError, ParseError } from 'libphonenumber-js'
|
|
@@ -406,12 +445,14 @@ try {
|
|
|
406
445
|
|
|
407
446
|
#### Strictness
|
|
408
447
|
|
|
409
|
-
By default, the parsing function will attempt to extract a phone number from
|
|
448
|
+
By default, the parsing function will attempt to extract a phone number from the input string even in cases like `"Support: (213) 373-4253 (robot)"`, which mimicks the behavior of the original Google's `libphonenumber` library, and is the default behavior for legacy reasons. However, if "strict" input validation is required, one can pass `extract: false` flag to demand that the whole input string be a viable phone number.
|
|
410
449
|
|
|
411
450
|
```js
|
|
451
|
+
// By default it parses without `extract: false` flag.
|
|
452
|
+
// It will "extract" the phone number from the input string.
|
|
412
453
|
parsePhoneNumber('Call: (213) 373-4253', 'US') === PhoneNumber
|
|
413
454
|
|
|
414
|
-
// When parsing the same string with `extract: false` flag,
|
|
455
|
+
// When parsing the same input string with `extract: false` flag,
|
|
415
456
|
// it will return `undefined`, because a phone number can't
|
|
416
457
|
// contain letters or a colon.
|
|
417
458
|
parsePhoneNumber('Call: (213) 373-4253', {
|
|
@@ -419,6 +460,8 @@ parsePhoneNumber('Call: (213) 373-4253', {
|
|
|
419
460
|
extract: false
|
|
420
461
|
}) === undefined
|
|
421
462
|
|
|
463
|
+
// When the whole input string represents a phone number,
|
|
464
|
+
// parsing it with `extract: false` flag will return a `PhoneNumber`.
|
|
422
465
|
parsePhoneNumber('(213) 373-4253', {
|
|
423
466
|
defaultCountry: 'US',
|
|
424
467
|
extract: false
|
|
@@ -427,9 +470,9 @@ parsePhoneNumber('(213) 373-4253', {
|
|
|
427
470
|
|
|
428
471
|
### `PhoneNumber`
|
|
429
472
|
|
|
430
|
-
`PhoneNumber` class represents a
|
|
473
|
+
`PhoneNumber` class represents a complete phone number.
|
|
431
474
|
|
|
432
|
-
`PhoneNumber` class instance is returned from [`parsePhoneNumber()`](#parsephonenumberstring-defaultcountry-string--options-object-phonenumber) function, or it could also be constructed manually from a
|
|
475
|
+
`PhoneNumber` class instance is returned from [`parsePhoneNumber()`](#parsephonenumberstring-defaultcountry-string--options-object-phonenumber) function, or it could also be constructed manually from a number in [`E.164`](https://en.wikipedia.org/wiki/E.164) format:
|
|
433
476
|
|
|
434
477
|
```js
|
|
435
478
|
const phoneNumber = new PhoneNumber('+12133734253')
|
|
@@ -464,7 +507,7 @@ const phoneNumber = new PhoneNumber('RU', '8005553535', metadata)
|
|
|
464
507
|
|
|
465
508
|
#### `setExt(ext: string)`
|
|
466
509
|
|
|
467
|
-
|
|
510
|
+
Adds a [phone number extension](https://en.wikipedia.org/wiki/Extension_(telephone)) to a phone number.
|
|
468
511
|
|
|
469
512
|
```js
|
|
470
513
|
const phone = "+12133734253"
|
|
@@ -482,7 +525,7 @@ if (phoneNumber) {
|
|
|
482
525
|
|
|
483
526
|
#### `format(format: string, [options]): string`
|
|
484
527
|
|
|
485
|
-
Formats the phone number into a string according to a `format`.
|
|
528
|
+
Formats the phone number into a string according to a specified `format`.
|
|
486
529
|
|
|
487
530
|
Available `format`s:
|
|
488
531
|
|
|
@@ -494,9 +537,9 @@ Available `format`s:
|
|
|
494
537
|
|
|
495
538
|
Available `options`:
|
|
496
539
|
|
|
497
|
-
* `formatExtension(number, extension)` — Formats `number` and `extension` into a string. By default returns `${number} ext. ${extension}` for almost all countries with rare exceptions of some special cases like `${number} x${extension}` for
|
|
540
|
+
* `formatExtension(number, extension)` — Formats `number` and `extension` into a string. By default returns `${number} ext. ${extension}` for almost all countries with rare exceptions of some special cases like `${number} x${extension}` for the United Kingdom.
|
|
498
541
|
|
|
499
|
-
* `nationalPrefix: Boolean` — Some phone numbers can be formatted both with national prefix and without it. In such cases the library defaults to "with national prefix" (for legacy reasons). Pass `nationalPrefix: false` option to force formatting without national prefix
|
|
542
|
+
* `nationalPrefix: Boolean` — Some phone numbers can be formatted both with national prefix and without it. In such cases the library defaults to "with national prefix" (for legacy reasons). Pass `nationalPrefix: false` option to force formatting without a national prefix.
|
|
500
543
|
|
|
501
544
|
Examples:
|
|
502
545
|
|
|
@@ -509,7 +552,7 @@ phoneNumber.format("NATIONAL") === '(213) 373-4253'
|
|
|
509
552
|
phoneNumber.format("INTERNATIONAL") === '+1 213 373 4253'
|
|
510
553
|
phoneNumber.format("RFC3966") === 'tel:+12133734253'
|
|
511
554
|
|
|
512
|
-
//
|
|
555
|
+
// The following are just convenience aliases for the `.format()` function.
|
|
513
556
|
phoneNumber.formatNational() === phoneNumber.format("NATIONAL")
|
|
514
557
|
phoneNumber.formatInternational() === phoneNumber.format("INTERNATIONAL")
|
|
515
558
|
phoneNumber.getURI() === phoneNumber.format("RFC3966")
|
|
@@ -517,16 +560,16 @@ phoneNumber.getURI() === phoneNumber.format("RFC3966")
|
|
|
517
560
|
|
|
518
561
|
#### `isPossible(): boolean`
|
|
519
562
|
|
|
520
|
-
Checks if the phone number is "possible". Only checks the phone number length
|
|
563
|
+
Checks if the phone number is "possible". Only checks the phone number length. Doesn't check the actual phone number digits.
|
|
521
564
|
|
|
522
565
|
#### `isValid(): boolean`
|
|
523
566
|
|
|
524
567
|
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.
|
|
525
568
|
|
|
526
|
-
By default
|
|
569
|
+
By default, this library uses `min` ("minimal") metadata which is only `80 kB` in size but also doesn't include the precise validation regular expressions resulting in less strict validation rules (some very basic validation like number length check is still included for each country). If 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. Google's library always uses "full" metadata so it will yield different `isValidNumber()` results compared to the "minimal" metadata used by default in this library.
|
|
527
570
|
|
|
528
571
|
<details>
|
|
529
|
-
<summary>See an example illustrating different results when using <code>/min</code> vs <code>/max</code> vs <code>/mobile</code
|
|
572
|
+
<summary>See an example illustrating different results when using <code>/min</code> vs <code>/max</code> vs <code>/mobile</code> metadata</summary>
|
|
530
573
|
|
|
531
574
|
####
|
|
532
575
|
```js
|
|
@@ -534,19 +577,22 @@ import parseMin from 'libphonenumber-js/min'
|
|
|
534
577
|
import parseMax from 'libphonenumber-js/max'
|
|
535
578
|
import parseMobile from 'libphonenumber-js/mobile'
|
|
536
579
|
|
|
537
|
-
//
|
|
538
|
-
// can only have the second digit in the range of `0..8`.
|
|
539
|
-
//
|
|
540
|
-
//
|
|
541
|
-
//
|
|
580
|
+
// In Singapore (+65), when a mobile number starts with `8`,
|
|
581
|
+
// it can only have the second digit in the range of `0..8`.
|
|
582
|
+
// In a number "+6589555555", the second digit is `9`
|
|
583
|
+
// which makes it an invalid mobile number.
|
|
584
|
+
// This is a "strict" validation rule and it is
|
|
585
|
+
// not included in the (default) `min` metadata.
|
|
542
586
|
|
|
543
|
-
//
|
|
544
|
-
//
|
|
587
|
+
// When using the (default) `min` metadata,
|
|
588
|
+
// basic number length check passes (from `8` to `11`)
|
|
589
|
+
// and the "loose" national number validation regexp check passes too:
|
|
545
590
|
// `(?:1\d{3}|[369]|7000|8(?:\d{2})?)\d{7}`.
|
|
546
591
|
parseMin('+6589555555').isValid() === true
|
|
547
592
|
|
|
548
|
-
//
|
|
549
|
-
//
|
|
593
|
+
// When using `max` or `mobile` metadata,
|
|
594
|
+
// the validation regexp for mobile numbers is more precise —
|
|
595
|
+
// `(?:8[1-8]|9[0-8])\\d{6}` — as well as the possible lengths (only `8`).
|
|
550
596
|
parseMax('+6589555555').isValid() === false
|
|
551
597
|
parseMobile('+6589555555').isValid() === false
|
|
552
598
|
```
|
|
@@ -604,15 +650,15 @@ parsePhoneNumber('444 1 4444444444', 'TR').validateLength() === 'TOO_LONG'
|
|
|
604
650
|
|
|
605
651
|
#### `getPossibleCountries(): string[]`
|
|
606
652
|
|
|
607
|
-
Returns a list of countries this phone number could possibly belong to.
|
|
653
|
+
Returns a list of countries that this phone number could possibly belong to.
|
|
608
654
|
|
|
609
|
-
Can be used when parsing complete international phone
|
|
655
|
+
Can be used when parsing a complete international phone number which contains a "country calling code" that is shared between several countries. If parsing such a phone number returns `country: undefined` then `getPossibleCountries()` function could be used to somehow speculate about what country could this phone number possibly belong to.
|
|
610
656
|
|
|
611
657
|
#### `getType(): string?`
|
|
612
658
|
|
|
613
|
-
Returns phone number type
|
|
659
|
+
Returns phone number type — fixed line, mobile, toll free, etc — or `undefined` if the number is invalid or if there are no phone number type regular expressions for this country in metadata.
|
|
614
660
|
|
|
615
|
-
By default
|
|
661
|
+
By default, this library uses `min` ("minimal") metadata which is only `80 kB` in size but also doesn't include the regular expressions for determining a phone number type — fixed line, mobile, toll free, etc — which could result in `getType()` returning `undefined` for most countries. If 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. Google's library always uses "full" metadata so it will yield different `getNumberType()` results compared to the "minimal" metadata used by default in this library.
|
|
616
662
|
|
|
617
663
|
<details>
|
|
618
664
|
<summary>The list of possible return values</summary>
|
|
@@ -635,7 +681,7 @@ By default the library uses "minimal" metadata which is only 75 kilobytes in siz
|
|
|
635
681
|
####
|
|
636
682
|
|
|
637
683
|
<details>
|
|
638
|
-
<summary>See an example illustrating different results when using <code>/min</code> vs <code>/max</code> vs <code>/mobile</code
|
|
684
|
+
<summary>See an example illustrating different results when using <code>/min</code> vs <code>/max</code> vs <code>/mobile</code> metadata</summary>
|
|
639
685
|
|
|
640
686
|
####
|
|
641
687
|
```js
|
|
@@ -643,20 +689,19 @@ import parseMin from 'libphonenumber-js/min'
|
|
|
643
689
|
import parseMax from 'libphonenumber-js/max'
|
|
644
690
|
import parseMobile from 'libphonenumber-js/mobile'
|
|
645
691
|
|
|
646
|
-
// Singapore
|
|
692
|
+
// Singapore (+65) mobile number "+6584655555".
|
|
647
693
|
|
|
648
|
-
// The (default)
|
|
649
|
-
// getting phone number type
|
|
694
|
+
// The (default) `min` bundle doesn't contain any regexps for
|
|
695
|
+
// getting phone number type from phone number digits for Singapore.
|
|
650
696
|
parseMin('+6584655555').getType() === undefined
|
|
651
697
|
|
|
652
|
-
// The
|
|
653
|
-
//
|
|
654
|
-
// for all possible phone number types.
|
|
698
|
+
// The `max` does contain all the regexps for getting
|
|
699
|
+
// phone number type from phone number digits for any country.
|
|
655
700
|
parseMax('+6584655555').getType() === 'MOBILE'
|
|
656
701
|
|
|
657
|
-
|
|
658
|
-
//
|
|
659
|
-
//
|
|
702
|
+
|
|
703
|
+
// The `max` does contain all the regexps for getting
|
|
704
|
+
// mobile phone number type from mobile phone number digits for any country.
|
|
660
705
|
parseMobile('+6584655555').getType() === 'MOBILE'
|
|
661
706
|
```
|
|
662
707
|
</details>
|
|
@@ -701,15 +746,15 @@ See ["Using phone number validation feature"](#using-phone-number-validation-fea
|
|
|
701
746
|
|
|
702
747
|
### `isValidPhoneNumberForCountry(input: string, country: string): boolean`
|
|
703
748
|
|
|
704
|
-
Same as `isValidPhoneNumber()` but with the "default country" argument being an "exact country"
|
|
749
|
+
Same as `isValidPhoneNumber()` but with the "default country" argument being replaced with an "exact country", which is more strict.
|
|
705
750
|
|
|
706
|
-
This function is not currently exported from this library. The reason is that its result would be too vague when `false`
|
|
751
|
+
This function is not currently exported from this library. The reason is that its result would be too vague when it returns `false` — it could mean any of:
|
|
707
752
|
|
|
708
753
|
* The input is not a valid phone number.
|
|
709
754
|
* The input is a valid phone number but it belongs to another country.
|
|
710
755
|
* The input is a phone number that belongs to the correct country but is not valid.
|
|
711
756
|
|
|
712
|
-
At least the second case should be handled separately from a "User Experience" point of view: if the user has input a valid phone number but for another country, they should be notified that "
|
|
757
|
+
At least the second case should be handled separately from a "User Experience" point of view: if the user has input a valid phone number but for another country, they should be notified that "perhaps you meant another country" rather than just throwing "the phone number is incorrect" error in their face.
|
|
713
758
|
|
|
714
759
|
But for those who'd still like to have such function, here's a possible implementation for it:
|
|
715
760
|
|
|
@@ -770,7 +815,7 @@ validatePhoneNumberLength('444 1 4444444444', 'TR') === 'TOO_LONG'
|
|
|
770
815
|
|
|
771
816
|
For the description of the `defaultCountry?: string | options?: object` argument, see [`parsePhoneNumber()`](#parsephonenumberstring-defaultcountry-string--options-object-phonenumber) function description.
|
|
772
817
|
|
|
773
|
-
This function is just a more detailed version of `isPossiblePhoneNumber()` for those who
|
|
818
|
+
This function is just a more detailed version of `isPossiblePhoneNumber()` for those who [asked](https://github.com/catamphetamine/libphonenumber-js/issues/406) for a more specific rejection reason.
|
|
774
819
|
|
|
775
820
|
<!-- This function is just a shortcut for a two-step process of ["strictly"](#strictness) parsing a phone number and then calling `.validateLength()`. -->
|
|
776
821
|
|
|
@@ -778,7 +823,7 @@ The phone number is parsed ["strictly"](#strictness) from the input string.
|
|
|
778
823
|
|
|
779
824
|
### `class` AsYouType(defaultCountry?: string | options?: object)
|
|
780
825
|
|
|
781
|
-
Creates a formatter for a partially
|
|
826
|
+
Creates a formatter for a partially-entered phone number.
|
|
782
827
|
|
|
783
828
|
For the description of the `defaultCountry?: string | options?: object` argument, see [`parsePhoneNumber()`](#parsephonenumberstring-defaultcountry-string--options-object-phonenumber) function description.
|
|
784
829
|
|
|
@@ -795,13 +840,13 @@ new AsYouType('US').input('2133734') === '(213) 373-4'
|
|
|
795
840
|
|
|
796
841
|
The formatter instance also provides the following getters:
|
|
797
842
|
|
|
798
|
-
* `getNumber(): PhoneNumber?` — Returns the [`PhoneNumber`](#phonenumber). Will return `undefined` if no [national (significant) number](#national-significant-number) digits have been entered so far, or if no `defaultCountry`/`defaultCallingCode` has been
|
|
843
|
+
* `getNumber(): PhoneNumber?` — Returns the [`PhoneNumber`](#phonenumber). Will return `undefined` if no [national (significant) number](#national-significant-number) digits have been entered so far, or if no `defaultCountry`/`defaultCallingCode` has been specified and the user enters a phone number in national format (without a `+`).
|
|
799
844
|
|
|
800
|
-
* `getNumberValue(): string?` — Returns the phone number in [`E.164`](https://en.wikipedia.org/wiki/E.164) format. For example, for country `"US"` and input `"(222) 333-4444"` it will return `"+12223334444"`. Will return `undefined` if no digits have been input, or when
|
|
845
|
+
* `getNumberValue(): string?` — Returns the phone number in [`E.164`](https://en.wikipedia.org/wiki/E.164) format. For example, for default country `"US"` and input `"(222) 333-4444"` it will return `"+12223334444"`. Will return `undefined` if no digits have been input, or when the user inputs a phone number in national format (without a `+`) and no default country or default "country calling code" have been specified.
|
|
801
846
|
|
|
802
|
-
* `getChars(): string` — Returns the phone number characters
|
|
847
|
+
* `getChars(): string` — Returns the phone number characters: `+` sign (if present) and the digits. Returns an empty string if no phone number characters have been input.
|
|
803
848
|
|
|
804
|
-
* `getTemplate(): string` — Returns the template used to format the phone number characters
|
|
849
|
+
* `getTemplate(): string` — Returns the template that is used to format the phone number characters — `+` sign (if present) and the digits — which are designated by `x`-es. Returns an empty string if no phone number characters have been input.
|
|
805
850
|
|
|
806
851
|
```js
|
|
807
852
|
// National phone number input example.
|
|
@@ -844,9 +889,9 @@ asYouType.getTemplate() === 'xx xxx xxx xxxx'
|
|
|
844
889
|
|
|
845
890
|
* `getCountry(): string?` — Returns a two-letter [country code](#country-code) of the phone number. Returns `undefined` for ["non-geographic"](#non-geographic) phone numbering plans. Returns `undefined` if no phone number has been input yet, or if it couldn't tell what country the phone number belongs to — that could happen when several countries have the same "country calling code" and the phone number doesn't look like it belongs to any particular one of them.
|
|
846
891
|
|
|
847
|
-
* `isPossible(): boolean` — Returns `true` if the phone number is "possible". Is just a shortcut for [`
|
|
892
|
+
* `isPossible(): boolean` — Returns `true` if the phone number is "possible". Is just a shortcut for [`getNumber()?.isPossible()`](#ispossible-boolean).
|
|
848
893
|
|
|
849
|
-
* `isValid(): boolean` — Returns `true` if the phone number is "valid". Is just a shortcut for [`
|
|
894
|
+
* `isValid(): boolean` — Returns `true` if the phone number is "valid". Is just a shortcut for [`getNumber()?.isValid()`](#isvalid-boolean).
|
|
850
895
|
|
|
851
896
|
<details>
|
|
852
897
|
<summary>Legacy API (before version <code>1.6.0</code>)</summary>
|
|
@@ -889,13 +934,13 @@ asYouType.getNationalNumber() === '2133734253'
|
|
|
889
934
|
|
|
890
935
|
####
|
|
891
936
|
|
|
892
|
-
"As You Type" formatter was created by Google as part of their Android OS and therefore only works for numerical keyboard input, i.e. it can only accept digits (and a `+` sign
|
|
937
|
+
"As You Type" formatter was created by Google as part of their Android OS and therefore only works for numerical keyboard input, i.e. it can only accept digits (and a `+` sign at the start of an international number). When used on desktops where a user can input all kinds of punctuation (spaces, dashes, parens, etc) it simply ignores everything except digits (and a `+` sign at the start of an international number).
|
|
893
938
|
|
|
894
|
-
Google's "As You Type" formatter does not support entering phone number extensions. If your project requires phone number extensions
|
|
939
|
+
Google's "As You Type" formatter does not support entering phone number extensions. If your project requires inputting phone number extensions then use a separate input field for that.
|
|
895
940
|
|
|
896
941
|
### findPhoneNumbersInText(text: string, defaultCountry?: string | options?: object): object[]
|
|
897
942
|
|
|
898
|
-
Searches for phone numbers in `text`.
|
|
943
|
+
Searches for complete phone numbers in `text`.
|
|
899
944
|
|
|
900
945
|
Available `options`:
|
|
901
946
|
|
|
@@ -971,7 +1016,7 @@ findNumbers(`
|
|
|
971
1016
|
|
|
972
1017
|
####
|
|
973
1018
|
|
|
974
|
-
By default it processes the whole text and then outputs the phone numbers found. If the text is very big (say, a hundred thousand characters) then it might freeze the user interface for a couple of seconds. To avoid such lags one
|
|
1019
|
+
By default it processes the whole text and then outputs the phone numbers found. If the text is very big (say, a hundred thousand characters) then it might freeze the user interface for a couple of seconds. To avoid such lags one could employ "iterator" approach using `searchPhoneNumbersInText()` to perform the search asynchronously (e.g. using `requestIdleCallback` or `requestAnimationFrame`).
|
|
975
1020
|
|
|
976
1021
|
(in previous versions, it was called `searchNumbers()`)
|
|
977
1022
|
|
|
@@ -1029,7 +1074,7 @@ iteration()
|
|
|
1029
1074
|
|
|
1030
1075
|
####
|
|
1031
1076
|
|
|
1032
|
-
Although Google's javascript port doesn't
|
|
1077
|
+
Although Google's javascript port doesn't provide the `findPhoneNumbersInText()` function, the Java and C++ ports do. I guess that's because Google just doesn't need to crawl phone numbers on Node.js because they do it in Java or C++. Still, javascript nowadays is the most popular programming language given its simplicity and user-friendliness. The `findPhoneNumbersInText()` function provided by this library is a port of Google's `PhoneNumberMatcher.java` into javascript.
|
|
1033
1078
|
|
|
1034
1079
|
### getExampleNumber(country: string, examples: object): PhoneNumber
|
|
1035
1080
|
|
|
@@ -1100,11 +1145,11 @@ parseIncompletePhoneNumber('+7 800 555') === '+7800555'
|
|
|
1100
1145
|
parseIncompletePhoneNumber('+٤٤٢٣٢٣٢٣٤') === '+442323234'
|
|
1101
1146
|
```
|
|
1102
1147
|
|
|
1103
|
-
### parsePhoneNumberCharacter(
|
|
1148
|
+
### parsePhoneNumberCharacter(character, prevParsedCharacters?, eventListener?)
|
|
1104
1149
|
|
|
1105
1150
|
Parses next character of an input string while parsing phone number digits (including a `+`) from that string. Basically, it discards everything except `+` and digits, and `+` is only allowed at the start of a phone number.
|
|
1106
1151
|
|
|
1107
|
-
This function is a low-level one that is currently only used in [`react-phone-number-input`](https://gitlab.com/catamphetamine/react-phone-number-input) with [`input-format`](https://gitlab.com/catamphetamine/input-format). Frankly speaking, that's the only reason why this function is exported. Other developers should just ignore it and use `parseIncompletePhoneNumber()` instead because it's much simpler.
|
|
1152
|
+
This function is a low-level one that is currently only used in [`react-phone-number-input`](https://gitlab.com/catamphetamine/react-phone-number-input) with [`input-format`](https://gitlab.com/catamphetamine/input-format). Frankly speaking, that's the only reason why this function is exported. Other developers, perhaps, should just ignore it and use `parseIncompletePhoneNumber()` instead because it's much simpler.
|
|
1108
1153
|
|
|
1109
1154
|
```js
|
|
1110
1155
|
// Suppose a user inputs a "+1 (213) 373-42-53" string
|
|
@@ -1132,7 +1177,7 @@ parsePhoneNumberCharacter('3', '+1213373425') === '3'
|
|
|
1132
1177
|
|
|
1133
1178
|
So basically, it's the same as `parseIncompletePhoneNumber()` with the only difference that it operates at a character-by-character level rather than at a string-as-a-whole level.
|
|
1134
1179
|
|
|
1135
|
-
The optional `
|
|
1180
|
+
The optional `eventListener` argument is a function of `eventName: string` argument. It will be called with an `"end"` argument in a situation when the application should stop parsing the input string. Currently, the only situation when that could happen is when it encounters an "out-of-place" `+` character. For example, when parsing a `"+1 (234) + 56-78"` string, it would emit an `"end"` event at the second `+` character so that the application could return `"+1234"` rather than `"+12345678"`.
|
|
1136
1181
|
|
|
1137
1182
|
### formatIncompletePhoneNumber(value: string, defaultCountry?: string | options?: object): string
|
|
1138
1183
|
|
|
@@ -1368,7 +1413,7 @@ formatNumber({}) throws Error
|
|
|
1368
1413
|
|
|
1369
1414
|
_(legacy API)_
|
|
1370
1415
|
|
|
1371
|
-
See the description for [`PhoneNumber.getType()`](#gettype).
|
|
1416
|
+
See the description for [`PhoneNumber.getType()`](#gettype-string).
|
|
1372
1417
|
|
|
1373
1418
|
The `number` argument can be either a result of the `parseNumber()` function call — `{ country, phone }` — or a string (phone number digits only) possibly accompanied with the second `defaultCountry` argument.
|
|
1374
1419
|
|
|
@@ -1439,9 +1484,9 @@ isValidNumberForRegion('07624369230', 'IM') === true
|
|
|
1439
1484
|
|
|
1440
1485
|
## Using phone number validation feature
|
|
1441
1486
|
|
|
1442
|
-
I personally don't use strict phone number validation
|
|
1487
|
+
I personally don't use strict phone number validation feature. The rationale is that telephone numbering plans can and sometimes do change, meaning that [`PhoneNumber.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.
|
|
1443
1488
|
|
|
1444
|
-
Whenever there's a "business requirement" to validate
|
|
1489
|
+
Whenever there's a "business requirement" to validate phone number input, I prefer using [`PhoneNumber.isPossible()`](#ispossible-boolean) instead of [`PhoneNumber.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 [`PhoneNumber.isValid()`](#isvalid-boolean) — maybe in your case it would make sense.
|
|
1445
1490
|
|
|
1446
1491
|
## React
|
|
1447
1492
|
|
|
@@ -1455,33 +1500,35 @@ This package currently [doesn't provide](https://gitlab.com/catamphetamine/libph
|
|
|
1455
1500
|
|
|
1456
1501
|
## Bug reporting
|
|
1457
1502
|
|
|
1458
|
-
This library is a simpler rewrite of Google's [`libphonenumber`](https://github.com/google/libphonenumber/blob/master/java/libphonenumber/) library. Basically, it mimicks Google's library behavior. Hence, as long as this library's [demo page](https://catamphetamine.gitlab.io/libphonenumber-js/) and Google's library [demo page](https://libphonenumber.appspot.com/) exhibit the same behavior, it's not considered a bug.
|
|
1503
|
+
This library is a simpler rewrite of Google's [`libphonenumber`](https://github.com/google/libphonenumber/blob/master/java/libphonenumber/) library. Basically, it mimicks Google's library behavior. Hence, as long as this library's [demo page](https://catamphetamine.gitlab.io/libphonenumber-js/) and Google's library [demo page](https://libphonenumber.appspot.com/) exhibit the same behavior, it's not considered a bug, regardless of whether that behavior contradicts any new changes in telephone numbering plans, etc. In that case, your only option would be to [report the issue to Google](https://github.com/google/libphonenumber/blob/master/CONTRIBUTING.md) directly.
|
|
1459
1504
|
|
|
1460
|
-
|
|
1505
|
+
That being said, if the two demo pages exhibit different behavior and it's not caused by the intentional [differences](#differences-from-googles-libphonenumber), then it would be considered a valid case for reporting an issue.
|
|
1461
1506
|
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
*
|
|
1465
|
-
*
|
|
1507
|
+
When reporting issues related to:
|
|
1508
|
+
|
|
1509
|
+
* Parsing numbers
|
|
1510
|
+
* Validating numbers
|
|
1511
|
+
* Formatting numbers
|
|
1512
|
+
* "As You Type" formatter
|
|
1466
1513
|
|
|
1467
1514
|
one must provide two links:
|
|
1468
1515
|
|
|
1469
1516
|
* A link to the [demo page](https://catamphetamine.gitlab.io/libphonenumber-js/) illustrating the supposedly incorrect behavior.
|
|
1470
1517
|
* A link to [Google's `libphonenumber` demo page](https://libphonenumber.appspot.com/) illustrating the expected behavior.
|
|
1471
1518
|
|
|
1472
|
-
Google's demo page [
|
|
1519
|
+
Google's demo page output is divided into [four sections](https://libphonenumber.appspot.com/phonenumberparser?number=438331999&country=AU):
|
|
1473
1520
|
* "Parsing Result"
|
|
1474
1521
|
* "Validation Results"
|
|
1475
1522
|
* "Formatting Results"
|
|
1476
1523
|
* "AsYouTypeFormatter Results"
|
|
1477
1524
|
|
|
1478
|
-
|
|
1525
|
+
Depending the type of bug you're reporting, mention the relevant section of the demo page output in your ticket.
|
|
1479
1526
|
|
|
1480
|
-
|
|
1527
|
+
When writing a bug report:
|
|
1481
1528
|
|
|
1482
1529
|
* First, describe the observed `libphonenumber-js` demo result (and provide a link to it).
|
|
1483
1530
|
* Second, describe the observed Google's demo result (and provide a link to it).
|
|
1484
|
-
* The described observed result of Google's demo must be different from the described observed result of `libphonenumber-js` demo, otherwise it's not considered a bug. If you don't agree with Google's demo result then
|
|
1531
|
+
* The described observed result of Google's demo must be different from the described observed result of `libphonenumber-js` demo, otherwise it's not considered a bug. If you don't agree with Google's demo result then [report it to Google](https://github.com/google/libphonenumber/blob/master/CONTRIBUTING.md) directly instead. If they fix it in their library, I'll port the fix to this library.
|
|
1485
1532
|
|
|
1486
1533
|
<!--
|
|
1487
1534
|
Phone number validation bugs should **only** be reported if they appear when using [custom metadata functions](#customizing-metadata) fed with `metadata.max.json` because by default all functions in this library use the reduced metadata set which results in looser validation than the original Google `libphonenumber`'s. The [demo page](https://catamphetamine.gitlab.io/libphonenumber-js/) also uses the reduced metadata set and therefore its validation is also looser than the original Google `libphonenumber`'s.
|
|
@@ -1491,17 +1538,13 @@ There is also a possibility of this library's demo metadata being outdated, or t
|
|
|
1491
1538
|
|
|
1492
1539
|
<!-- When reporting `findPhoneNumbersInText()` bugs, one should know that `findPhoneNumbersInText()` code was ported from [Google's Java code](https://github.com/googlei18n/libphonenumber/blob/master/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java). I didn't write it myself, I just ported it. Therefore, it is unlikely that anyone other than Google would fix any bugs. -->
|
|
1493
1540
|
|
|
1494
|
-
## TypeScript
|
|
1495
|
-
|
|
1496
|
-
This library comes with TypeScript "typings". If you happen to find any bugs in those, create an issue.
|
|
1497
|
-
|
|
1498
1541
|
<!--
|
|
1499
1542
|
https://codesandbox.io/s/damp-fast-26c7z?file=/src/index.ts
|
|
1500
1543
|
-->
|
|
1501
1544
|
|
|
1502
1545
|
## CDN
|
|
1503
1546
|
|
|
1504
|
-
|
|
1547
|
+
To include this library directly via a `<script/>` tag on a page, one can use any npm CDN service, e.g. [unpkg.com](https://unpkg.com) or [jsdelivr.com](https://jsdelivr.com)
|
|
1505
1548
|
|
|
1506
1549
|
```html
|
|
1507
1550
|
<script src="https://unpkg.com/libphonenumber-js@[version]/bundle/libphonenumber-[type].js"></script>
|
|
@@ -1589,7 +1632,7 @@ import min from 'libphonenumber-js/min/metadata'
|
|
|
1589
1632
|
const metadata = new Metadata(min)
|
|
1590
1633
|
```
|
|
1591
1634
|
|
|
1592
|
-
As one can see, the [`Metadata` class](https://gitlab.com/catamphetamine/libphonenumber-js/-/blob/master/source/metadata.js) is not documented much. Partially, that's because its usage is not necessarily encouraged, but it's still used, for example, in [`react-phone-number-input`](https://gitlab.com/catamphetamine/react-phone-number-input/-/blob/master/source/helpers/phoneInputHelpers.js) to get "leading digits" for a country, or to get maximum phone number length for a country. Stick to the methods documented above
|
|
1635
|
+
As one can see, the [`Metadata` class](https://gitlab.com/catamphetamine/libphonenumber-js/-/blob/master/source/metadata.js) is not documented much. Partially, that's because its usage is not necessarily encouraged, but it's still used, for example, in [`react-phone-number-input`](https://gitlab.com/catamphetamine/react-phone-number-input/-/blob/master/source/helpers/phoneInputHelpers.js) to get the "leading digits" for a country, or to get the maximum phone number length for a country. Stick to the methods documented above and don't call any other methods. If you think there's a need to call any other methods not mentioned above, create an issue with a discussion.
|
|
1593
1636
|
|
|
1594
1637
|
<!--
|
|
1595
1638
|
Currently I have a script set up monitoring changes to `PhoneNumberMetadata.xml` in Google's repo and automatically releasing new versions of this library when metadata in Google's repo gets updated. So this library's metadata is supposed to be up-to-date. Still, in case the automatic metadata update script malfunctions some day, anyone can request metadata update via a Pull Request here on GitHub:
|
|
@@ -1612,12 +1655,12 @@ Alternatively, a developer may wish to update metadata urgently, without waiting
|
|
|
1612
1655
|
|
|
1613
1656
|
This library comes prepackaged with [three types of metadata](#min-vs-max-vs-mobile-vs-core).
|
|
1614
1657
|
|
|
1615
|
-
Sometimes, if only a specific set of countries is needed in a project, and a developer really wants to reduce the resulting bundle size, say, by 50 kilobytes
|
|
1658
|
+
Sometimes, if only a specific set of countries is needed in a project, and a developer really wants to reduce the resulting bundle size, say, by 50 kilobytes, while still including all regular expressions for validating phone number digits and detecting phone number type, then they can generate such custom metadata and pass it as the last argument to this library's "core" functions.
|
|
1616
1659
|
|
|
1617
|
-
See [generate custom metadata
|
|
1660
|
+
See [generate custom metadata](https://gitlab.com/catamphetamine/libphonenumber-metadata-generator) instructions.
|
|
1618
1661
|
|
|
1619
1662
|
<details>
|
|
1620
|
-
<summary>
|
|
1663
|
+
<summary>How to use the generated <code>metadata.custom.json</code> file with the "core" functions.</summary>
|
|
1621
1664
|
|
|
1622
1665
|
####
|
|
1623
1666
|
|
|
@@ -1753,7 +1796,9 @@ exports.AsYouType.prototype.constructor = exports.AsYouType
|
|
|
1753
1796
|
```
|
|
1754
1797
|
</details>
|
|
1755
1798
|
|
|
1756
|
-
|
|
1799
|
+
######
|
|
1800
|
+
|
|
1801
|
+
Note that if you'll be using your own "custom" metadata then you're responsible for keeping it up-to-date because Google regularly updates their metadata.
|
|
1757
1802
|
|
|
1758
1803
|
<!-- ## To do -->
|
|
1759
1804
|
|
|
@@ -1814,55 +1859,58 @@ launchctl list | grep 'libphonenumber-js'
|
|
|
1814
1859
|
|
|
1815
1860
|
### Maintenance
|
|
1816
1861
|
|
|
1817
|
-
Google periodically releases new metadata with the changes described in
|
|
1862
|
+
Google periodically releases new metadata with the changes described in their [release notes](https://github.com/googlei18n/libphonenumber/blob/master/release_notes.txt). Most of the times, those are minor non-breaking updates. Rarely, those could be major-version breaking changes.
|
|
1863
|
+
|
|
1864
|
+
After Google does that, this library pulls the updated metadata from Google's repository and publishes a new version of itself on `npm`.
|
|
1865
|
+
|
|
1866
|
+
The metadata pulling process is automated through an "autoupdate" script: see `autoupdate.cmd` (Windows) or `autoupdate.sh` (Linux/macOS). The script detects changes to `PhoneNumberMetadata.xml` file in Google `libphonenumber`'s repo and, if there are any changes, it pulls the latest metadata, transforms it, commits the changes to the repository, builds a new version of the package and releases it to `npm`.
|
|
1818
1867
|
|
|
1819
|
-
|
|
1868
|
+
I did attempt to set up the autoupdate script to run dialy on my Windows machine in an automatic fashion through "Task Scheduler", and even overcame the issue of `ssh-agent` asking for a password input every time when running `git` command, but then `npm` started requiring "two-factor authentication" in order to publish a package, which requires human intervention, so the autoupdate process can't really be 100% autonomous and automatic and has to be run by a human's hand every now-and-then.
|
|
1820
1869
|
|
|
1821
|
-
|
|
1870
|
+
So I just run the "autoupdate" script manually from time to time. With this workflow, one can see how the metadata could potentially get a bit stale, in which case just ping me to re-run the autoupdate script, assuming I'm still alive and well.
|
|
1822
1871
|
|
|
1823
|
-
|
|
1824
|
-
* [`AsYouTypeFormatter.java`](https://github.com/google/libphonenumber/blob/master/java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java) — `AsYouType`
|
|
1825
|
-
* [`PhoneNumberMatcher.java`](https://github.com/googlei18n/libphonenumber/blob/master/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java) — `findPhoneNumbersInText()`
|
|
1872
|
+
Also Google sometimes (extremely rarely) updates their code:
|
|
1826
1873
|
|
|
1827
|
-
|
|
1874
|
+
* [`phonenumberutil.js`](https://github.com/googlei18n/libphonenumber/blob/master/javascript/i18n/phonenumbers/phonenumberutil.js) — is mirrored as functions: `parseNumber()`, `formatNumber()`, `isValidNumber()`, `getNumberType()`
|
|
1875
|
+
* [`AsYouTypeFormatter.java`](https://github.com/google/libphonenumber/blob/master/java/libphonenumber/src/com/google/i18n/phonenumbers/AsYouTypeFormatter.java) — is mirrored as `AsYouType` class
|
|
1876
|
+
* [`PhoneNumberMatcher.java`](https://github.com/googlei18n/libphonenumber/blob/master/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java) — is mirrored as `findPhoneNumbersInText()` function
|
|
1877
|
+
|
|
1878
|
+
The latest sync-up with Google's code was on Sep 19th, 2025.
|
|
1828
1879
|
|
|
1829
1880
|
## Contributing
|
|
1830
1881
|
|
|
1831
|
-
After cloning this repo,
|
|
1882
|
+
After cloning this repo, install the dependencies:
|
|
1832
1883
|
|
|
1833
1884
|
```sh
|
|
1834
1885
|
npm install
|
|
1835
1886
|
```
|
|
1836
1887
|
|
|
1837
|
-
This
|
|
1838
|
-
transpilation
|
|
1888
|
+
This library is written in ES6 and uses [Babel](http://babeljs.io/) for ES5
|
|
1889
|
+
transpilation during the "build" step:
|
|
1839
1890
|
|
|
1840
1891
|
```sh
|
|
1841
1892
|
npm run build
|
|
1842
1893
|
```
|
|
1843
1894
|
|
|
1844
|
-
|
|
1845
|
-
node.
|
|
1846
|
-
|
|
1847
|
-
After developing, the full test suite can be evaluated by running:
|
|
1895
|
+
After making any code changes, run the tests:
|
|
1848
1896
|
|
|
1849
1897
|
```sh
|
|
1850
1898
|
npm test
|
|
1851
1899
|
```
|
|
1852
1900
|
|
|
1853
|
-
|
|
1901
|
+
Aside from all tests passing, it should also remain at 100% of code coverage. At some point, the coverage analysis tools got buggy and stopped reporting the coverage correctly. Anyway, a code coverage report could be generated by running the following command:
|
|
1854
1902
|
|
|
1855
1903
|
```sh
|
|
1856
1904
|
npm run test-coverage
|
|
1857
1905
|
```
|
|
1858
1906
|
|
|
1859
|
-
|
|
1907
|
+
To test the updated code on a real project before publishing a new release, one could use "pack-and-install" trick to simulate publishing a new release.
|
|
1860
1908
|
|
|
1861
1909
|
```sh
|
|
1862
1910
|
npm pack
|
|
1863
1911
|
```
|
|
1864
1912
|
|
|
1865
|
-
It will `build`, `test` and then create a `.tgz` archive
|
|
1913
|
+
It will `build`, `test` and then create a `.tgz` archive that can be installed from a project folder just like a normal `npm` package.
|
|
1866
1914
|
|
|
1867
1915
|
```sh
|
|
1868
1916
|
npm install [module name with version].tar.gz
|
|
@@ -1886,16 +1934,19 @@ npm run test-coverage
|
|
|
1886
1934
|
|
|
1887
1935
|
The code coverage report can be viewed by opening `./coverage/lcov-report/index.html`.
|
|
1888
1936
|
|
|
1937
|
+
At some point, the coverage analysis tools got buggy and stopped reporting the coverage correctly.
|
|
1938
|
+
|
|
1889
1939
|
## Advertisement
|
|
1890
1940
|
|
|
1891
1941
|
If you like this library then you might also like:
|
|
1892
1942
|
|
|
1893
1943
|
* [`react-phone-number-input`](https://npmjs.com/package/react-phone-number-input) — A `React` component for phone number input.
|
|
1894
1944
|
* [`javascript-time-ago`](https://npmjs.com/package/javascript-time-ago) — An international human-readable past or future date formatter. Example: `"2 days ago"`.
|
|
1895
|
-
* [`react-time-ago`](https://npmjs.com/package/react-time-ago) — A `React` component for international human-readable
|
|
1945
|
+
* [`react-time-ago`](https://npmjs.com/package/react-time-ago) — A `React` component for international human-readable past or future date formatter. Example: `"2 days ago"`.
|
|
1896
1946
|
* [`read-excel-file`](https://www.npmjs.com/package/read-excel-file) — A simple and easy-to-use `*.xlsx` file reader (client-side or server-side).
|
|
1897
1947
|
* [`write-excel-file`](https://www.npmjs.com/package/write-excel-file) — A simple and easy-to-use `*.xlsx` file writer (client-side or server-side).
|
|
1898
1948
|
* [`flexible-json-schema`](https://www.npmjs.com/package/flexible-json-schema) — A simple and easy-to-use `*.json` schema data validator / parser.
|
|
1949
|
+
* [`navigation-stack`](https://www.npmjs.com/package/navigation-stack) — Navigation in a Single-Page Application.
|
|
1899
1950
|
* [`virtual-scroller`](https://www.npmjs.com/package/virtual-scroller) — A universal implementation of a "virtual scroller" infinite list scrolling component: only renders the rows that fit the screen bounds.
|
|
1900
1951
|
|
|
1901
1952
|
## License
|