libphonenumber-js 1.12.43 → 1.13.1
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 +131 -104
- package/README.md +141 -81
- package/core/es6/index.d.cts +2 -0
- package/core/es6/index.d.ts +2 -0
- package/core/es6/index.js +39 -0
- package/core/es6/package.json +12 -0
- package/core/index.cjs +5 -0
- package/core/index.cjs.js +5 -0
- package/core/index.d.cts +3 -1
- package/core/index.d.ts +3 -1
- package/core/index.js +5 -0
- package/custom.d.ts +6 -4
- package/custom.js +5 -5
- package/es6-modern/AsYouType.js +445 -0
- package/es6-modern/AsYouType.js.map +1 -0
- package/es6-modern/AsYouType.test.js.map +1 -0
- package/es6-modern/AsYouTypeFormatter.PatternMatcher.js +195 -0
- package/es6-modern/AsYouTypeFormatter.PatternMatcher.js.map +1 -0
- package/es6-modern/AsYouTypeFormatter.PatternMatcher.test.js.map +1 -0
- package/es6-modern/AsYouTypeFormatter.PatternParser.js +184 -0
- package/es6-modern/AsYouTypeFormatter.PatternParser.js.map +1 -0
- package/es6-modern/AsYouTypeFormatter.PatternParser.test.js.map +1 -0
- package/es6-modern/AsYouTypeFormatter.complete.js +120 -0
- package/es6-modern/AsYouTypeFormatter.complete.js.map +1 -0
- package/es6-modern/AsYouTypeFormatter.js +659 -0
- package/es6-modern/AsYouTypeFormatter.js.map +1 -0
- package/es6-modern/AsYouTypeFormatter.util.js +96 -0
- package/es6-modern/AsYouTypeFormatter.util.js.map +1 -0
- package/es6-modern/AsYouTypeFormatter.util.test.js.map +1 -0
- package/es6-modern/AsYouTypeParser.js +435 -0
- package/es6-modern/AsYouTypeParser.js.map +1 -0
- package/es6-modern/AsYouTypeState.js +104 -0
- package/es6-modern/AsYouTypeState.js.map +1 -0
- package/es6-modern/ParseError.js +15 -0
- package/es6-modern/ParseError.js.map +1 -0
- package/es6-modern/PhoneNumber.js +166 -0
- package/es6-modern/PhoneNumber.js.map +1 -0
- package/es6-modern/PhoneNumber.test.js.map +1 -0
- package/es6-modern/PhoneNumberMatcher.js +330 -0
- package/es6-modern/PhoneNumberMatcher.js.map +1 -0
- package/es6-modern/PhoneNumberMatcher.test.js.map +1 -0
- package/es6-modern/constants.js +31 -0
- package/es6-modern/constants.js.map +1 -0
- package/es6-modern/findNumbers/LRUCache.js +98 -0
- package/es6-modern/findNumbers/LRUCache.js.map +1 -0
- package/es6-modern/findNumbers/Leniency.js +311 -0
- package/es6-modern/findNumbers/Leniency.js.map +1 -0
- package/es6-modern/findNumbers/RegExpCache.js +20 -0
- package/es6-modern/findNumbers/RegExpCache.js.map +1 -0
- package/es6-modern/findNumbers/isValidCandidate.js +66 -0
- package/es6-modern/findNumbers/isValidCandidate.js.map +1 -0
- package/es6-modern/findNumbers/isValidPreCandidate.js +26 -0
- package/es6-modern/findNumbers/isValidPreCandidate.js.map +1 -0
- package/es6-modern/findNumbers/matchPhoneNumberStringAgainstPhoneNumber.js +63 -0
- package/es6-modern/findNumbers/matchPhoneNumberStringAgainstPhoneNumber.js.map +1 -0
- package/es6-modern/findNumbers/parsePreCandidate.js +18 -0
- package/es6-modern/findNumbers/parsePreCandidate.js.map +1 -0
- package/es6-modern/findNumbers/utf-8.js +63 -0
- package/es6-modern/findNumbers/utf-8.js.map +1 -0
- package/es6-modern/findNumbers/util.js +26 -0
- package/es6-modern/findNumbers/util.js.map +1 -0
- package/es6-modern/findNumbers/util.test.js.map +1 -0
- package/es6-modern/findPhoneNumbersInText.js +19 -0
- package/es6-modern/findPhoneNumbersInText.js.map +1 -0
- package/es6-modern/findPhoneNumbersInText.test.js.map +1 -0
- package/es6-modern/format.js +204 -0
- package/es6-modern/format.js.map +1 -0
- package/es6-modern/format.test.js.map +1 -0
- package/es6-modern/formatIncompletePhoneNumber.js +18 -0
- package/es6-modern/formatIncompletePhoneNumber.js.map +1 -0
- package/es6-modern/formatIncompletePhoneNumber.test.js.map +1 -0
- package/es6-modern/formatPhoneNumberForMobileDialing.js +190 -0
- package/es6-modern/formatPhoneNumberForMobileDialing.js.map +1 -0
- package/es6-modern/getCountries.js +5 -0
- package/es6-modern/getCountries.js.map +1 -0
- package/es6-modern/getCountries.test.js.map +1 -0
- package/es6-modern/getCountryCallingCode.js +3 -0
- package/es6-modern/getCountryCallingCode.js.map +1 -0
- package/es6-modern/getCountryCallingCode.test.js.map +1 -0
- package/es6-modern/getExampleNumber.js +7 -0
- package/es6-modern/getExampleNumber.js.map +1 -0
- package/es6-modern/getExampleNumber.test.js.map +1 -0
- package/es6-modern/helpers/RFC3966.js +63 -0
- package/es6-modern/helpers/RFC3966.js.map +1 -0
- package/es6-modern/helpers/RFC3966.test.js.map +1 -0
- package/es6-modern/helpers/applyInternationalSeparatorStyle.js +36 -0
- package/es6-modern/helpers/applyInternationalSeparatorStyle.js.map +1 -0
- package/es6-modern/helpers/applyInternationalSeparatorStyle.test.js.map +1 -0
- package/es6-modern/helpers/checkNumberLength.js +112 -0
- package/es6-modern/helpers/checkNumberLength.js.map +1 -0
- package/es6-modern/helpers/checkNumberLength.test.js.map +1 -0
- package/es6-modern/helpers/extension/createExtensionPattern.js +93 -0
- package/es6-modern/helpers/extension/createExtensionPattern.js.map +1 -0
- package/es6-modern/helpers/extension/extractExtension.js +30 -0
- package/es6-modern/helpers/extension/extractExtension.js.map +1 -0
- package/es6-modern/helpers/extractCountryCallingCode.js +142 -0
- package/es6-modern/helpers/extractCountryCallingCode.js.map +1 -0
- package/es6-modern/helpers/extractCountryCallingCode.test.js.map +1 -0
- package/es6-modern/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +49 -0
- package/es6-modern/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map +1 -0
- package/es6-modern/helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js +66 -0
- package/es6-modern/helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js.map +1 -0
- package/es6-modern/helpers/extractNationalNumber.js +117 -0
- package/es6-modern/helpers/extractNationalNumber.js.map +1 -0
- package/es6-modern/helpers/extractNationalNumber.test.js.map +1 -0
- package/es6-modern/helpers/extractNationalNumberFromPossiblyIncompleteNumber.js +102 -0
- package/es6-modern/helpers/extractNationalNumberFromPossiblyIncompleteNumber.js.map +1 -0
- package/es6-modern/helpers/extractNationalNumberFromPossiblyIncompleteNumber.test.js.map +1 -0
- package/es6-modern/helpers/extractPhoneContext.js +82 -0
- package/es6-modern/helpers/extractPhoneContext.js.map +1 -0
- package/es6-modern/helpers/extractPhoneContext.test.js.map +1 -0
- package/es6-modern/helpers/formatNationalNumberUsingFormat.js +34 -0
- package/es6-modern/helpers/formatNationalNumberUsingFormat.js.map +1 -0
- package/es6-modern/helpers/getCountryByCallingCode.js +30 -0
- package/es6-modern/helpers/getCountryByCallingCode.js.map +1 -0
- package/es6-modern/helpers/getCountryByNationalNumber.js +76 -0
- package/es6-modern/helpers/getCountryByNationalNumber.js.map +1 -0
- package/es6-modern/helpers/getIddPrefix.js +26 -0
- package/es6-modern/helpers/getIddPrefix.js.map +1 -0
- package/es6-modern/helpers/getNumberType.js +81 -0
- package/es6-modern/helpers/getNumberType.js.map +1 -0
- package/es6-modern/helpers/getNumberType.test.js.map +1 -0
- package/es6-modern/helpers/getPossibleCountriesForNumber.js +28 -0
- package/es6-modern/helpers/getPossibleCountriesForNumber.js.map +1 -0
- package/es6-modern/helpers/isObject.js +5 -0
- package/es6-modern/helpers/isObject.js.map +1 -0
- package/es6-modern/helpers/isViablePhoneNumber.js +77 -0
- package/es6-modern/helpers/isViablePhoneNumber.js.map +1 -0
- package/es6-modern/helpers/matchesEntirely.js +12 -0
- package/es6-modern/helpers/matchesEntirely.js.map +1 -0
- package/es6-modern/helpers/matchesEntirely.test.js.map +1 -0
- package/es6-modern/helpers/mergeArrays.js +23 -0
- package/es6-modern/helpers/mergeArrays.js.map +1 -0
- package/es6-modern/helpers/mergeArrays.test.js.map +1 -0
- package/es6-modern/helpers/parseDigits.js +111 -0
- package/es6-modern/helpers/parseDigits.js.map +1 -0
- package/es6-modern/helpers/parseDigits.test.js.map +1 -0
- package/es6-modern/helpers/stripIddPrefix.js +29 -0
- package/es6-modern/helpers/stripIddPrefix.js.map +1 -0
- package/es6-modern/helpers/stripIddPrefix.test.js.map +1 -0
- package/es6-modern/isPossible.js +77 -0
- package/es6-modern/isPossible.js.map +1 -0
- package/es6-modern/isPossible.test.js.map +1 -0
- package/es6-modern/isPossiblePhoneNumber.js +16 -0
- package/es6-modern/isPossiblePhoneNumber.js.map +1 -0
- package/es6-modern/isPossiblePhoneNumber.test.js.map +1 -0
- package/es6-modern/isValid.js +57 -0
- package/es6-modern/isValid.js.map +1 -0
- package/es6-modern/isValid.test.js.map +1 -0
- package/es6-modern/isValidPhoneNumber.js +16 -0
- package/es6-modern/isValidPhoneNumber.js.map +1 -0
- package/es6-modern/isValidPhoneNumber.test.js.map +1 -0
- package/es6-modern/legacy/PhoneNumberSearch.js +109 -0
- package/es6-modern/legacy/PhoneNumberSearch.js.map +1 -0
- package/es6-modern/legacy/findNumbers.js +16 -0
- package/es6-modern/legacy/findNumbers.js.map +1 -0
- package/es6-modern/legacy/findNumbers.test.js.map +1 -0
- package/es6-modern/legacy/findPhoneNumbers.js +26 -0
- package/es6-modern/legacy/findPhoneNumbers.js.map +1 -0
- package/es6-modern/legacy/findPhoneNumbers.test.js.map +1 -0
- package/es6-modern/legacy/findPhoneNumbersInitialImplementation.js +79 -0
- package/es6-modern/legacy/findPhoneNumbersInitialImplementation.js.map +1 -0
- package/es6-modern/legacy/format.js +97 -0
- package/es6-modern/legacy/format.js.map +1 -0
- package/es6-modern/legacy/format.test.js.map +1 -0
- package/es6-modern/legacy/getNumberType.js +104 -0
- package/es6-modern/legacy/getNumberType.js.map +1 -0
- package/es6-modern/legacy/getNumberType.test.js.map +1 -0
- package/es6-modern/legacy/isPossibleNumber.js +30 -0
- package/es6-modern/legacy/isPossibleNumber.js.map +1 -0
- package/es6-modern/legacy/isPossibleNumber.test.js.map +1 -0
- package/es6-modern/legacy/isValidNumber.js +17 -0
- package/es6-modern/legacy/isValidNumber.js.map +1 -0
- package/es6-modern/legacy/isValidNumber.test.js.map +1 -0
- package/es6-modern/legacy/isValidNumberForRegion.js +65 -0
- package/es6-modern/legacy/isValidNumberForRegion.js.map +1 -0
- package/es6-modern/legacy/isValidNumberForRegion.test.js.map +1 -0
- package/es6-modern/legacy/isValidNumberForRegion_.js +14 -0
- package/es6-modern/legacy/isValidNumberForRegion_.js.map +1 -0
- package/es6-modern/legacy/parse.js +11 -0
- package/es6-modern/legacy/parse.js.map +1 -0
- package/es6-modern/legacy/parse.test.js.map +1 -0
- package/es6-modern/legacy/searchNumbers.js +32 -0
- package/es6-modern/legacy/searchNumbers.js.map +1 -0
- package/es6-modern/legacy/searchNumbers.test.js.map +1 -0
- package/es6-modern/metadata.js +494 -0
- package/es6-modern/metadata.js.map +1 -0
- package/es6-modern/metadata.test.js.map +1 -0
- package/es6-modern/normalizeArguments.js +52 -0
- package/es6-modern/normalizeArguments.js.map +1 -0
- package/es6-modern/parse.js +341 -0
- package/es6-modern/parse.js.map +1 -0
- package/es6-modern/parse.test.js.map +1 -0
- package/es6-modern/parseIncompletePhoneNumber.js +70 -0
- package/es6-modern/parseIncompletePhoneNumber.js.map +1 -0
- package/es6-modern/parseIncompletePhoneNumber.test.js.map +1 -0
- package/es6-modern/parsePhoneNumber.js +11 -0
- package/es6-modern/parsePhoneNumber.js.map +1 -0
- package/es6-modern/parsePhoneNumber.test.js.map +1 -0
- package/es6-modern/parsePhoneNumberWithError.js +11 -0
- package/es6-modern/parsePhoneNumberWithError.js.map +1 -0
- package/es6-modern/parsePhoneNumberWithError.test.js.map +1 -0
- package/es6-modern/parsePhoneNumberWithError_.js +8 -0
- package/es6-modern/parsePhoneNumberWithError_.js.map +1 -0
- package/es6-modern/parsePhoneNumber_.js +24 -0
- package/es6-modern/parsePhoneNumber_.js.map +1 -0
- package/es6-modern/searchPhoneNumbersInText.js +31 -0
- package/es6-modern/searchPhoneNumbersInText.js.map +1 -0
- package/es6-modern/searchPhoneNumbersInText.test.js.map +1 -0
- package/es6-modern/tools/semver-compare.js +27 -0
- package/es6-modern/tools/semver-compare.js.map +1 -0
- package/es6-modern/tools/semver-compare.test.js.map +1 -0
- package/es6-modern/validatePhoneNumberLength.js +35 -0
- package/es6-modern/validatePhoneNumberLength.js.map +1 -0
- package/es6-modern/validatePhoneNumberLength.test.js.map +1 -0
- package/index.cjs +4 -0
- package/index.cjs.js +4 -0
- package/index.d.cts +5 -3
- package/index.d.ts +5 -3
- package/index.js +5 -0
- package/max/es6/exports/AsYouType.js +11 -0
- package/max/es6/exports/Metadata.js +11 -0
- package/max/es6/exports/PhoneNumber.js +11 -0
- package/max/es6/exports/PhoneNumberMatcher.js +11 -0
- package/max/es6/exports/findNumbers.js +6 -0
- package/max/es6/exports/findPhoneNumbersInText.js +6 -0
- package/max/es6/exports/formatIncompletePhoneNumber.js +6 -0
- package/max/es6/exports/getCountries.js +6 -0
- package/max/es6/exports/getCountryCallingCode.js +6 -0
- package/max/es6/exports/getExampleNumber.js +6 -0
- package/max/es6/exports/getExtPrefix.js +6 -0
- package/max/es6/exports/isPossiblePhoneNumber.js +6 -0
- package/max/es6/exports/isSupportedCountry.js +6 -0
- package/max/es6/exports/isValidPhoneNumber.js +6 -0
- package/max/es6/exports/parsePhoneNumber.js +7 -0
- package/max/es6/exports/parsePhoneNumberWithError.js +7 -0
- package/max/es6/exports/searchNumbers.js +6 -0
- package/max/es6/exports/searchPhoneNumbersInText.js +6 -0
- package/max/es6/exports/validatePhoneNumberLength.js +6 -0
- package/max/es6/exports/withMetadataArgument.js +13 -0
- package/max/es6/index.d.cts +2 -0
- package/max/es6/index.d.ts +2 -0
- package/max/es6/index.js +44 -0
- package/max/es6/package.json +12 -0
- package/max/index.cjs +5 -0
- package/max/index.cjs.js +5 -0
- package/max/index.d.cts +3 -1
- package/max/index.d.ts +3 -1
- package/max/index.js +4 -0
- package/min/es6/exports/AsYouType.js +11 -0
- package/min/es6/exports/Metadata.js +11 -0
- package/min/es6/exports/PhoneNumber.js +11 -0
- package/min/es6/exports/PhoneNumberMatcher.js +11 -0
- package/min/es6/exports/findNumbers.js +6 -0
- package/min/es6/exports/findPhoneNumbersInText.js +6 -0
- package/min/es6/exports/formatIncompletePhoneNumber.js +6 -0
- package/min/es6/exports/getCountries.js +6 -0
- package/min/es6/exports/getCountryCallingCode.js +6 -0
- package/min/es6/exports/getExampleNumber.js +6 -0
- package/min/es6/exports/getExtPrefix.js +6 -0
- package/min/es6/exports/isPossiblePhoneNumber.js +6 -0
- package/min/es6/exports/isSupportedCountry.js +6 -0
- package/min/es6/exports/isValidPhoneNumber.js +6 -0
- package/min/es6/exports/parsePhoneNumber.js +7 -0
- package/min/es6/exports/parsePhoneNumberWithError.js +7 -0
- package/min/es6/exports/searchNumbers.js +6 -0
- package/min/es6/exports/searchPhoneNumbersInText.js +6 -0
- package/min/es6/exports/validatePhoneNumberLength.js +6 -0
- package/min/es6/exports/withMetadataArgument.js +13 -0
- package/min/es6/index.d.cts +2 -0
- package/min/es6/index.d.ts +2 -0
- package/min/es6/index.js +44 -0
- package/min/es6/package.json +12 -0
- package/min/index.cjs +5 -0
- package/min/index.cjs.js +5 -0
- package/min/index.d.cts +3 -1
- package/min/index.d.ts +3 -1
- package/min/index.js +3 -0
- package/mobile/es6/exports/AsYouType.js +11 -0
- package/mobile/es6/exports/Metadata.js +11 -0
- package/mobile/es6/exports/PhoneNumber.js +11 -0
- package/mobile/es6/exports/PhoneNumberMatcher.js +11 -0
- package/mobile/es6/exports/findNumbers.js +6 -0
- package/mobile/es6/exports/findPhoneNumbersInText.js +6 -0
- package/mobile/es6/exports/formatIncompletePhoneNumber.js +6 -0
- package/mobile/es6/exports/getCountries.js +6 -0
- package/mobile/es6/exports/getCountryCallingCode.js +6 -0
- package/mobile/es6/exports/getExampleNumber.js +6 -0
- package/mobile/es6/exports/getExtPrefix.js +6 -0
- package/mobile/es6/exports/isPossiblePhoneNumber.js +6 -0
- package/mobile/es6/exports/isSupportedCountry.js +6 -0
- package/mobile/es6/exports/isValidPhoneNumber.js +6 -0
- package/mobile/es6/exports/parsePhoneNumber.js +7 -0
- package/mobile/es6/exports/parsePhoneNumberWithError.js +7 -0
- package/mobile/es6/exports/searchNumbers.js +6 -0
- package/mobile/es6/exports/searchPhoneNumbersInText.js +6 -0
- package/mobile/es6/exports/validatePhoneNumberLength.js +6 -0
- package/mobile/es6/exports/withMetadataArgument.js +13 -0
- package/mobile/es6/index.d.cts +2 -0
- package/mobile/es6/index.d.ts +2 -0
- package/mobile/es6/index.js +44 -0
- package/mobile/es6/package.json +12 -0
- package/mobile/index.cjs +5 -0
- package/mobile/index.cjs.js +5 -0
- package/mobile/index.d.cts +3 -1
- package/mobile/index.d.ts +3 -1
- package/mobile/index.js +4 -0
- package/package.json +28 -9
- package/types.d.cts +54 -21
- package/types.d.ts +54 -21
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>
|
|
30
|
+
|
|
31
|
+
######
|
|
28
32
|
|
|
29
|
-
|
|
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).
|
|
30
34
|
|
|
31
|
-
|
|
35
|
+
* Comes with TypeScript definitions.
|
|
32
36
|
|
|
33
|
-
|
|
37
|
+
* Can search for phone numbers in text. Google's autogenerated javascript port doesn't provide such feature for some reason.
|
|
34
38
|
|
|
35
|
-
|
|
39
|
+
* Focuses on parsing and formatting personal phone numbers while skipping any other "special" cases like:
|
|
36
40
|
|
|
37
|
-
|
|
41
|
+
* Emergency phone numbers like `911`.
|
|
38
42
|
|
|
39
|
-
|
|
43
|
+
* ["Short codes"](https://www.tatango.com/blog/sms-short-codes-what-every-business-needs-to-know/) — short SMS-only numbers like `12345`.
|
|
40
44
|
|
|
41
|
-
|
|
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 `*`.
|
|
42
46
|
|
|
43
|
-
|
|
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).
|
|
44
48
|
|
|
45
|
-
|
|
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.
|
|
46
50
|
|
|
47
|
-
|
|
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.
|
|
48
52
|
|
|
49
|
-
|
|
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.
|
|
50
54
|
|
|
51
|
-
|
|
55
|
+
* Doesn't provide "geolocation" feature when it can tell a city by a phone number.
|
|
52
56
|
|
|
53
|
-
|
|
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.
|
|
54
58
|
|
|
55
|
-
|
|
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.
|
|
56
60
|
|
|
57
|
-
|
|
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.
|
|
58
62
|
|
|
59
|
-
|
|
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,8 +246,8 @@ 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
253
|
`isPossiblePhoneNumber()` only validates phone number length, while `isValidPhoneNumber()` validates both phone number length and phone number digits.
|
|
@@ -271,37 +268,37 @@ import { findPhoneNumbersInText } from 'libphonenumber-js'
|
|
|
271
268
|
findPhoneNumbersInText(`
|
|
272
269
|
For tech support call +7 (800) 555-35-35 internationally
|
|
273
270
|
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
|
-
|
|
271
|
+
`, 'US') ===
|
|
272
|
+
[
|
|
273
|
+
{
|
|
274
|
+
number: PhoneNumber {
|
|
275
|
+
country: 'RU',
|
|
276
|
+
countryCallingCode: '7',
|
|
277
|
+
number: '+78005553535',
|
|
278
|
+
nationalNumber: '8005553535'
|
|
279
|
+
},
|
|
280
|
+
startsAt: 22,
|
|
281
|
+
endsAt: 40
|
|
282
|
+
},
|
|
283
|
+
{
|
|
284
|
+
number: PhoneNumber {
|
|
285
|
+
country: 'US',
|
|
286
|
+
countryCallingCode: '1',
|
|
287
|
+
number: '+12133734253',
|
|
288
|
+
nationalNumber: '2133734253',
|
|
289
|
+
ext: '1234'
|
|
290
|
+
},
|
|
291
|
+
startsAt: 86,
|
|
292
|
+
endsAt: 110
|
|
293
|
+
}
|
|
294
|
+
]
|
|
298
295
|
```
|
|
299
296
|
|
|
300
297
|
## "min" vs "max" vs "mobile" vs "core"
|
|
301
298
|
|
|
302
|
-
This
|
|
299
|
+
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
300
|
|
|
304
|
-
As one may guess, the complete list of those rules is huge, so this
|
|
301
|
+
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
302
|
|
|
306
303
|
* `min` — (default) The smallest metadata set
|
|
307
304
|
* Is about `80 kilobytes` in size (`libphonenumber-js/metadata.min.json` file)
|
|
@@ -323,21 +320,36 @@ As one may guess, the complete list of those rules is huge, so this library prov
|
|
|
323
320
|
* You need `max` metadata capabilities and you _only_ accept mobile numbers
|
|
324
321
|
* 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`.
|
|
325
322
|
|
|
323
|
+
* "custom" — (advanced) Create [your own](#custom-metadata) metadata that is even smaller in size by dropping support for most countries
|
|
324
|
+
* The size depends on the number of supported countries and the type of supported phone numbers
|
|
325
|
+
* Choose this when:
|
|
326
|
+
* You only need to support a handful of countries and you're absolutely obsessed with reducing the bundle size
|
|
327
|
+
|
|
326
328
|
Choose one from the above and then simply `import` the functions from the relevant sub-package:
|
|
327
329
|
|
|
328
|
-
* `min` — `libphonenumber-js/min`, or just `libphonenumber-js
|
|
330
|
+
* `min` — `libphonenumber-js/min`, or just `libphonenumber-js` (shorter alias)
|
|
329
331
|
* `max` — `libphonenumber-js/max`
|
|
330
332
|
* `mobile` — `libphonenumber-js/mobile`
|
|
333
|
+
* "custom" — `libphonenumber-js/core`
|
|
334
|
+
|
|
335
|
+
And if you're at the cutting edge of web development and you only need to support [ES6](https://caniuse.com/?search=es6)-compliant web browsers, which are basically any web browsers released since mid-2017, then just append `/es6` to the package name:
|
|
336
|
+
|
|
337
|
+
* `min` — `libphonenumber-js/min/es6`
|
|
338
|
+
* `max` — `libphonenumber-js/max/es6`
|
|
339
|
+
* `mobile` — `libphonenumber-js/mobile/es6`
|
|
340
|
+
* "custom" — `libphonenumber-js/core/es6`
|
|
331
341
|
|
|
332
342
|
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 their own ["custom"](#custom-metadata) 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.
|
|
333
343
|
|
|
334
344
|
## Definitions
|
|
335
345
|
|
|
346
|
+
This package introduces a few new terms in order describe its behavior more precisely.
|
|
347
|
+
|
|
336
348
|
### National (Significant) Number
|
|
337
349
|
|
|
338
|
-
"National (significant) number" is all national phone number digits
|
|
350
|
+
"National (significant) number" is all national phone number digits excluding the "national prefix". Examples:
|
|
339
351
|
* International number: `+1 213 373 4253`. Country: `"US"`. National number: `(213) 373-4253`. National (significant) number: `213 373 4253`
|
|
340
|
-
* 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)
|
|
352
|
+
* 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")
|
|
341
353
|
|
|
342
354
|
### Country Calling Code
|
|
343
355
|
|
|
@@ -349,7 +361,12 @@ In some cases, multiple countries could share the same "country calling code". F
|
|
|
349
361
|
|
|
350
362
|
### Non-Geographic
|
|
351
363
|
|
|
352
|
-
|
|
364
|
+
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`.
|
|
365
|
+
|
|
366
|
+
<details>
|
|
367
|
+
<summary>Here's a full list of "non-geographic" calling codes.</summary>
|
|
368
|
+
|
|
369
|
+
######
|
|
353
370
|
|
|
354
371
|
* `+800` — [Universal International Toll Free Number](https://en.wikipedia.org/wiki/Toll-free_telephone_number)
|
|
355
372
|
* `+808` — [Universal International Shared Cost Number](https://en.wikipedia.org/wiki/Shared-cost_service)
|
|
@@ -359,8 +376,7 @@ The following "calling codes" don't belong to any given country or territory and
|
|
|
359
376
|
* `+882` and `+883` — [International Networks](https://en.wikipedia.org/wiki/International_Networks_(country_code))
|
|
360
377
|
* `+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)
|
|
361
378
|
* `+979` — [International Premium Rate Service](https://en.wikipedia.org/wiki/International_Premium_Rate_Service)
|
|
362
|
-
|
|
363
|
-
Such phone numbers are called "non-geographic", and their `country` is `undefined`.
|
|
379
|
+
</details>
|
|
364
380
|
|
|
365
381
|
### Country Code
|
|
366
382
|
|
|
@@ -370,7 +386,7 @@ However, this library uses the term "country code" rather broadly and it include
|
|
|
370
386
|
|
|
371
387
|
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".
|
|
372
388
|
|
|
373
|
-
A developer could manually transform any unofficial "country code" returned from this library to a most-suitable official one.
|
|
389
|
+
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.
|
|
374
390
|
|
|
375
391
|
To get a list of all supported "country codes", both official and unofficial, use [`getCountries()`](#getcountries-string) function.
|
|
376
392
|
|
|
@@ -378,7 +394,17 @@ To check if a certain two-letter "country code" is supported by this library, us
|
|
|
378
394
|
|
|
379
395
|
### Country Code (additions)
|
|
380
396
|
|
|
381
|
-
Here's a list of the unofficial "country codes"
|
|
397
|
+
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).
|
|
398
|
+
|
|
399
|
+
* `AC` — [Ascension Island](https://en.wikipedia.org/wiki/Ascension_Island)
|
|
400
|
+
* `TA` — [Tristan da Cunha](https://en.wikipedia.org/wiki/Tristan_da_Cunha)
|
|
401
|
+
* `SH` — [Saint Helena](https://en.wikipedia.org/wiki/Saint_Helena)
|
|
402
|
+
* `XK` — [Kosovo](https://en.wikipedia.org/wiki/Kosovo)
|
|
403
|
+
|
|
404
|
+
<details>
|
|
405
|
+
<summary>Here're some more details on each one of them for those who're interested.</summary>
|
|
406
|
+
|
|
407
|
+
######
|
|
382
408
|
|
|
383
409
|
* `AC` — [Ascension Island](https://en.wikipedia.org/wiki/Ascension_Island)
|
|
384
410
|
* (history) The region of Ascension Island is officially part of [Saint Helena, Ascension and Tristan da Cunha
|
|
@@ -405,25 +431,42 @@ Here's a list of the unofficial "country codes" — these codes are used for cer
|
|
|
405
431
|
* An unofficial code `XK` is used for Kosovo region in Google's `libphonenumber`.
|
|
406
432
|
* `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.
|
|
407
433
|
* 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`.
|
|
434
|
+
</details>
|
|
435
|
+
|
|
436
|
+
######
|
|
437
|
+
|
|
438
|
+
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.
|
|
439
|
+
|
|
440
|
+
* [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`)
|
|
441
|
+
* [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`)
|
|
442
|
+
* [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`)
|
|
443
|
+
|
|
444
|
+
<details>
|
|
445
|
+
<summary>Here're some more details on each one of them for those who're interested.</summary>
|
|
408
446
|
|
|
409
|
-
|
|
410
|
-
|
|
447
|
+
######
|
|
448
|
+
|
|
449
|
+
These territories are currently not implemented in this library but they could potentially be implemented in some future:
|
|
450
|
+
* [Abkhazia](https://en.wikipedia.org/wiki/Abkhazia)
|
|
411
451
|
* (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.
|
|
412
452
|
* 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`.
|
|
413
453
|
* 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`.
|
|
414
454
|
* `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.
|
|
415
455
|
* 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`.
|
|
416
|
-
|
|
456
|
+
|
|
457
|
+
* [South Ossetia](https://en.wikipedia.org/wiki/South_Ossetia)
|
|
417
458
|
* (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.
|
|
418
459
|
* 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`.
|
|
419
460
|
* 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`.
|
|
420
461
|
* `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.
|
|
421
462
|
* 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).
|
|
422
|
-
|
|
463
|
+
|
|
464
|
+
* [Northern Cyprus](https://en.wikipedia.org/wiki/Northern_Cyprus)
|
|
423
465
|
* (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.
|
|
424
466
|
* 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`.
|
|
425
467
|
* 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`.
|
|
426
468
|
* 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).
|
|
469
|
+
</details>
|
|
427
470
|
|
|
428
471
|
## API
|
|
429
472
|
|
|
@@ -1507,7 +1550,7 @@ isValidNumber({ phone: '2133734253', country: 'US' }) === true
|
|
|
1507
1550
|
<details>
|
|
1508
1551
|
<summary>The difference between using <code>parseNumber()</code> and <code>isValidNumber()</code></summary>
|
|
1509
1552
|
|
|
1510
|
-
The difference between using `parseNumber()` and `isValidNumber()` for phone number validation is that `isValidNumber()` also checks the precise regular expressions of possible phone numbers for a country. For example, for Germany `parseNumber('123456', 'DE')` would return `{ country: 'DE', phone: '123456' }` because this phone number matches the general phone number rules for Germany (basic length check, etc). But
|
|
1553
|
+
The difference between using `parseNumber()` and `isValidNumber()` for phone number validation is that `isValidNumber()` also checks the precise regular expressions of possible phone numbers for a country. For example, for Germany, `parseNumber('123456', 'DE')` would return `{ country: 'DE', phone: '123456' }` because this phone number matches the general phone number rules for Germany (basic length check, etc). But if the metadata also includes the regular expressions for phone number types (which is the case when using `/max` metadata) then `isValidNumber()` is gonna use those precise regular expressions for extensive validation, and `isValid('123456', 'DE')` will return `false` because the phone number `123456` doesn't actually exist in Germany.
|
|
1511
1554
|
|
|
1512
1555
|
This is how it is implemented in the original Google's [`libphonenumber`](https://static.javadoc.io/com.googlecode.libphonenumber/libphonenumber/8.9.1/com/google/i18n/phonenumbers/PhoneNumberUtil.html#parse-java.lang.CharSequence-java.lang.String-): `parseNumber()` parses phone numbers and loosely validates them while `isValidNumber()` validates phone numbers precisely (provided the precise regular expressions are included in metadata).
|
|
1513
1556
|
|
|
@@ -1677,7 +1720,7 @@ metadata.numberingPlan.IDDPrefix() === '011'
|
|
|
1677
1720
|
metadata.numberingPlan.defaultIDDPrefix() === undefined
|
|
1678
1721
|
```
|
|
1679
1722
|
|
|
1680
|
-
|
|
1723
|
+
Example with metadata argument:
|
|
1681
1724
|
|
|
1682
1725
|
```js
|
|
1683
1726
|
import { Metadata } from 'libphonenumber-js/core'
|
|
@@ -1712,10 +1755,25 @@ Alternatively, a developer may wish to update metadata urgently, without waiting
|
|
|
1712
1755
|
|
|
1713
1756
|
This library comes prepackaged with [three types of metadata](#min-vs-max-vs-mobile-vs-core).
|
|
1714
1757
|
|
|
1715
|
-
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,
|
|
1758
|
+
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, then they could create a "custom" slice of metadata using [`libphonenumber-metadata-generator`](npmjs.com/libphonenumber-metadata-generator) and then pass it as the last argument to the functions imported from the `/core` subpackage.
|
|
1716
1759
|
|
|
1717
|
-
|
|
1760
|
+
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.
|
|
1718
1761
|
|
|
1762
|
+
```js
|
|
1763
|
+
import parsePhoneNumber, { AsYouType } from 'libphonenumber-js/core'
|
|
1764
|
+
|
|
1765
|
+
import metadata from './metadata.RU.json'
|
|
1766
|
+
|
|
1767
|
+
const phoneNumber = parsePhoneNumber(' 8 (800) 555-35-35 ', 'RU', metadata)
|
|
1768
|
+
if (phoneNumber) {
|
|
1769
|
+
phoneNumber.country === 'RU'
|
|
1770
|
+
phoneNumber.number === '+78005553535'
|
|
1771
|
+
}
|
|
1772
|
+
|
|
1773
|
+
new AsYouType('RU', metadata).input('88005553535') === '8 (800) 555-35-35'
|
|
1774
|
+
```
|
|
1775
|
+
|
|
1776
|
+
<!--
|
|
1719
1777
|
<details>
|
|
1720
1778
|
<summary>How to use the generated <code>metadata.custom.json</code> file with the "core" functions.</summary>
|
|
1721
1779
|
|
|
@@ -1786,7 +1844,9 @@ exports.AsYouType.prototype.constructor = exports.AsYouType
|
|
|
1786
1844
|
</details>
|
|
1787
1845
|
|
|
1788
1846
|
####
|
|
1847
|
+
-->
|
|
1789
1848
|
|
|
1849
|
+
<!--
|
|
1790
1850
|
<details>
|
|
1791
1851
|
<summary>Legacy: How to use the generated <code>metadata.custom.json</code> file with the legacy "custom" functions.</summary>
|
|
1792
1852
|
|
|
@@ -1855,7 +1915,7 @@ exports.AsYouType.prototype.constructor = exports.AsYouType
|
|
|
1855
1915
|
|
|
1856
1916
|
######
|
|
1857
1917
|
|
|
1858
|
-
|
|
1918
|
+
-->
|
|
1859
1919
|
|
|
1860
1920
|
<!-- ## To do -->
|
|
1861
1921
|
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export { default as PhoneNumber } from '../../es6-modern/PhoneNumber.js'
|
|
2
|
+
export { default as ParseError } from '../../es6-modern/ParseError.js'
|
|
3
|
+
// `parsePhoneNumber()` named export has been renamed to `parsePhoneNumberWithError()`.
|
|
4
|
+
export { default as parsePhoneNumberWithError, default as parsePhoneNumber } from '../../es6-modern/parsePhoneNumberWithError.js'
|
|
5
|
+
|
|
6
|
+
// `parsePhoneNumberFromString()` named export is now considered legacy:
|
|
7
|
+
// it has been promoted to a default export due to being too verbose.
|
|
8
|
+
export { default as default, default as parsePhoneNumberFromString } from '../../es6-modern/parsePhoneNumber.js'
|
|
9
|
+
|
|
10
|
+
export { default as isValidPhoneNumber } from '../../es6-modern/isValidPhoneNumber.js'
|
|
11
|
+
export { default as isPossiblePhoneNumber } from '../../es6-modern/isPossiblePhoneNumber.js'
|
|
12
|
+
export { default as validatePhoneNumberLength } from '../../es6-modern/validatePhoneNumberLength.js'
|
|
13
|
+
|
|
14
|
+
// Deprecated.
|
|
15
|
+
export { default as findNumbers } from '../../es6-modern/legacy/findNumbers.js'
|
|
16
|
+
export { default as searchNumbers } from '../../es6-modern/legacy/searchNumbers.js'
|
|
17
|
+
|
|
18
|
+
export { default as findPhoneNumbersInText } from '../../es6-modern/findPhoneNumbersInText.js'
|
|
19
|
+
export { default as searchPhoneNumbersInText } from '../../es6-modern/searchPhoneNumbersInText.js'
|
|
20
|
+
export { default as PhoneNumberMatcher } from '../../es6-modern/PhoneNumberMatcher.js'
|
|
21
|
+
|
|
22
|
+
export { default as AsYouType } from '../../es6-modern/AsYouType.js'
|
|
23
|
+
|
|
24
|
+
// Deprecated:
|
|
25
|
+
// `DIGIT_PLACEHOLDER` is the character that `AsYouType` formatter uses in a phone number template.
|
|
26
|
+
// It's basically an "x" character. I guess, there's no point in exporting it as a constant
|
|
27
|
+
// because the established convention is that it's an "x" character so everyone just assumes "x".
|
|
28
|
+
export { DIGIT_PLACEHOLDER } from '../../es6-modern/AsYouTypeFormatter.js'
|
|
29
|
+
|
|
30
|
+
export { default as getCountries } from '../../es6-modern/getCountries.js'
|
|
31
|
+
export { default as Metadata, isSupportedCountry, getCountryCallingCode, getExtPrefix } from '../../es6-modern/metadata.js'
|
|
32
|
+
|
|
33
|
+
export { default as getExampleNumber } from '../../es6-modern/getExampleNumber.js'
|
|
34
|
+
|
|
35
|
+
export { default as formatIncompletePhoneNumber } from '../../es6-modern/formatIncompletePhoneNumber.js'
|
|
36
|
+
export { default as parseIncompletePhoneNumber, parsePhoneNumberCharacter } from '../../es6-modern/parseIncompletePhoneNumber.js'
|
|
37
|
+
export { default as parseDigits } from '../../es6-modern/helpers/parseDigits.js'
|
|
38
|
+
|
|
39
|
+
export { parseRFC3966, formatRFC3966 } from '../../es6-modern/helpers/RFC3966.js'
|
package/core/index.cjs
CHANGED
|
@@ -46,6 +46,11 @@ exports.formatIncompletePhoneNumber = require('../build/formatIncompletePhoneNum
|
|
|
46
46
|
exports.parseIncompletePhoneNumber = require('../build/parseIncompletePhoneNumber.js').default
|
|
47
47
|
exports.parsePhoneNumberCharacter = require('../build/parseIncompletePhoneNumber.js').parsePhoneNumberCharacter
|
|
48
48
|
exports.parseDigits = require('../build/helpers/parseDigits.js').default
|
|
49
|
+
|
|
50
|
+
// Deprecated:
|
|
51
|
+
// `DIGIT_PLACEHOLDER` is the character that `AsYouType` formatter uses in a phone number template.
|
|
52
|
+
// It's basically an "x" character. I guess, there's no point in exporting it as a constant
|
|
53
|
+
// because the established convention is that it's an "x" character so everyone just assumes "x".
|
|
49
54
|
exports.DIGIT_PLACEHOLDER = require('../build/AsYouTypeFormatter.js').DIGIT_PLACEHOLDER
|
|
50
55
|
|
|
51
56
|
exports.parseRFC3966 = require('../build/helpers/RFC3966.js').parseRFC3966
|
package/core/index.cjs.js
CHANGED
|
@@ -51,6 +51,11 @@ exports.formatIncompletePhoneNumber = require('../build/formatIncompletePhoneNum
|
|
|
51
51
|
exports.parseIncompletePhoneNumber = require('../build/parseIncompletePhoneNumber.js').default
|
|
52
52
|
exports.parsePhoneNumberCharacter = require('../build/parseIncompletePhoneNumber.js').parsePhoneNumberCharacter
|
|
53
53
|
exports.parseDigits = require('../build/helpers/parseDigits.js').default
|
|
54
|
+
|
|
55
|
+
// Deprecated:
|
|
56
|
+
// `DIGIT_PLACEHOLDER` is the character that `AsYouType` formatter uses in a phone number template.
|
|
57
|
+
// It's basically an "x" character. I guess, there's no point in exporting it as a constant
|
|
58
|
+
// because the established convention is that it's an "x" character so everyone just assumes "x".
|
|
54
59
|
exports.DIGIT_PLACEHOLDER = require('../build/AsYouTypeFormatter.js').DIGIT_PLACEHOLDER
|
|
55
60
|
|
|
56
61
|
exports.parseRFC3966 = require('../build/helpers/RFC3966.js').parseRFC3966
|
package/core/index.d.cts
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
Extension,
|
|
10
10
|
ParseError,
|
|
11
11
|
NumberFoundLegacy,
|
|
12
|
+
PhoneNumberType,
|
|
12
13
|
NumberType,
|
|
13
14
|
NumberFormat,
|
|
14
15
|
NumberingPlan,
|
|
@@ -30,6 +31,7 @@ export {
|
|
|
30
31
|
Extension,
|
|
31
32
|
ParseError,
|
|
32
33
|
NumberFoundLegacy,
|
|
34
|
+
PhoneNumberType,
|
|
33
35
|
NumberType,
|
|
34
36
|
NumberFormat,
|
|
35
37
|
NumberingPlan,
|
|
@@ -48,7 +50,7 @@ export class PhoneNumber {
|
|
|
48
50
|
getPossibleCountries(): CountryCode[];
|
|
49
51
|
isPossible(): boolean;
|
|
50
52
|
isValid(): boolean;
|
|
51
|
-
getType():
|
|
53
|
+
getType(): PhoneNumberType | undefined;
|
|
52
54
|
format(format: NumberFormat, options?: FormatNumberOptions): string;
|
|
53
55
|
formatNational(options?: FormatNumberOptionsForNationalOrInternational): string;
|
|
54
56
|
formatInternational(options?: FormatNumberOptionsForNationalOrInternational): string;
|
package/core/index.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
Extension,
|
|
10
10
|
ParseError,
|
|
11
11
|
NumberFoundLegacy,
|
|
12
|
+
PhoneNumberType,
|
|
12
13
|
NumberType,
|
|
13
14
|
NumberFormat,
|
|
14
15
|
NumberingPlan,
|
|
@@ -30,6 +31,7 @@ export {
|
|
|
30
31
|
Extension,
|
|
31
32
|
ParseError,
|
|
32
33
|
NumberFoundLegacy,
|
|
34
|
+
PhoneNumberType,
|
|
33
35
|
NumberType,
|
|
34
36
|
NumberFormat,
|
|
35
37
|
NumberingPlan,
|
|
@@ -48,7 +50,7 @@ export class PhoneNumber {
|
|
|
48
50
|
getPossibleCountries(): CountryCode[];
|
|
49
51
|
isPossible(): boolean;
|
|
50
52
|
isValid(): boolean;
|
|
51
|
-
getType():
|
|
53
|
+
getType(): PhoneNumberType | undefined;
|
|
52
54
|
format(format: NumberFormat, options?: FormatNumberOptions): string;
|
|
53
55
|
formatNational(options?: FormatNumberOptionsForNationalOrInternational): string;
|
|
54
56
|
formatInternational(options?: FormatNumberOptionsForNationalOrInternational): string;
|
package/core/index.js
CHANGED
|
@@ -20,6 +20,11 @@ export { default as searchPhoneNumbersInText } from '../es6/searchPhoneNumbersIn
|
|
|
20
20
|
export { default as PhoneNumberMatcher } from '../es6/PhoneNumberMatcher.js'
|
|
21
21
|
|
|
22
22
|
export { default as AsYouType } from '../es6/AsYouType.js'
|
|
23
|
+
|
|
24
|
+
// Deprecated:
|
|
25
|
+
// `DIGIT_PLACEHOLDER` is the character that `AsYouType` formatter uses in a phone number template.
|
|
26
|
+
// It's basically an "x" character. I guess, there's no point in exporting it as a constant
|
|
27
|
+
// because the established convention is that it's an "x" character so everyone just assumes "x".
|
|
23
28
|
export { DIGIT_PLACEHOLDER } from '../es6/AsYouTypeFormatter.js'
|
|
24
29
|
|
|
25
30
|
export { default as getCountries } from '../es6/getCountries.js'
|
package/custom.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
Extension,
|
|
14
14
|
ParseError,
|
|
15
15
|
NumberFoundLegacy,
|
|
16
|
+
PhoneNumberType,
|
|
16
17
|
NumberType,
|
|
17
18
|
NumberFormat,
|
|
18
19
|
NumberingPlan
|
|
@@ -40,6 +41,7 @@ export {
|
|
|
40
41
|
ParseError,
|
|
41
42
|
NumberFoundLegacy,
|
|
42
43
|
NumberFormat,
|
|
44
|
+
PhoneNumberType,
|
|
43
45
|
NumberType,
|
|
44
46
|
NumberingPlan
|
|
45
47
|
};
|
|
@@ -56,7 +58,7 @@ export class PhoneNumber {
|
|
|
56
58
|
getPossibleCountries(): CountryCode[];
|
|
57
59
|
isPossible(): boolean;
|
|
58
60
|
isValid(): boolean;
|
|
59
|
-
getType():
|
|
61
|
+
getType(): PhoneNumberType | undefined;
|
|
60
62
|
format(format: NumberFormat, options?: FormatNumberOptions): string;
|
|
61
63
|
formatNational(options?: FormatNumberOptionsForNationalOrInternational): string;
|
|
62
64
|
formatInternational(options?: FormatNumberOptionsForNationalOrInternational): string;
|
|
@@ -160,9 +162,9 @@ export function formatNumber(phone: string, country: CountryCode, format: Number
|
|
|
160
162
|
export function formatNumber(phone: string, country: CountryCode, format: NumberFormat, options: FormatNumberOptions, metadata: MetadataJson): string;
|
|
161
163
|
|
|
162
164
|
// Deprecated
|
|
163
|
-
export function getNumberType(parsedNumber: ParsedNumber, metadata: MetadataJson):
|
|
164
|
-
export function getNumberType(phone: string, metadata: MetadataJson):
|
|
165
|
-
export function getNumberType(phone: string, country: CountryCode, metadata: MetadataJson):
|
|
165
|
+
export function getNumberType(parsedNumber: ParsedNumber, metadata: MetadataJson): PhoneNumberType | undefined;
|
|
166
|
+
export function getNumberType(phone: string, metadata: MetadataJson): PhoneNumberType | undefined;
|
|
167
|
+
export function getNumberType(phone: string, country: CountryCode, metadata: MetadataJson): PhoneNumberType | undefined;
|
|
166
168
|
|
|
167
169
|
// Deprecated
|
|
168
170
|
export function isPossibleNumber(parsedNumber: ParsedNumber, metadata: MetadataJson): boolean;
|