libphonenumber-js 1.12.42 → 1.13.0
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 -94
- package/README.md +36 -6
- 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/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/max/metadata/metadata.max.json +1 -1
- package/max/metadata/metadata.max.json.js +1 -1
- package/metadata.full.json +1 -1
- package/metadata.full.json.js +1 -1
- package/metadata.max.json +1 -1
- package/metadata.max.json.js +1 -1
- package/metadata.min.json +1 -1
- package/metadata.min.json.js +1 -1
- package/metadata.mobile.json +1 -1
- package/metadata.mobile.json.js +1 -1
- 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/min/metadata/metadata.min.json +1 -1
- package/min/metadata/metadata.min.json.js +1 -1
- 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/mobile/metadata/metadata.mobile.json +1 -1
- package/mobile/metadata/metadata.mobile.json.js +1 -1
- package/package.json +28 -9
- package/types.d.cts +54 -21
- package/types.d.ts +54 -21
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import extractNationalNumberFromPossiblyIncompleteNumber from './extractNationalNumberFromPossiblyIncompleteNumber.js';
|
|
2
|
+
import matchesEntirely from './matchesEntirely.js';
|
|
3
|
+
import checkNumberLength from './checkNumberLength.js';
|
|
4
|
+
import getCountryByCallingCode from './getCountryByCallingCode.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Strips national prefix and carrier code from a complete phone number.
|
|
8
|
+
* The difference from the non-"FromCompleteNumber" function is that
|
|
9
|
+
* it won't extract national prefix if the resultant number is too short
|
|
10
|
+
* to be a complete number for the selected phone numbering plan.
|
|
11
|
+
* @param {string} number — Complete phone number digits.
|
|
12
|
+
* @param {string?} country — Country, if known.
|
|
13
|
+
* @param {Metadata} metadata — Metadata with a phone numbering plan selected.
|
|
14
|
+
* @return {object} `{ nationalNumber: string, carrierCode: string? }`.
|
|
15
|
+
*/
|
|
16
|
+
export default function extractNationalNumber(number, country, metadata) {
|
|
17
|
+
// Parsing national prefixes and carrier codes
|
|
18
|
+
// is only required for local phone numbers
|
|
19
|
+
// but some people don't understand that
|
|
20
|
+
// and sometimes write international phone numbers
|
|
21
|
+
// with national prefixes (or maybe even carrier codes).
|
|
22
|
+
// http://ucken.blogspot.ru/2016/03/trunk-prefixes-in-skype4b.html
|
|
23
|
+
// Google's original library forgives such mistakes
|
|
24
|
+
// and so does this library, because it has been requested:
|
|
25
|
+
// https://github.com/catamphetamine/libphonenumber-js/issues/127
|
|
26
|
+
const {
|
|
27
|
+
carrierCode,
|
|
28
|
+
nationalNumber
|
|
29
|
+
} = extractNationalNumberFromPossiblyIncompleteNumber(number, metadata);
|
|
30
|
+
if (nationalNumber !== number) {
|
|
31
|
+
if (!shouldHaveExtractedNationalPrefix(number, nationalNumber, metadata)) {
|
|
32
|
+
// Don't strip the national prefix.
|
|
33
|
+
return {
|
|
34
|
+
nationalNumber: number
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
// Check the national (significant) number length after extracting national prefix and carrier code.
|
|
38
|
+
// Legacy generated metadata (before `1.0.18`) didn't support the "possible lengths" feature.
|
|
39
|
+
if (metadata.numberingPlan.possibleLengths()) {
|
|
40
|
+
// If an exact `country` is not specified, attempt to detect it from the assumed national number.
|
|
41
|
+
if (!country) {
|
|
42
|
+
country = getCountryByCallingCode(metadata.numberingPlan.callingCode(), {
|
|
43
|
+
nationalNumber,
|
|
44
|
+
metadata
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// The number remaining after stripping the national prefix and carrier code
|
|
49
|
+
// should be long enough to have a possible length for the country.
|
|
50
|
+
// Otherwise, don't strip the national prefix and carrier code,
|
|
51
|
+
// since the original number could be a valid number.
|
|
52
|
+
// This check has been copy-pasted "as is" from Google's original library:
|
|
53
|
+
// https://github.com/google/libphonenumber/blob/876268eb1ad6cdc1b7b5bef17fc5e43052702d57/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L3236-L3250
|
|
54
|
+
// It doesn't check for the "possibility" of the original `number`.
|
|
55
|
+
// I guess it's fine not checking that one. It works as is anyway.
|
|
56
|
+
if (!isPossibleIncompleteNationalNumber(nationalNumber, country, metadata)) {
|
|
57
|
+
// Don't strip the national prefix.
|
|
58
|
+
return {
|
|
59
|
+
nationalNumber: number
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
nationalNumber,
|
|
66
|
+
carrierCode
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// In some countries, the same digit could be a national prefix
|
|
71
|
+
// or a leading digit of a valid phone number.
|
|
72
|
+
// For example, in Russia, national prefix is `8`,
|
|
73
|
+
// and also `800 555 35 35` is a valid number
|
|
74
|
+
// in which `8` is not a national prefix, but the first digit
|
|
75
|
+
// of a national (significant) number.
|
|
76
|
+
// Same's with Belarus:
|
|
77
|
+
// `82004910060` is a valid national (significant) number,
|
|
78
|
+
// but `2004910060` is not.
|
|
79
|
+
// To support such cases (to prevent the code from always stripping
|
|
80
|
+
// national prefix), a condition is imposed: a national prefix
|
|
81
|
+
// is not extracted when the original number is "viable" and the
|
|
82
|
+
// resultant number is not, a "viable" national number being the one
|
|
83
|
+
// that matches `national_number_pattern`.
|
|
84
|
+
function shouldHaveExtractedNationalPrefix(nationalNumberBefore, nationalNumberAfter, metadata) {
|
|
85
|
+
// The equivalent in Google's code is:
|
|
86
|
+
// https://github.com/google/libphonenumber/blob/e326fa1fc4283bb05eb35cb3c15c18f98a31af33/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L2969-L3004
|
|
87
|
+
if (matchesEntirely(nationalNumberBefore, metadata.nationalNumberPattern()) && !matchesEntirely(nationalNumberAfter, metadata.nationalNumberPattern())) {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
// This "is possible" national number (length) check has been commented out
|
|
91
|
+
// because it's superceded by the (effectively) same check done in the
|
|
92
|
+
// `extractNationalNumber()` function after it calls `shouldHaveExtractedNationalPrefix()`.
|
|
93
|
+
// In other words, why run the same check twice if it could only be run once.
|
|
94
|
+
// // Check the national (significant) number length after extracting national prefix and carrier code.
|
|
95
|
+
// // Fixes a minor "weird behavior" bug: https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/57
|
|
96
|
+
// // (Legacy generated metadata (before `1.0.18`) didn't support the "possible lengths" feature).
|
|
97
|
+
// if (metadata.possibleLengths()) {
|
|
98
|
+
// if (isPossibleIncompleteNationalNumber(nationalNumberBefore, metadata) &&
|
|
99
|
+
// !isPossibleIncompleteNationalNumber(nationalNumberAfter, metadata)) {
|
|
100
|
+
// return false
|
|
101
|
+
// }
|
|
102
|
+
// }
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
function isPossibleIncompleteNationalNumber(nationalNumber, country, metadata) {
|
|
106
|
+
switch (checkNumberLength(nationalNumber, country, metadata)) {
|
|
107
|
+
case 'TOO_SHORT':
|
|
108
|
+
case 'INVALID_LENGTH':
|
|
109
|
+
// This library ignores "local-only" phone numbers (for simplicity).
|
|
110
|
+
// See the readme for more info on what are "local-only" phone numbers.
|
|
111
|
+
// case 'IS_POSSIBLE_LOCAL_ONLY':
|
|
112
|
+
return false;
|
|
113
|
+
default:
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=extractNationalNumber.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extractNationalNumber.js","names":["extractNationalNumberFromPossiblyIncompleteNumber","matchesEntirely","checkNumberLength","getCountryByCallingCode","extractNationalNumber","number","country","metadata","carrierCode","nationalNumber","shouldHaveExtractedNationalPrefix","numberingPlan","possibleLengths","callingCode","isPossibleIncompleteNationalNumber","nationalNumberBefore","nationalNumberAfter","nationalNumberPattern"],"sources":["../../source/helpers/extractNationalNumber.js"],"sourcesContent":["import extractNationalNumberFromPossiblyIncompleteNumber from './extractNationalNumberFromPossiblyIncompleteNumber.js'\r\nimport matchesEntirely from './matchesEntirely.js'\r\nimport checkNumberLength from './checkNumberLength.js'\r\nimport getCountryByCallingCode from './getCountryByCallingCode.js'\r\n\r\n/**\r\n * Strips national prefix and carrier code from a complete phone number.\r\n * The difference from the non-\"FromCompleteNumber\" function is that\r\n * it won't extract national prefix if the resultant number is too short\r\n * to be a complete number for the selected phone numbering plan.\r\n * @param {string} number — Complete phone number digits.\r\n * @param {string?} country — Country, if known.\r\n * @param {Metadata} metadata — Metadata with a phone numbering plan selected.\r\n * @return {object} `{ nationalNumber: string, carrierCode: string? }`.\r\n */\r\nexport default function extractNationalNumber(number, country, metadata) {\r\n\t// Parsing national prefixes and carrier codes\r\n\t// is only required for local phone numbers\r\n\t// but some people don't understand that\r\n\t// and sometimes write international phone numbers\r\n\t// with national prefixes (or maybe even carrier codes).\r\n\t// http://ucken.blogspot.ru/2016/03/trunk-prefixes-in-skype4b.html\r\n\t// Google's original library forgives such mistakes\r\n\t// and so does this library, because it has been requested:\r\n\t// https://github.com/catamphetamine/libphonenumber-js/issues/127\r\n\tconst {\r\n\t\tcarrierCode,\r\n\t\tnationalNumber\r\n\t} = extractNationalNumberFromPossiblyIncompleteNumber(\r\n\t\tnumber,\r\n\t\tmetadata\r\n\t)\r\n\r\n\tif (nationalNumber !== number) {\r\n\t\tif (!shouldHaveExtractedNationalPrefix(number, nationalNumber, metadata)) {\r\n\t\t\t// Don't strip the national prefix.\r\n\t\t\treturn { nationalNumber: number }\r\n\t\t}\r\n\t\t// Check the national (significant) number length after extracting national prefix and carrier code.\r\n\t\t// Legacy generated metadata (before `1.0.18`) didn't support the \"possible lengths\" feature.\r\n\t\tif (metadata.numberingPlan.possibleLengths()) {\r\n\t\t\t// If an exact `country` is not specified, attempt to detect it from the assumed national number.\r\n\t\t\tif (!country) {\r\n\t\t\t\tcountry = getCountryByCallingCode(metadata.numberingPlan.callingCode(), {\r\n\t\t\t\t\tnationalNumber,\r\n\t\t\t\t\tmetadata\r\n\t\t\t\t})\r\n\t\t\t}\r\n\r\n\t\t\t// The number remaining after stripping the national prefix and carrier code\r\n\t\t\t// should be long enough to have a possible length for the country.\r\n\t\t\t// Otherwise, don't strip the national prefix and carrier code,\r\n\t\t\t// since the original number could be a valid number.\r\n\t\t\t// This check has been copy-pasted \"as is\" from Google's original library:\r\n\t\t\t// https://github.com/google/libphonenumber/blob/876268eb1ad6cdc1b7b5bef17fc5e43052702d57/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L3236-L3250\r\n\t\t\t// It doesn't check for the \"possibility\" of the original `number`.\r\n\t\t\t// I guess it's fine not checking that one. It works as is anyway.\r\n\t\t\tif (!isPossibleIncompleteNationalNumber(nationalNumber, country, metadata)) {\r\n\t\t\t\t// Don't strip the national prefix.\r\n\t\t\t\treturn { nationalNumber: number }\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\treturn { nationalNumber, carrierCode }\r\n}\r\n\r\n// In some countries, the same digit could be a national prefix\r\n// or a leading digit of a valid phone number.\r\n// For example, in Russia, national prefix is `8`,\r\n// and also `800 555 35 35` is a valid number\r\n// in which `8` is not a national prefix, but the first digit\r\n// of a national (significant) number.\r\n// Same's with Belarus:\r\n// `82004910060` is a valid national (significant) number,\r\n// but `2004910060` is not.\r\n// To support such cases (to prevent the code from always stripping\r\n// national prefix), a condition is imposed: a national prefix\r\n// is not extracted when the original number is \"viable\" and the\r\n// resultant number is not, a \"viable\" national number being the one\r\n// that matches `national_number_pattern`.\r\nfunction shouldHaveExtractedNationalPrefix(nationalNumberBefore, nationalNumberAfter, metadata) {\r\n\t// The equivalent in Google's code is:\r\n\t// https://github.com/google/libphonenumber/blob/e326fa1fc4283bb05eb35cb3c15c18f98a31af33/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L2969-L3004\r\n\tif (matchesEntirely(nationalNumberBefore, metadata.nationalNumberPattern()) &&\r\n\t\t!matchesEntirely(nationalNumberAfter, metadata.nationalNumberPattern())) {\r\n\t\treturn false\r\n\t}\r\n\t// This \"is possible\" national number (length) check has been commented out\r\n\t// because it's superceded by the (effectively) same check done in the\r\n\t// `extractNationalNumber()` function after it calls `shouldHaveExtractedNationalPrefix()`.\r\n\t// In other words, why run the same check twice if it could only be run once.\r\n\t// // Check the national (significant) number length after extracting national prefix and carrier code.\r\n\t// // Fixes a minor \"weird behavior\" bug: https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/57\r\n\t// // (Legacy generated metadata (before `1.0.18`) didn't support the \"possible lengths\" feature).\r\n\t// if (metadata.possibleLengths()) {\r\n\t// \tif (isPossibleIncompleteNationalNumber(nationalNumberBefore, metadata) &&\r\n\t// \t\t!isPossibleIncompleteNationalNumber(nationalNumberAfter, metadata)) {\r\n\t// \t\treturn false\r\n\t// \t}\r\n\t// }\r\n\treturn true\r\n}\r\n\r\nfunction isPossibleIncompleteNationalNumber(nationalNumber, country, metadata) {\r\n\tswitch (checkNumberLength(nationalNumber, country, metadata)) {\r\n\t\tcase 'TOO_SHORT':\r\n\t\tcase 'INVALID_LENGTH':\r\n\t\t// This library ignores \"local-only\" phone numbers (for simplicity).\r\n\t\t// See the readme for more info on what are \"local-only\" phone numbers.\r\n\t\t// case 'IS_POSSIBLE_LOCAL_ONLY':\r\n\t\t\treturn false\r\n\t\tdefault:\r\n\t\t\treturn true\r\n\t}\r\n}"],"mappings":"AAAA,OAAOA,iDAAiD,MAAM,wDAAwD;AACtH,OAAOC,eAAe,MAAM,sBAAsB;AAClD,OAAOC,iBAAiB,MAAM,wBAAwB;AACtD,OAAOC,uBAAuB,MAAM,8BAA8B;;AAElE;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,qBAAqBA,CAACC,MAAM,EAAEC,OAAO,EAAEC,QAAQ,EAAE;EACxE;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,MAAM;IACLC,WAAW;IACXC;EACD,CAAC,GAAGT,iDAAiD,CACpDK,MAAM,EACNE,QACD,CAAC;EAED,IAAIE,cAAc,KAAKJ,MAAM,EAAE;IAC9B,IAAI,CAACK,iCAAiC,CAACL,MAAM,EAAEI,cAAc,EAAEF,QAAQ,CAAC,EAAE;MACzE;MACA,OAAO;QAAEE,cAAc,EAAEJ;MAAO,CAAC;IAClC;IACA;IACA;IACA,IAAIE,QAAQ,CAACI,aAAa,CAACC,eAAe,CAAC,CAAC,EAAE;MAC7C;MACA,IAAI,CAACN,OAAO,EAAE;QACbA,OAAO,GAAGH,uBAAuB,CAACI,QAAQ,CAACI,aAAa,CAACE,WAAW,CAAC,CAAC,EAAE;UACvEJ,cAAc;UACdF;QACD,CAAC,CAAC;MACH;;MAEA;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA,IAAI,CAACO,kCAAkC,CAACL,cAAc,EAAEH,OAAO,EAAEC,QAAQ,CAAC,EAAE;QAC3E;QACA,OAAO;UAAEE,cAAc,EAAEJ;QAAO,CAAC;MAClC;IACD;EACD;EAEA,OAAO;IAAEI,cAAc;IAAED;EAAY,CAAC;AACvC;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASE,iCAAiCA,CAACK,oBAAoB,EAAEC,mBAAmB,EAAET,QAAQ,EAAE;EAC/F;EACA;EACA,IAAIN,eAAe,CAACc,oBAAoB,EAAER,QAAQ,CAACU,qBAAqB,CAAC,CAAC,CAAC,IAC1E,CAAChB,eAAe,CAACe,mBAAmB,EAAET,QAAQ,CAACU,qBAAqB,CAAC,CAAC,CAAC,EAAE;IACzE,OAAO,KAAK;EACb;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,OAAO,IAAI;AACZ;AAEA,SAASH,kCAAkCA,CAACL,cAAc,EAAEH,OAAO,EAAEC,QAAQ,EAAE;EAC9E,QAAQL,iBAAiB,CAACO,cAAc,EAAEH,OAAO,EAAEC,QAAQ,CAAC;IAC3D,KAAK,WAAW;IAChB,KAAK,gBAAgB;MACrB;MACA;MACA;MACC,OAAO,KAAK;IACb;MACC,OAAO,IAAI;EACb;AACD","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extractNationalNumber.test.js","names":["describe","it","expect","extractNationalNumber","Metadata","oldMetadata","type","_oldMetadata","selectNumberingPlan","undefined","to","deep","equal","nationalNumber","carrierCode"],"sources":["../../source/helpers/extractNationalNumber.test.js"],"sourcesContent":["import { describe, it } from 'mocha'\r\nimport { expect } from 'chai'\r\n\r\nimport extractNationalNumber from './extractNationalNumber.js'\r\n\r\nimport Metadata from '../metadata.js'\r\nimport oldMetadata from '../../test/metadata/1.0.0/metadata.min.json' with { type: 'json' }\r\n\r\ndescribe('extractNationalNumber', function() {\r\n\tit('should extract a national number when using old metadata (no `country` or `defaultCountry` specified)', function() {\r\n\t\tconst _oldMetadata = new Metadata(oldMetadata)\r\n\t\t_oldMetadata.selectNumberingPlan('RU')\r\n\t\texpect(extractNationalNumber('88005553535', undefined, _oldMetadata)).to.deep.equal({\r\n\t\t\tnationalNumber: '8005553535',\r\n\t\t\tcarrierCode: undefined\r\n\t\t})\r\n\t})\r\n\r\n\tit('should extract a national number when using old metadata (`country` is specified)', function() {\r\n\t\tconst _oldMetadata = new Metadata(oldMetadata)\r\n\t\t_oldMetadata.selectNumberingPlan('RU')\r\n\t\texpect(extractNationalNumber('88005553535', 'RU', _oldMetadata)).to.deep.equal({\r\n\t\t\tnationalNumber: '8005553535',\r\n\t\t\tcarrierCode: undefined\r\n\t\t})\r\n\t})\r\n})"],"mappings":"AAAA,SAASA,QAAQ,EAAEC,EAAE,QAAQ,OAAO;AACpC,SAASC,MAAM,QAAQ,MAAM;AAE7B,OAAOC,qBAAqB,MAAM,4BAA4B;AAE9D,OAAOC,QAAQ,MAAM,gBAAgB;AACrC,OAAOC,WAAW,MAAM,6CAA6C,QAAQC,IAAI,EAAE,MAAM;AAEzFN,QAAQ,CAAC,uBAAuB,EAAE,YAAW;EAC5CC,EAAE,CAAC,uGAAuG,EAAE,YAAW;IACtH,MAAMM,YAAY,GAAG,IAAIH,QAAQ,CAACC,WAAW,CAAC;IAC9CE,YAAY,CAACC,mBAAmB,CAAC,IAAI,CAAC;IACtCN,MAAM,CAACC,qBAAqB,CAAC,aAAa,EAAEM,SAAS,EAAEF,YAAY,CAAC,CAAC,CAACG,EAAE,CAACC,IAAI,CAACC,KAAK,CAAC;MACnFC,cAAc,EAAE,YAAY;MAC5BC,WAAW,EAAEL;IACd,CAAC,CAAC;EACH,CAAC,CAAC;EAEFR,EAAE,CAAC,mFAAmF,EAAE,YAAW;IAClG,MAAMM,YAAY,GAAG,IAAIH,QAAQ,CAACC,WAAW,CAAC;IAC9CE,YAAY,CAACC,mBAAmB,CAAC,IAAI,CAAC;IACtCN,MAAM,CAACC,qBAAqB,CAAC,aAAa,EAAE,IAAI,EAAEI,YAAY,CAAC,CAAC,CAACG,EAAE,CAACC,IAAI,CAACC,KAAK,CAAC;MAC9EC,cAAc,EAAE,YAAY;MAC5BC,WAAW,EAAEL;IACd,CAAC,CAAC;EACH,CAAC,CAAC;AACH,CAAC,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Strips any national prefix (such as 0, 1) present in a
|
|
3
|
+
* (possibly incomplete) number provided.
|
|
4
|
+
* "Carrier codes" are only used in Colombia and Brazil,
|
|
5
|
+
* and only when dialing within those countries from a mobile phone to a fixed line number.
|
|
6
|
+
* Sometimes it won't actually strip national prefix
|
|
7
|
+
* and will instead prepend some digits to the `number`:
|
|
8
|
+
* for example, when number `2345678` is passed with `VI` country selected,
|
|
9
|
+
* it will return `{ number: "3402345678" }`, because `340` area code is prepended.
|
|
10
|
+
* @param {string} number — National number digits.
|
|
11
|
+
* @param {object} metadata — Metadata with country selected.
|
|
12
|
+
* @return {object} `{ nationalNumber: string, nationalPrefix: string? carrierCode: string? }`. Even if a national prefix was extracted, it's not necessarily present in the returned object, so don't rely on its presence in the returned object in order to find out whether a national prefix has been extracted or not.
|
|
13
|
+
*/
|
|
14
|
+
export default function extractNationalNumberFromPossiblyIncompleteNumber(number, metadata) {
|
|
15
|
+
if (number && metadata.numberingPlan.nationalPrefixForParsing()) {
|
|
16
|
+
// See METADATA.md for the description of
|
|
17
|
+
// `national_prefix_for_parsing` and `national_prefix_transform_rule`.
|
|
18
|
+
// Attempt to parse the first digits as a national prefix.
|
|
19
|
+
const prefixPattern = new RegExp('^(?:' + metadata.numberingPlan.nationalPrefixForParsing() + ')');
|
|
20
|
+
const prefixMatch = prefixPattern.exec(number);
|
|
21
|
+
if (prefixMatch) {
|
|
22
|
+
let nationalNumber;
|
|
23
|
+
let carrierCode;
|
|
24
|
+
// https://gitlab.com/catamphetamine/libphonenumber-js/-/blob/master/METADATA.md#national_prefix_for_parsing--national_prefix_transform_rule
|
|
25
|
+
// If a `national_prefix_for_parsing` has any "capturing groups"
|
|
26
|
+
// then it means that the national (significant) number is equal to
|
|
27
|
+
// those "capturing groups" transformed via `national_prefix_transform_rule`,
|
|
28
|
+
// and nothing could be said about the actual national prefix:
|
|
29
|
+
// what is it and was it even there.
|
|
30
|
+
// If a `national_prefix_for_parsing` doesn't have any "capturing groups",
|
|
31
|
+
// then everything it matches is a national prefix.
|
|
32
|
+
// To determine whether `national_prefix_for_parsing` matched any
|
|
33
|
+
// "capturing groups", the value of the result of calling `.exec()`
|
|
34
|
+
// is looked at, and if it has non-undefined values where there're
|
|
35
|
+
// "capturing groups" in the regular expression, then it means
|
|
36
|
+
// that "capturing groups" have been matched.
|
|
37
|
+
// It's not possible to tell whether there'll be any "capturing gropus"
|
|
38
|
+
// before the matching process, because a `national_prefix_for_parsing`
|
|
39
|
+
// could exhibit both behaviors.
|
|
40
|
+
const capturedGroupsCount = prefixMatch.length - 1;
|
|
41
|
+
const hasCapturedGroups = capturedGroupsCount > 0 && prefixMatch[capturedGroupsCount];
|
|
42
|
+
if (metadata.nationalPrefixTransformRule() && hasCapturedGroups) {
|
|
43
|
+
nationalNumber = number.replace(prefixPattern, metadata.nationalPrefixTransformRule());
|
|
44
|
+
// If there's more than one captured group,
|
|
45
|
+
// then carrier code is the second one.
|
|
46
|
+
if (capturedGroupsCount > 1) {
|
|
47
|
+
carrierCode = prefixMatch[1];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
// If there're no "capturing groups",
|
|
51
|
+
// or if there're "capturing groups" but no
|
|
52
|
+
// `national_prefix_transform_rule`,
|
|
53
|
+
// then just strip the national prefix from the number,
|
|
54
|
+
// and possibly a carrier code.
|
|
55
|
+
// Seems like there could be more.
|
|
56
|
+
else {
|
|
57
|
+
// `prefixBeforeNationalNumber` is the whole substring matched by
|
|
58
|
+
// the `national_prefix_for_parsing` regular expression.
|
|
59
|
+
// There seem to be no guarantees that it's just a national prefix.
|
|
60
|
+
// For example, if there's a carrier code, it's gonna be a
|
|
61
|
+
// part of `prefixBeforeNationalNumber` too.
|
|
62
|
+
const prefixBeforeNationalNumber = prefixMatch[0];
|
|
63
|
+
nationalNumber = number.slice(prefixBeforeNationalNumber.length);
|
|
64
|
+
// If there's at least one captured group,
|
|
65
|
+
// then carrier code is the first one.
|
|
66
|
+
if (hasCapturedGroups) {
|
|
67
|
+
carrierCode = prefixMatch[1];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
// Tries to guess whether a national prefix was present in the input.
|
|
71
|
+
// This is not something copy-pasted from Google's library:
|
|
72
|
+
// they don't seem to have an equivalent for that.
|
|
73
|
+
// So this isn't an "officially approved" way of doing something like that.
|
|
74
|
+
// But since there seems no other existing method, this library uses it.
|
|
75
|
+
let nationalPrefix;
|
|
76
|
+
if (hasCapturedGroups) {
|
|
77
|
+
const possiblePositionOfTheFirstCapturedGroup = number.indexOf(prefixMatch[1]);
|
|
78
|
+
const possibleNationalPrefix = number.slice(0, possiblePositionOfTheFirstCapturedGroup);
|
|
79
|
+
// Example: an Argentinian (AR) phone number `0111523456789`.
|
|
80
|
+
// `prefixMatch[0]` is `01115`, and `$1` is `11`,
|
|
81
|
+
// and the rest of the phone number is `23456789`.
|
|
82
|
+
// The national number is transformed via `9$1` to `91123456789`.
|
|
83
|
+
// National prefix `0` is detected being present at the start.
|
|
84
|
+
// if (possibleNationalPrefix.indexOf(metadata.numberingPlan.nationalPrefix()) === 0) {
|
|
85
|
+
if (possibleNationalPrefix === metadata.numberingPlan.nationalPrefix()) {
|
|
86
|
+
nationalPrefix = metadata.numberingPlan.nationalPrefix();
|
|
87
|
+
}
|
|
88
|
+
} else {
|
|
89
|
+
nationalPrefix = prefixMatch[0];
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
nationalNumber,
|
|
93
|
+
nationalPrefix,
|
|
94
|
+
carrierCode
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
nationalNumber: number
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=extractNationalNumberFromPossiblyIncompleteNumber.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extractNationalNumberFromPossiblyIncompleteNumber.js","names":["extractNationalNumberFromPossiblyIncompleteNumber","number","metadata","numberingPlan","nationalPrefixForParsing","prefixPattern","RegExp","prefixMatch","exec","nationalNumber","carrierCode","capturedGroupsCount","length","hasCapturedGroups","nationalPrefixTransformRule","replace","prefixBeforeNationalNumber","slice","nationalPrefix","possiblePositionOfTheFirstCapturedGroup","indexOf","possibleNationalPrefix"],"sources":["../../source/helpers/extractNationalNumberFromPossiblyIncompleteNumber.js"],"sourcesContent":["/**\r\n * Strips any national prefix (such as 0, 1) present in a\r\n * (possibly incomplete) number provided.\r\n * \"Carrier codes\" are only used in Colombia and Brazil,\r\n * and only when dialing within those countries from a mobile phone to a fixed line number.\r\n * Sometimes it won't actually strip national prefix\r\n * and will instead prepend some digits to the `number`:\r\n * for example, when number `2345678` is passed with `VI` country selected,\r\n * it will return `{ number: \"3402345678\" }`, because `340` area code is prepended.\r\n * @param {string} number — National number digits.\r\n * @param {object} metadata — Metadata with country selected.\r\n * @return {object} `{ nationalNumber: string, nationalPrefix: string? carrierCode: string? }`. Even if a national prefix was extracted, it's not necessarily present in the returned object, so don't rely on its presence in the returned object in order to find out whether a national prefix has been extracted or not.\r\n */\r\nexport default function extractNationalNumberFromPossiblyIncompleteNumber(number, metadata) {\r\n\tif (number && metadata.numberingPlan.nationalPrefixForParsing()) {\r\n\t\t// See METADATA.md for the description of\r\n\t\t// `national_prefix_for_parsing` and `national_prefix_transform_rule`.\r\n\t\t// Attempt to parse the first digits as a national prefix.\r\n\t\tconst prefixPattern = new RegExp('^(?:' + metadata.numberingPlan.nationalPrefixForParsing() + ')')\r\n\t\tconst prefixMatch = prefixPattern.exec(number)\r\n\t\tif (prefixMatch) {\r\n\t\t\tlet nationalNumber\r\n\t\t\tlet carrierCode\r\n\t\t\t// https://gitlab.com/catamphetamine/libphonenumber-js/-/blob/master/METADATA.md#national_prefix_for_parsing--national_prefix_transform_rule\r\n\t\t\t// If a `national_prefix_for_parsing` has any \"capturing groups\"\r\n\t\t\t// then it means that the national (significant) number is equal to\r\n\t\t\t// those \"capturing groups\" transformed via `national_prefix_transform_rule`,\r\n\t\t\t// and nothing could be said about the actual national prefix:\r\n\t\t\t// what is it and was it even there.\r\n\t\t\t// If a `national_prefix_for_parsing` doesn't have any \"capturing groups\",\r\n\t\t\t// then everything it matches is a national prefix.\r\n\t\t\t// To determine whether `national_prefix_for_parsing` matched any\r\n\t\t\t// \"capturing groups\", the value of the result of calling `.exec()`\r\n\t\t\t// is looked at, and if it has non-undefined values where there're\r\n\t\t\t// \"capturing groups\" in the regular expression, then it means\r\n\t\t\t// that \"capturing groups\" have been matched.\r\n\t\t\t// It's not possible to tell whether there'll be any \"capturing gropus\"\r\n\t\t\t// before the matching process, because a `national_prefix_for_parsing`\r\n\t\t\t// could exhibit both behaviors.\r\n\t\t\tconst capturedGroupsCount = prefixMatch.length - 1\r\n\t\t\tconst hasCapturedGroups = capturedGroupsCount > 0 && prefixMatch[capturedGroupsCount]\r\n\t\t\tif (metadata.nationalPrefixTransformRule() && hasCapturedGroups) {\r\n\t\t\t\tnationalNumber = number.replace(\r\n\t\t\t\t\tprefixPattern,\r\n\t\t\t\t\tmetadata.nationalPrefixTransformRule()\r\n\t\t\t\t)\r\n\t\t\t\t// If there's more than one captured group,\r\n\t\t\t\t// then carrier code is the second one.\r\n\t\t\t\tif (capturedGroupsCount > 1) {\r\n\t\t\t\t\tcarrierCode = prefixMatch[1]\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t// If there're no \"capturing groups\",\r\n\t\t\t// or if there're \"capturing groups\" but no\r\n\t\t\t// `national_prefix_transform_rule`,\r\n\t\t\t// then just strip the national prefix from the number,\r\n\t\t\t// and possibly a carrier code.\r\n\t\t\t// Seems like there could be more.\r\n\t\t\telse {\r\n\t\t\t\t// `prefixBeforeNationalNumber` is the whole substring matched by\r\n\t\t\t\t// the `national_prefix_for_parsing` regular expression.\r\n\t\t\t\t// There seem to be no guarantees that it's just a national prefix.\r\n\t\t\t\t// For example, if there's a carrier code, it's gonna be a\r\n\t\t\t\t// part of `prefixBeforeNationalNumber` too.\r\n\t\t\t\tconst prefixBeforeNationalNumber = prefixMatch[0]\r\n\t\t\t\tnationalNumber = number.slice(prefixBeforeNationalNumber.length)\r\n\t\t\t\t// If there's at least one captured group,\r\n\t\t\t\t// then carrier code is the first one.\r\n\t\t\t\tif (hasCapturedGroups) {\r\n\t\t\t\t\tcarrierCode = prefixMatch[1]\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t// Tries to guess whether a national prefix was present in the input.\r\n\t\t\t// This is not something copy-pasted from Google's library:\r\n\t\t\t// they don't seem to have an equivalent for that.\r\n\t\t\t// So this isn't an \"officially approved\" way of doing something like that.\r\n\t\t\t// But since there seems no other existing method, this library uses it.\r\n\t\t\tlet nationalPrefix\r\n\t\t\tif (hasCapturedGroups) {\r\n\t\t\t\tconst possiblePositionOfTheFirstCapturedGroup = number.indexOf(prefixMatch[1])\r\n\t\t\t\tconst possibleNationalPrefix = number.slice(0, possiblePositionOfTheFirstCapturedGroup)\r\n\t\t\t\t// Example: an Argentinian (AR) phone number `0111523456789`.\r\n\t\t\t\t// `prefixMatch[0]` is `01115`, and `$1` is `11`,\r\n\t\t\t\t// and the rest of the phone number is `23456789`.\r\n\t\t\t\t// The national number is transformed via `9$1` to `91123456789`.\r\n\t\t\t\t// National prefix `0` is detected being present at the start.\r\n\t\t\t\t// if (possibleNationalPrefix.indexOf(metadata.numberingPlan.nationalPrefix()) === 0) {\r\n\t\t\t\tif (possibleNationalPrefix === metadata.numberingPlan.nationalPrefix()) {\r\n\t\t\t\t\tnationalPrefix = metadata.numberingPlan.nationalPrefix()\r\n\t\t\t\t}\r\n\t\t\t} else {\r\n\t\t\t\tnationalPrefix = prefixMatch[0]\r\n\t\t\t}\r\n\t\t\treturn {\r\n\t\t\t\tnationalNumber,\r\n\t\t\t\tnationalPrefix,\r\n\t\t\t\tcarrierCode\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n return {\r\n \tnationalNumber: number\r\n }\r\n}"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASA,iDAAiDA,CAACC,MAAM,EAAEC,QAAQ,EAAE;EAC3F,IAAID,MAAM,IAAIC,QAAQ,CAACC,aAAa,CAACC,wBAAwB,CAAC,CAAC,EAAE;IAChE;IACA;IACA;IACA,MAAMC,aAAa,GAAG,IAAIC,MAAM,CAAC,MAAM,GAAGJ,QAAQ,CAACC,aAAa,CAACC,wBAAwB,CAAC,CAAC,GAAG,GAAG,CAAC;IAClG,MAAMG,WAAW,GAAGF,aAAa,CAACG,IAAI,CAACP,MAAM,CAAC;IAC9C,IAAIM,WAAW,EAAE;MAChB,IAAIE,cAAc;MAClB,IAAIC,WAAW;MACf;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA,MAAMC,mBAAmB,GAAGJ,WAAW,CAACK,MAAM,GAAG,CAAC;MAClD,MAAMC,iBAAiB,GAAGF,mBAAmB,GAAG,CAAC,IAAIJ,WAAW,CAACI,mBAAmB,CAAC;MACrF,IAAIT,QAAQ,CAACY,2BAA2B,CAAC,CAAC,IAAID,iBAAiB,EAAE;QAChEJ,cAAc,GAAGR,MAAM,CAACc,OAAO,CAC9BV,aAAa,EACbH,QAAQ,CAACY,2BAA2B,CAAC,CACtC,CAAC;QACD;QACA;QACA,IAAIH,mBAAmB,GAAG,CAAC,EAAE;UAC5BD,WAAW,GAAGH,WAAW,CAAC,CAAC,CAAC;QAC7B;MACD;MACA;MACA;MACA;MACA;MACA;MACA;MAAA,KACK;QACJ;QACA;QACA;QACA;QACA;QACA,MAAMS,0BAA0B,GAAGT,WAAW,CAAC,CAAC,CAAC;QACjDE,cAAc,GAAGR,MAAM,CAACgB,KAAK,CAACD,0BAA0B,CAACJ,MAAM,CAAC;QAChE;QACA;QACA,IAAIC,iBAAiB,EAAE;UACtBH,WAAW,GAAGH,WAAW,CAAC,CAAC,CAAC;QAC7B;MACD;MACA;MACA;MACA;MACA;MACA;MACA,IAAIW,cAAc;MAClB,IAAIL,iBAAiB,EAAE;QACtB,MAAMM,uCAAuC,GAAGlB,MAAM,CAACmB,OAAO,CAACb,WAAW,CAAC,CAAC,CAAC,CAAC;QAC9E,MAAMc,sBAAsB,GAAGpB,MAAM,CAACgB,KAAK,CAAC,CAAC,EAAEE,uCAAuC,CAAC;QACvF;QACA;QACA;QACA;QACA;QACA;QACA,IAAIE,sBAAsB,KAAKnB,QAAQ,CAACC,aAAa,CAACe,cAAc,CAAC,CAAC,EAAE;UACvEA,cAAc,GAAGhB,QAAQ,CAACC,aAAa,CAACe,cAAc,CAAC,CAAC;QACzD;MACD,CAAC,MAAM;QACNA,cAAc,GAAGX,WAAW,CAAC,CAAC,CAAC;MAChC;MACA,OAAO;QACNE,cAAc;QACdS,cAAc;QACdR;MACD,CAAC;IACF;EACD;EACE,OAAO;IACND,cAAc,EAAER;EACjB,CAAC;AACJ","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extractNationalNumberFromPossiblyIncompleteNumber.test.js","names":["describe","it","expect","Metadata","metadata","type","extractNationalNumberFromPossiblyIncompleteNumber","meta","country","to","deep","equal","nationalPrefix","undefined","carrierCode","nationalNumber"],"sources":["../../source/helpers/extractNationalNumberFromPossiblyIncompleteNumber.test.js"],"sourcesContent":["import { describe, it } from 'mocha'\r\nimport { expect } from 'chai'\r\n\r\nimport Metadata from '../metadata.js'\r\nimport metadata from '../../metadata.min.json' with { type: 'json' }\r\nimport extractNationalNumberFromPossiblyIncompleteNumber from './extractNationalNumberFromPossiblyIncompleteNumber.js'\r\n\r\ndescribe('extractNationalNumberFromPossiblyIncompleteNumber', () => {\r\n\tit('should parse a carrier code when there is no national prefix transform rule', () => {\r\n\t\tconst meta = new Metadata(metadata)\r\n\t\tmeta.country('AU')\r\n\t\texpect(extractNationalNumberFromPossiblyIncompleteNumber('18311800123', meta)).to.deep.equal({\r\n\t\t\tnationalPrefix: undefined,\r\n\t\t\tcarrierCode: '1831',\r\n\t\t\tnationalNumber: '1800123'\r\n\t\t})\r\n\t})\r\n})"],"mappings":"AAAA,SAASA,QAAQ,EAAEC,EAAE,QAAQ,OAAO;AACpC,SAASC,MAAM,QAAQ,MAAM;AAE7B,OAAOC,QAAQ,MAAM,gBAAgB;AACrC,OAAOC,QAAQ,MAAM,yBAAyB,QAAQC,IAAI,EAAE,MAAM;AAClE,OAAOC,iDAAiD,MAAM,wDAAwD;AAEtHN,QAAQ,CAAC,mDAAmD,EAAE,MAAM;EACnEC,EAAE,CAAC,6EAA6E,EAAE,MAAM;IACvF,MAAMM,IAAI,GAAG,IAAIJ,QAAQ,CAACC,QAAQ,CAAC;IACnCG,IAAI,CAACC,OAAO,CAAC,IAAI,CAAC;IAClBN,MAAM,CAACI,iDAAiD,CAAC,aAAa,EAAEC,IAAI,CAAC,CAAC,CAACE,EAAE,CAACC,IAAI,CAACC,KAAK,CAAC;MAC5FC,cAAc,EAAEC,SAAS;MACzBC,WAAW,EAAE,MAAM;MACnBC,cAAc,EAAE;IACjB,CAAC,CAAC;EACH,CAAC,CAAC;AACH,CAAC,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// When phone numbers are written in `RFC3966` format — `"tel:+12133734253"` —
|
|
2
|
+
// they can have their "calling code" part written separately in a `phone-context` parameter.
|
|
3
|
+
// Example: `"tel:12133734253;phone-context=+1"`.
|
|
4
|
+
// This function parses the full phone number from the local number and the `phone-context`
|
|
5
|
+
// when the `phone-context` contains a `+` sign.
|
|
6
|
+
|
|
7
|
+
import { VALID_DIGITS
|
|
8
|
+
// PLUS_CHARS
|
|
9
|
+
} from '../constants.js';
|
|
10
|
+
export const PLUS_SIGN = '+';
|
|
11
|
+
const RFC3966_VISUAL_SEPARATOR_ = '[\\-\\.\\(\\)]?';
|
|
12
|
+
const RFC3966_PHONE_DIGIT_ = '(' + '[' + VALID_DIGITS + ']' + '|' + RFC3966_VISUAL_SEPARATOR_ + ')';
|
|
13
|
+
const RFC3966_GLOBAL_NUMBER_DIGITS_ = '^' + '\\' + PLUS_SIGN + RFC3966_PHONE_DIGIT_ + '*' + '[' + VALID_DIGITS + ']' + RFC3966_PHONE_DIGIT_ + '*' + '$';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Regular expression of valid global-number-digits for the phone-context
|
|
17
|
+
* parameter, following the syntax defined in RFC3966.
|
|
18
|
+
*/
|
|
19
|
+
const RFC3966_GLOBAL_NUMBER_DIGITS_PATTERN_ = new RegExp(RFC3966_GLOBAL_NUMBER_DIGITS_, 'g');
|
|
20
|
+
|
|
21
|
+
// In this port of Google's library, we don't accept alpha characters in phone numbers.
|
|
22
|
+
// const ALPHANUM_ = VALID_ALPHA_ + VALID_DIGITS
|
|
23
|
+
const ALPHANUM_ = VALID_DIGITS;
|
|
24
|
+
const RFC3966_DOMAINLABEL_ = '[' + ALPHANUM_ + ']+((\\-)*[' + ALPHANUM_ + '])*';
|
|
25
|
+
const VALID_ALPHA_ = 'a-zA-Z';
|
|
26
|
+
const RFC3966_TOPLABEL_ = '[' + VALID_ALPHA_ + ']+((\\-)*[' + ALPHANUM_ + '])*';
|
|
27
|
+
const RFC3966_DOMAINNAME_ = '^(' + RFC3966_DOMAINLABEL_ + '\\.)*' + RFC3966_TOPLABEL_ + '\\.?$';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Regular expression of valid domainname for the phone-context parameter,
|
|
31
|
+
* following the syntax defined in RFC3966.
|
|
32
|
+
*/
|
|
33
|
+
const RFC3966_DOMAINNAME_PATTERN_ = new RegExp(RFC3966_DOMAINNAME_, 'g');
|
|
34
|
+
export const RFC3966_PREFIX_ = 'tel:';
|
|
35
|
+
export const RFC3966_PHONE_CONTEXT_ = ';phone-context=';
|
|
36
|
+
export const RFC3966_ISDN_SUBADDRESS_ = ';isub=';
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Extracts the value of the phone-context parameter of `numberToExtractFrom`,
|
|
40
|
+
* following the syntax defined in RFC3966.
|
|
41
|
+
*
|
|
42
|
+
* @param {string} numberToExtractFrom
|
|
43
|
+
* @return {string|null} the extracted string (possibly empty), or `null` if no phone-context parameter is found.
|
|
44
|
+
*/
|
|
45
|
+
export default function extractPhoneContext(numberToExtractFrom) {
|
|
46
|
+
const indexOfPhoneContext = numberToExtractFrom.indexOf(RFC3966_PHONE_CONTEXT_);
|
|
47
|
+
// If no phone-context parameter is present
|
|
48
|
+
if (indexOfPhoneContext < 0) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
const phoneContextStart = indexOfPhoneContext + RFC3966_PHONE_CONTEXT_.length;
|
|
52
|
+
// If phone-context parameter is empty
|
|
53
|
+
if (phoneContextStart >= numberToExtractFrom.length) {
|
|
54
|
+
return '';
|
|
55
|
+
}
|
|
56
|
+
const phoneContextEnd = numberToExtractFrom.indexOf(';', phoneContextStart);
|
|
57
|
+
// If phone-context is not the last parameter
|
|
58
|
+
if (phoneContextEnd >= 0) {
|
|
59
|
+
return numberToExtractFrom.substring(phoneContextStart, phoneContextEnd);
|
|
60
|
+
} else {
|
|
61
|
+
return numberToExtractFrom.substring(phoneContextStart);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Returns whether the value of phoneContext follows the syntax defined in RFC3966.
|
|
67
|
+
*
|
|
68
|
+
* @param {string|null} phoneContext
|
|
69
|
+
* @return {boolean}
|
|
70
|
+
*/
|
|
71
|
+
export function isPhoneContextValid(phoneContext) {
|
|
72
|
+
if (phoneContext === null) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
if (phoneContext.length === 0) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Does phone-context value match pattern of global-number-digits or domainname.
|
|
80
|
+
return RFC3966_GLOBAL_NUMBER_DIGITS_PATTERN_.test(phoneContext) || RFC3966_DOMAINNAME_PATTERN_.test(phoneContext);
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=extractPhoneContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extractPhoneContext.js","names":["VALID_DIGITS","PLUS_SIGN","RFC3966_VISUAL_SEPARATOR_","RFC3966_PHONE_DIGIT_","RFC3966_GLOBAL_NUMBER_DIGITS_","RFC3966_GLOBAL_NUMBER_DIGITS_PATTERN_","RegExp","ALPHANUM_","RFC3966_DOMAINLABEL_","VALID_ALPHA_","RFC3966_TOPLABEL_","RFC3966_DOMAINNAME_","RFC3966_DOMAINNAME_PATTERN_","RFC3966_PREFIX_","RFC3966_PHONE_CONTEXT_","RFC3966_ISDN_SUBADDRESS_","extractPhoneContext","numberToExtractFrom","indexOfPhoneContext","indexOf","phoneContextStart","length","phoneContextEnd","substring","isPhoneContextValid","phoneContext","test"],"sources":["../../source/helpers/extractPhoneContext.js"],"sourcesContent":["// When phone numbers are written in `RFC3966` format — `\"tel:+12133734253\"` —\r\n// they can have their \"calling code\" part written separately in a `phone-context` parameter.\r\n// Example: `\"tel:12133734253;phone-context=+1\"`.\r\n// This function parses the full phone number from the local number and the `phone-context`\r\n// when the `phone-context` contains a `+` sign.\r\n\r\nimport {\r\n VALID_DIGITS,\r\n // PLUS_CHARS\r\n} from '../constants.js'\r\n\r\nexport const PLUS_SIGN = '+'\r\n\r\nconst RFC3966_VISUAL_SEPARATOR_ = '[\\\\-\\\\.\\\\(\\\\)]?'\r\n\r\nconst RFC3966_PHONE_DIGIT_ = '(' + '[' + VALID_DIGITS + ']' + '|' + RFC3966_VISUAL_SEPARATOR_ + ')'\r\n\r\nconst RFC3966_GLOBAL_NUMBER_DIGITS_ =\r\n\t'^' +\r\n\t'\\\\' +\r\n\tPLUS_SIGN +\r\n\tRFC3966_PHONE_DIGIT_ +\r\n\t'*' +\r\n\t'[' + VALID_DIGITS + ']' +\r\n\tRFC3966_PHONE_DIGIT_ +\r\n\t'*' +\r\n\t'$'\r\n\r\n/**\r\n * Regular expression of valid global-number-digits for the phone-context\r\n * parameter, following the syntax defined in RFC3966.\r\n */\r\nconst RFC3966_GLOBAL_NUMBER_DIGITS_PATTERN_ = new RegExp(RFC3966_GLOBAL_NUMBER_DIGITS_, 'g')\r\n\r\n// In this port of Google's library, we don't accept alpha characters in phone numbers.\r\n// const ALPHANUM_ = VALID_ALPHA_ + VALID_DIGITS\r\nconst ALPHANUM_ = VALID_DIGITS\r\n\r\nconst RFC3966_DOMAINLABEL_ = '[' + ALPHANUM_ + ']+((\\\\-)*[' + ALPHANUM_ + '])*'\r\n\r\nconst VALID_ALPHA_ = 'a-zA-Z'\r\nconst RFC3966_TOPLABEL_ = '[' + VALID_ALPHA_ + ']+((\\\\-)*[' + ALPHANUM_ + '])*'\r\n\r\nconst RFC3966_DOMAINNAME_ = '^(' + RFC3966_DOMAINLABEL_ + '\\\\.)*' + RFC3966_TOPLABEL_ + '\\\\.?$'\r\n\r\n/**\r\n * Regular expression of valid domainname for the phone-context parameter,\r\n * following the syntax defined in RFC3966.\r\n */\r\nconst RFC3966_DOMAINNAME_PATTERN_ = new RegExp(RFC3966_DOMAINNAME_, 'g')\r\n\r\nexport const RFC3966_PREFIX_ = 'tel:'\r\nexport const RFC3966_PHONE_CONTEXT_ = ';phone-context='\r\nexport const RFC3966_ISDN_SUBADDRESS_ = ';isub='\r\n\r\n/**\r\n * Extracts the value of the phone-context parameter of `numberToExtractFrom`,\r\n * following the syntax defined in RFC3966.\r\n *\r\n * @param {string} numberToExtractFrom\r\n * @return {string|null} the extracted string (possibly empty), or `null` if no phone-context parameter is found.\r\n */\r\nexport default function extractPhoneContext(numberToExtractFrom) {\r\n\tconst indexOfPhoneContext = numberToExtractFrom.indexOf(RFC3966_PHONE_CONTEXT_)\r\n\t// If no phone-context parameter is present\r\n\tif (indexOfPhoneContext < 0) {\r\n\t\treturn null\r\n\t}\r\n\r\n\tconst phoneContextStart = indexOfPhoneContext + RFC3966_PHONE_CONTEXT_.length\r\n\t// If phone-context parameter is empty\r\n\tif (phoneContextStart >= numberToExtractFrom.length) {\r\n\t\treturn ''\r\n\t}\r\n\r\n\tconst phoneContextEnd = numberToExtractFrom.indexOf(';', phoneContextStart)\r\n\t// If phone-context is not the last parameter\r\n\tif (phoneContextEnd >= 0) {\r\n\t\treturn numberToExtractFrom.substring(phoneContextStart, phoneContextEnd)\r\n\t} else {\r\n\t\treturn numberToExtractFrom.substring(phoneContextStart)\r\n\t}\r\n}\r\n\r\n/**\r\n * Returns whether the value of phoneContext follows the syntax defined in RFC3966.\r\n *\r\n * @param {string|null} phoneContext\r\n * @return {boolean}\r\n */\r\nexport function isPhoneContextValid(phoneContext) {\r\n\tif (phoneContext === null) {\r\n\t\treturn true\r\n\t}\r\n\r\n\tif (phoneContext.length === 0) {\r\n\t\treturn false\r\n\t}\r\n\r\n\t// Does phone-context value match pattern of global-number-digits or domainname.\r\n\treturn RFC3966_GLOBAL_NUMBER_DIGITS_PATTERN_.test(phoneContext) ||\r\n\t\tRFC3966_DOMAINNAME_PATTERN_.test(phoneContext)\r\n}"],"mappings":"AAAA;AACA;AACA;AACA;AACA;;AAEA,SACEA;AACA;AAAA,OACK,iBAAiB;AAExB,OAAO,MAAMC,SAAS,GAAG,GAAG;AAE5B,MAAMC,yBAAyB,GAAG,iBAAiB;AAEnD,MAAMC,oBAAoB,GAAG,GAAG,GAAG,GAAG,GAAGH,YAAY,GAAG,GAAG,GAAG,GAAG,GAAGE,yBAAyB,GAAG,GAAG;AAEnG,MAAME,6BAA6B,GAClC,GAAG,GACH,IAAI,GACJH,SAAS,GACTE,oBAAoB,GACpB,GAAG,GACH,GAAG,GAAGH,YAAY,GAAI,GAAG,GACzBG,oBAAoB,GACpB,GAAG,GACH,GAAG;;AAEJ;AACA;AACA;AACA;AACA,MAAME,qCAAqC,GAAG,IAAIC,MAAM,CAACF,6BAA6B,EAAE,GAAG,CAAC;;AAE5F;AACA;AACA,MAAMG,SAAS,GAAGP,YAAY;AAE9B,MAAMQ,oBAAoB,GAAG,GAAG,GAAGD,SAAS,GAAG,YAAY,GAAGA,SAAS,GAAG,KAAK;AAE/E,MAAME,YAAY,GAAG,QAAQ;AAC7B,MAAMC,iBAAiB,GAAG,GAAG,GAAGD,YAAY,GAAG,YAAY,GAAGF,SAAS,GAAG,KAAK;AAE/E,MAAMI,mBAAmB,GAAG,IAAI,GAAGH,oBAAoB,GAAG,OAAO,GAAGE,iBAAiB,GAAG,OAAO;;AAE/F;AACA;AACA;AACA;AACA,MAAME,2BAA2B,GAAG,IAAIN,MAAM,CAACK,mBAAmB,EAAE,GAAG,CAAC;AAExE,OAAO,MAAME,eAAe,GAAG,MAAM;AACrC,OAAO,MAAMC,sBAAsB,GAAG,iBAAiB;AACvD,OAAO,MAAMC,wBAAwB,GAAG,QAAQ;;AAEhD;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,mBAAmBA,CAACC,mBAAmB,EAAE;EAChE,MAAMC,mBAAmB,GAAGD,mBAAmB,CAACE,OAAO,CAACL,sBAAsB,CAAC;EAC/E;EACA,IAAII,mBAAmB,GAAG,CAAC,EAAE;IAC5B,OAAO,IAAI;EACZ;EAEA,MAAME,iBAAiB,GAAGF,mBAAmB,GAAGJ,sBAAsB,CAACO,MAAM;EAC7E;EACA,IAAID,iBAAiB,IAAIH,mBAAmB,CAACI,MAAM,EAAE;IACpD,OAAO,EAAE;EACV;EAEA,MAAMC,eAAe,GAAGL,mBAAmB,CAACE,OAAO,CAAC,GAAG,EAAEC,iBAAiB,CAAC;EAC3E;EACA,IAAIE,eAAe,IAAI,CAAC,EAAE;IACzB,OAAOL,mBAAmB,CAACM,SAAS,CAACH,iBAAiB,EAAEE,eAAe,CAAC;EACzE,CAAC,MAAM;IACN,OAAOL,mBAAmB,CAACM,SAAS,CAACH,iBAAiB,CAAC;EACxD;AACD;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASI,mBAAmBA,CAACC,YAAY,EAAE;EACjD,IAAIA,YAAY,KAAK,IAAI,EAAE;IAC1B,OAAO,IAAI;EACZ;EAEA,IAAIA,YAAY,CAACJ,MAAM,KAAK,CAAC,EAAE;IAC9B,OAAO,KAAK;EACb;;EAEA;EACA,OAAOhB,qCAAqC,CAACqB,IAAI,CAACD,YAAY,CAAC,IAC9Db,2BAA2B,CAACc,IAAI,CAACD,YAAY,CAAC;AAChD","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extractPhoneContext.test.js","names":["describe","it","expect","parsePhoneNumber_","PhoneNumber","metadata","type","parsePhoneNumber","parameters","push","apply","NZ_NUMBER","expectPhoneNumbersToBeEqual","nzFromPhoneContext","brFromPhoneContext","usFromPhoneContext","expectToThrowForInvalidPhoneContext","string","to","be","undefined","phoneNumber1","phoneNumber2","number","ext"],"sources":["../../source/helpers/extractPhoneContext.test.js"],"sourcesContent":["import { describe, it } from 'mocha'\r\nimport { expect } from 'chai'\r\n\r\nimport parsePhoneNumber_ from '../parsePhoneNumber.js'\r\nimport PhoneNumber from '../PhoneNumber.js'\r\nimport metadata from '../../metadata.min.json' with { type: 'json' }\r\n\r\nfunction parsePhoneNumber(...parameters) {\r\n\tparameters.push(metadata)\r\n\treturn parsePhoneNumber_.apply(this, parameters)\r\n}\r\n\r\ndescribe('extractPhoneContext', function() {\r\n\tit('should parse RFC 3966 phone number URIs', function() {\r\n\t\t// context = \";phone-context=\" descriptor\r\n\t\t// descriptor = domainname / global-number-digits\r\n\r\n\t\tconst NZ_NUMBER = new PhoneNumber('64', '33316005', metadata)\r\n\r\n\t\t// Valid global-phone-digits\r\n\t\texpectPhoneNumbersToBeEqual(\r\n\t\t\tparsePhoneNumber('tel:033316005;phone-context=+64'),\r\n\t\t\tNZ_NUMBER\r\n\t\t)\r\n\r\n\t\texpectPhoneNumbersToBeEqual(\r\n\t\t\tparsePhoneNumber('tel:033316005;phone-context=+64;{this isn\\'t part of phone-context anymore!}'),\r\n\t\t\tNZ_NUMBER\r\n\t\t)\r\n\r\n\t\tconst nzFromPhoneContext = new PhoneNumber('64', '3033316005', metadata)\r\n\t\texpectPhoneNumbersToBeEqual(\r\n\t\t\tparsePhoneNumber('tel:033316005;phone-context=+64-3'),\r\n\t\t\tnzFromPhoneContext\r\n\t\t)\r\n\r\n\t\tconst brFromPhoneContext = new PhoneNumber('55', '5033316005', metadata)\r\n\t\texpectPhoneNumbersToBeEqual(\r\n\t\t\tparsePhoneNumber('tel:033316005;phone-context=+(555)'),\r\n\t\t\tbrFromPhoneContext\r\n\t\t)\r\n\r\n\t\tconst usFromPhoneContext = new PhoneNumber('1', '23033316005', metadata)\r\n\t\texpectPhoneNumbersToBeEqual(\r\n\t\t\tparsePhoneNumber('tel:033316005;phone-context=+-1-2.3()'),\r\n\t\t\tusFromPhoneContext\r\n\t\t)\r\n\r\n\t\t// Valid domainname.\r\n\t\texpectPhoneNumbersToBeEqual(\r\n\t\t\tparsePhoneNumber('tel:033316005;phone-context=abc.nz', 'NZ'),\r\n\t\t\tNZ_NUMBER\r\n\t\t)\r\n\t\texpectPhoneNumbersToBeEqual(\r\n\t\t\tparsePhoneNumber('tel:033316005;phone-context=www.PHONE-numb3r.com', 'NZ'),\r\n\t\t\tNZ_NUMBER\r\n\t\t)\r\n\t\texpectPhoneNumbersToBeEqual(\r\n\t\t\tparsePhoneNumber('tel:033316005;phone-context=a', 'NZ'),\r\n\t\t\tNZ_NUMBER\r\n\t\t)\r\n\t\texpectPhoneNumbersToBeEqual(\r\n\t\t\tparsePhoneNumber('tel:033316005;phone-context=3phone.J.', 'NZ'),\r\n\t\t\tNZ_NUMBER\r\n\t\t)\r\n\t\texpectPhoneNumbersToBeEqual(\r\n\t\t\tparsePhoneNumber('tel:033316005;phone-context=a--z', 'NZ'),\r\n\t\t\tNZ_NUMBER\r\n\t\t)\r\n\r\n\t\t// Should strip ISDN subaddress.\r\n\t\texpectPhoneNumbersToBeEqual(\r\n\t\t\tparsePhoneNumber('tel:033316005;isub=/@;phone-context=+64', 'NZ'),\r\n\t\t\tNZ_NUMBER\r\n\t\t)\r\n\r\n\t\t// // Should support incorrectly-written RFC 3966 phone numbers:\r\n\t\t// // the ones written without a `tel:` prefix.\r\n\t\t// expectPhoneNumbersToBeEqual(\r\n\t\t// \tparsePhoneNumber('033316005;phone-context=+64', 'NZ'),\r\n\t\t// \tNZ_NUMBER\r\n\t\t// )\r\n\r\n\t\t// Invalid descriptor.\r\n\t\texpectToThrowForInvalidPhoneContext('tel:033316005;phone-context=')\r\n\t\texpectToThrowForInvalidPhoneContext('tel:033316005;phone-context=+')\r\n\t\texpectToThrowForInvalidPhoneContext('tel:033316005;phone-context=64')\r\n\t\texpectToThrowForInvalidPhoneContext('tel:033316005;phone-context=++64')\r\n\t\texpectToThrowForInvalidPhoneContext('tel:033316005;phone-context=+abc')\r\n\t\texpectToThrowForInvalidPhoneContext('tel:033316005;phone-context=.')\r\n\t\texpectToThrowForInvalidPhoneContext('tel:033316005;phone-context=3phone')\r\n\t\texpectToThrowForInvalidPhoneContext('tel:033316005;phone-context=a-.nz')\r\n\t\texpectToThrowForInvalidPhoneContext('tel:033316005;phone-context=a{b}c')\r\n\t})\r\n})\r\n\r\nfunction expectToThrowForInvalidPhoneContext(string) {\r\n\texpect(parsePhoneNumber(string)).to.be.undefined\r\n}\r\n\r\nfunction expectPhoneNumbersToBeEqual(phoneNumber1, phoneNumber2) {\r\n\tif (!phoneNumber1 || !phoneNumber2) {\r\n\t\treturn false\r\n\t}\r\n\treturn phoneNumber1.number === phoneNumber2.number &&\r\n\t\tphoneNumber1.ext === phoneNumber2.ext\r\n}"],"mappings":"AAAA,SAASA,QAAQ,EAAEC,EAAE,QAAQ,OAAO;AACpC,SAASC,MAAM,QAAQ,MAAM;AAE7B,OAAOC,iBAAiB,MAAM,wBAAwB;AACtD,OAAOC,WAAW,MAAM,mBAAmB;AAC3C,OAAOC,QAAQ,MAAM,yBAAyB,QAAQC,IAAI,EAAE,MAAM;AAElE,SAASC,gBAAgBA,CAAC,GAAGC,UAAU,EAAE;EACxCA,UAAU,CAACC,IAAI,CAACJ,QAAQ,CAAC;EACzB,OAAOF,iBAAiB,CAACO,KAAK,CAAC,IAAI,EAAEF,UAAU,CAAC;AACjD;AAEAR,QAAQ,CAAC,qBAAqB,EAAE,YAAW;EAC1CC,EAAE,CAAC,yCAAyC,EAAE,YAAW;IACxD;IACA;;IAEA,MAAMU,SAAS,GAAG,IAAIP,WAAW,CAAC,IAAI,EAAE,UAAU,EAAEC,QAAQ,CAAC;;IAE7D;IACAO,2BAA2B,CAC1BL,gBAAgB,CAAC,iCAAiC,CAAC,EACnDI,SACD,CAAC;IAEDC,2BAA2B,CAC1BL,gBAAgB,CAAC,8EAA8E,CAAC,EAChGI,SACD,CAAC;IAED,MAAME,kBAAkB,GAAG,IAAIT,WAAW,CAAC,IAAI,EAAE,YAAY,EAAEC,QAAQ,CAAC;IACxEO,2BAA2B,CAC1BL,gBAAgB,CAAC,mCAAmC,CAAC,EACrDM,kBACD,CAAC;IAED,MAAMC,kBAAkB,GAAG,IAAIV,WAAW,CAAC,IAAI,EAAE,YAAY,EAAEC,QAAQ,CAAC;IACxEO,2BAA2B,CAC1BL,gBAAgB,CAAC,oCAAoC,CAAC,EACtDO,kBACD,CAAC;IAED,MAAMC,kBAAkB,GAAG,IAAIX,WAAW,CAAC,GAAG,EAAE,aAAa,EAAEC,QAAQ,CAAC;IACxEO,2BAA2B,CAC1BL,gBAAgB,CAAC,uCAAuC,CAAC,EACzDQ,kBACD,CAAC;;IAED;IACAH,2BAA2B,CAC1BL,gBAAgB,CAAC,oCAAoC,EAAE,IAAI,CAAC,EAC5DI,SACD,CAAC;IACDC,2BAA2B,CAC1BL,gBAAgB,CAAC,kDAAkD,EAAE,IAAI,CAAC,EAC1EI,SACD,CAAC;IACDC,2BAA2B,CAC1BL,gBAAgB,CAAC,+BAA+B,EAAE,IAAI,CAAC,EACvDI,SACD,CAAC;IACDC,2BAA2B,CAC1BL,gBAAgB,CAAC,uCAAuC,EAAE,IAAI,CAAC,EAC/DI,SACD,CAAC;IACDC,2BAA2B,CAC1BL,gBAAgB,CAAC,kCAAkC,EAAE,IAAI,CAAC,EAC1DI,SACD,CAAC;;IAED;IACAC,2BAA2B,CAC1BL,gBAAgB,CAAC,yCAAyC,EAAE,IAAI,CAAC,EACjEI,SACD,CAAC;;IAED;IACA;IACA;IACA;IACA;IACA;;IAEA;IACAK,mCAAmC,CAAC,8BAA8B,CAAC;IACnEA,mCAAmC,CAAC,+BAA+B,CAAC;IACpEA,mCAAmC,CAAC,gCAAgC,CAAC;IACrEA,mCAAmC,CAAC,kCAAkC,CAAC;IACvEA,mCAAmC,CAAC,kCAAkC,CAAC;IACvEA,mCAAmC,CAAC,+BAA+B,CAAC;IACpEA,mCAAmC,CAAC,oCAAoC,CAAC;IACzEA,mCAAmC,CAAC,mCAAmC,CAAC;IACxEA,mCAAmC,CAAC,mCAAmC,CAAC;EACzE,CAAC,CAAC;AACH,CAAC,CAAC;AAEF,SAASA,mCAAmCA,CAACC,MAAM,EAAE;EACpDf,MAAM,CAACK,gBAAgB,CAACU,MAAM,CAAC,CAAC,CAACC,EAAE,CAACC,EAAE,CAACC,SAAS;AACjD;AAEA,SAASR,2BAA2BA,CAACS,YAAY,EAAEC,YAAY,EAAE;EAChE,IAAI,CAACD,YAAY,IAAI,CAACC,YAAY,EAAE;IACnC,OAAO,KAAK;EACb;EACA,OAAOD,YAAY,CAACE,MAAM,KAAKD,YAAY,CAACC,MAAM,IACjDF,YAAY,CAACG,GAAG,KAAKF,YAAY,CAACE,GAAG;AACvC","ignoreList":[]}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import applyInternationalSeparatorStyle from './applyInternationalSeparatorStyle.js';
|
|
2
|
+
|
|
3
|
+
// This was originally set to $1 but there are some countries for which the
|
|
4
|
+
// first group is not used in the national pattern (e.g. Argentina) so the $1
|
|
5
|
+
// group does not match correctly. Therefore, we use `\d`, so that the first
|
|
6
|
+
// group actually used in the pattern will be matched.
|
|
7
|
+
export const FIRST_GROUP_PATTERN = /(\$\d)/;
|
|
8
|
+
export default function formatNationalNumberUsingFormat(number, format, {
|
|
9
|
+
useInternationalFormat,
|
|
10
|
+
withNationalPrefix,
|
|
11
|
+
carrierCode,
|
|
12
|
+
metadata
|
|
13
|
+
}) {
|
|
14
|
+
const formattedNumber = number.replace(new RegExp(format.pattern()), useInternationalFormat ? format.internationalFormat() :
|
|
15
|
+
// This library doesn't use `domestic_carrier_code_formatting_rule`,
|
|
16
|
+
// because that one is only used when formatting phone numbers
|
|
17
|
+
// for dialing from a mobile phone, and this is not a dialing library.
|
|
18
|
+
// carrierCode && format.domesticCarrierCodeFormattingRule()
|
|
19
|
+
// // First, replace the $CC in the formatting rule with the desired carrier code.
|
|
20
|
+
// // Then, replace the $FG in the formatting rule with the first group
|
|
21
|
+
// // and the carrier code combined in the appropriate way.
|
|
22
|
+
// ? format.format().replace(FIRST_GROUP_PATTERN, format.domesticCarrierCodeFormattingRule().replace('$CC', carrierCode))
|
|
23
|
+
// : (
|
|
24
|
+
// withNationalPrefix && format.nationalPrefixFormattingRule()
|
|
25
|
+
// ? format.format().replace(FIRST_GROUP_PATTERN, format.nationalPrefixFormattingRule())
|
|
26
|
+
// : format.format()
|
|
27
|
+
// )
|
|
28
|
+
withNationalPrefix && format.nationalPrefixFormattingRule() ? format.format().replace(FIRST_GROUP_PATTERN, format.nationalPrefixFormattingRule()) : format.format());
|
|
29
|
+
if (useInternationalFormat) {
|
|
30
|
+
return applyInternationalSeparatorStyle(formattedNumber);
|
|
31
|
+
}
|
|
32
|
+
return formattedNumber;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=formatNationalNumberUsingFormat.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatNationalNumberUsingFormat.js","names":["applyInternationalSeparatorStyle","FIRST_GROUP_PATTERN","formatNationalNumberUsingFormat","number","format","useInternationalFormat","withNationalPrefix","carrierCode","metadata","formattedNumber","replace","RegExp","pattern","internationalFormat","nationalPrefixFormattingRule"],"sources":["../../source/helpers/formatNationalNumberUsingFormat.js"],"sourcesContent":["import applyInternationalSeparatorStyle from './applyInternationalSeparatorStyle.js'\r\n\r\n// This was originally set to $1 but there are some countries for which the\r\n// first group is not used in the national pattern (e.g. Argentina) so the $1\r\n// group does not match correctly. Therefore, we use `\\d`, so that the first\r\n// group actually used in the pattern will be matched.\r\nexport const FIRST_GROUP_PATTERN = /(\\$\\d)/\r\n\r\nexport default function formatNationalNumberUsingFormat(\r\n\tnumber,\r\n\tformat,\r\n\t{\r\n\t\tuseInternationalFormat,\r\n\t\twithNationalPrefix,\r\n\t\tcarrierCode,\r\n\t\tmetadata\r\n\t}\r\n) {\r\n\tconst formattedNumber = number.replace(\r\n\t\tnew RegExp(format.pattern()),\r\n\t\tuseInternationalFormat\r\n\t\t\t? format.internationalFormat()\r\n\t\t\t: (\r\n\t\t\t\t// This library doesn't use `domestic_carrier_code_formatting_rule`,\r\n\t\t\t\t// because that one is only used when formatting phone numbers\r\n\t\t\t\t// for dialing from a mobile phone, and this is not a dialing library.\r\n\t\t\t\t// carrierCode && format.domesticCarrierCodeFormattingRule()\r\n\t\t\t\t// \t// First, replace the $CC in the formatting rule with the desired carrier code.\r\n\t\t\t\t// \t// Then, replace the $FG in the formatting rule with the first group\r\n\t\t\t\t// \t// and the carrier code combined in the appropriate way.\r\n\t\t\t\t// \t? format.format().replace(FIRST_GROUP_PATTERN, format.domesticCarrierCodeFormattingRule().replace('$CC', carrierCode))\r\n\t\t\t\t// \t: (\r\n\t\t\t\t// \t\twithNationalPrefix && format.nationalPrefixFormattingRule()\r\n\t\t\t\t// \t\t\t? format.format().replace(FIRST_GROUP_PATTERN, format.nationalPrefixFormattingRule())\r\n\t\t\t\t// \t\t\t: format.format()\r\n\t\t\t\t// \t)\r\n\t\t\t\twithNationalPrefix && format.nationalPrefixFormattingRule()\r\n\t\t\t\t\t? format.format().replace(FIRST_GROUP_PATTERN, format.nationalPrefixFormattingRule())\r\n\t\t\t\t\t: format.format()\r\n\t\t\t)\r\n\t)\r\n\tif (useInternationalFormat) {\r\n\t\treturn applyInternationalSeparatorStyle(formattedNumber)\r\n\t}\r\n\treturn formattedNumber\r\n}"],"mappings":"AAAA,OAAOA,gCAAgC,MAAM,uCAAuC;;AAEpF;AACA;AACA;AACA;AACA,OAAO,MAAMC,mBAAmB,GAAG,QAAQ;AAE3C,eAAe,SAASC,+BAA+BA,CACtDC,MAAM,EACNC,MAAM,EACN;EACCC,sBAAsB;EACtBC,kBAAkB;EAClBC,WAAW;EACXC;AACD,CAAC,EACA;EACD,MAAMC,eAAe,GAAGN,MAAM,CAACO,OAAO,CACrC,IAAIC,MAAM,CAACP,MAAM,CAACQ,OAAO,CAAC,CAAC,CAAC,EAC5BP,sBAAsB,GACnBD,MAAM,CAACS,mBAAmB,CAAC,CAAC;EAE7B;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACAP,kBAAkB,IAAIF,MAAM,CAACU,4BAA4B,CAAC,CAAC,GACxDV,MAAM,CAACA,MAAM,CAAC,CAAC,CAACM,OAAO,CAACT,mBAAmB,EAAEG,MAAM,CAACU,4BAA4B,CAAC,CAAC,CAAC,GACnFV,MAAM,CAACA,MAAM,CAAC,CAEpB,CAAC;EACD,IAAIC,sBAAsB,EAAE;IAC3B,OAAOL,gCAAgC,CAACS,eAAe,CAAC;EACzD;EACA,OAAOA,eAAe;AACvB","ignoreList":[]}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import getCountryByNationalNumber from './getCountryByNationalNumber.js';
|
|
2
|
+
const USE_NON_GEOGRAPHIC_COUNTRY_CODE = false;
|
|
3
|
+
|
|
4
|
+
// Returns the exact country for the `nationalNumber`
|
|
5
|
+
// that belongs to the specified "country calling code".
|
|
6
|
+
export default function getCountryByCallingCode(callingCode, {
|
|
7
|
+
nationalNumber: nationalPhoneNumber,
|
|
8
|
+
metadata
|
|
9
|
+
}) {
|
|
10
|
+
/* istanbul ignore if */
|
|
11
|
+
if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
|
|
12
|
+
if (metadata.isNonGeographicCallingCode(callingCode)) {
|
|
13
|
+
return '001';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const possibleCountries = metadata.getCountryCodesForCallingCode(callingCode);
|
|
17
|
+
if (!possibleCountries) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
// If there's just one country corresponding to the country code,
|
|
21
|
+
// then just return it, without further phone number digits validation.
|
|
22
|
+
if (possibleCountries.length === 1) {
|
|
23
|
+
return possibleCountries[0];
|
|
24
|
+
}
|
|
25
|
+
return getCountryByNationalNumber(nationalPhoneNumber, {
|
|
26
|
+
countries: possibleCountries,
|
|
27
|
+
metadata: metadata.metadata
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=getCountryByCallingCode.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getCountryByCallingCode.js","names":["getCountryByNationalNumber","USE_NON_GEOGRAPHIC_COUNTRY_CODE","getCountryByCallingCode","callingCode","nationalNumber","nationalPhoneNumber","metadata","isNonGeographicCallingCode","possibleCountries","getCountryCodesForCallingCode","length","countries"],"sources":["../../source/helpers/getCountryByCallingCode.js"],"sourcesContent":["import getCountryByNationalNumber from './getCountryByNationalNumber.js'\r\n\r\nconst USE_NON_GEOGRAPHIC_COUNTRY_CODE = false\r\n\r\n// Returns the exact country for the `nationalNumber`\r\n// that belongs to the specified \"country calling code\".\r\nexport default function getCountryByCallingCode(callingCode, {\r\n\tnationalNumber: nationalPhoneNumber,\r\n\tmetadata\r\n}) {\r\n\t/* istanbul ignore if */\r\n\tif (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {\r\n\t\tif (metadata.isNonGeographicCallingCode(callingCode)) {\r\n\t\t\treturn '001'\r\n\t\t}\r\n\t}\r\n\tconst possibleCountries = metadata.getCountryCodesForCallingCode(callingCode)\r\n\tif (!possibleCountries) {\r\n\t\treturn\r\n\t}\r\n\t// If there's just one country corresponding to the country code,\r\n\t// then just return it, without further phone number digits validation.\r\n\tif (possibleCountries.length === 1) {\r\n\t\treturn possibleCountries[0]\r\n\t}\r\n\treturn getCountryByNationalNumber(nationalPhoneNumber, {\r\n\t\tcountries: possibleCountries,\r\n\t\tmetadata: metadata.metadata\r\n\t})\r\n}"],"mappings":"AAAA,OAAOA,0BAA0B,MAAM,iCAAiC;AAExE,MAAMC,+BAA+B,GAAG,KAAK;;AAE7C;AACA;AACA,eAAe,SAASC,uBAAuBA,CAACC,WAAW,EAAE;EAC5DC,cAAc,EAAEC,mBAAmB;EACnCC;AACD,CAAC,EAAE;EACF;EACA,IAAIL,+BAA+B,EAAE;IACpC,IAAIK,QAAQ,CAACC,0BAA0B,CAACJ,WAAW,CAAC,EAAE;MACrD,OAAO,KAAK;IACb;EACD;EACA,MAAMK,iBAAiB,GAAGF,QAAQ,CAACG,6BAA6B,CAACN,WAAW,CAAC;EAC7E,IAAI,CAACK,iBAAiB,EAAE;IACvB;EACD;EACA;EACA;EACA,IAAIA,iBAAiB,CAACE,MAAM,KAAK,CAAC,EAAE;IACnC,OAAOF,iBAAiB,CAAC,CAAC,CAAC;EAC5B;EACA,OAAOR,0BAA0B,CAACK,mBAAmB,EAAE;IACtDM,SAAS,EAAEH,iBAAiB;IAC5BF,QAAQ,EAAEA,QAAQ,CAACA;EACpB,CAAC,CAAC;AACH","ignoreList":[]}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import Metadata from '../metadata.js';
|
|
2
|
+
import getNumberType from './getNumberType.js';
|
|
3
|
+
|
|
4
|
+
// Returns the exact country that the `nationalPhoneNumber` belongs to
|
|
5
|
+
// in cases of ambiguity, i.e. when multiple countries share the same "country calling code".
|
|
6
|
+
export default function getCountryByNationalNumber(nationalPhoneNumber, {
|
|
7
|
+
countries,
|
|
8
|
+
metadata
|
|
9
|
+
}) {
|
|
10
|
+
// Re-create `metadata` because it will be selecting a `country`.
|
|
11
|
+
metadata = new Metadata(metadata);
|
|
12
|
+
|
|
13
|
+
// const matchingCountries = []
|
|
14
|
+
|
|
15
|
+
for (const country of countries) {
|
|
16
|
+
metadata.selectNumberingPlan(country);
|
|
17
|
+
// "Leading digits" patterns are only defined for about 20% of all countries.
|
|
18
|
+
// By definition, matching "leading digits" is a sufficient but not a necessary
|
|
19
|
+
// condition for a phone number to belong to a country.
|
|
20
|
+
// The point of "leading digits" check is that it's the fastest one to get a match.
|
|
21
|
+
// https://gitlab.com/catamphetamine/libphonenumber-js/blob/master/METADATA.md#leading_digits
|
|
22
|
+
// I'd suppose that "leading digits" patterns are mutually exclusive for different countries
|
|
23
|
+
// because of the intended use of that feature.
|
|
24
|
+
if (metadata.leadingDigits()) {
|
|
25
|
+
if (nationalPhoneNumber && nationalPhoneNumber.search(metadata.leadingDigits()) === 0) {
|
|
26
|
+
return country;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// Else perform full validation with all of those
|
|
30
|
+
// fixed-line/mobile/etc regular expressions.
|
|
31
|
+
else if (getNumberType({
|
|
32
|
+
phone: nationalPhoneNumber,
|
|
33
|
+
country
|
|
34
|
+
}, undefined, metadata.metadata)) {
|
|
35
|
+
// When multiple countries share the same "country calling code",
|
|
36
|
+
// type patterns aren't guaranteed to be unique among them.
|
|
37
|
+
// For example, both `US` and `CA` have the same pattern for `toll_free` numbers.
|
|
38
|
+
// https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/103#note_1417147572
|
|
39
|
+
//
|
|
40
|
+
// That means that this `if` condition could be `true` for multiple countries from the list.
|
|
41
|
+
// Currently, it just returns the first one, which is also the "main" country for the "country calling code".
|
|
42
|
+
// In an example with `toll_free` numbers above, `"US"` would be returned even though
|
|
43
|
+
// it could as well be `"CA"`.
|
|
44
|
+
//
|
|
45
|
+
// There was also a time when this attempted to be overly smart
|
|
46
|
+
// and kept track of all such multiple matching countries
|
|
47
|
+
// and then picked the one that matched the `defaultCountry`, if provided.
|
|
48
|
+
// For example, with `toll_free` numbers above, and with `defaultCountry: "CA"`,
|
|
49
|
+
// it would've returned `"CA"` instead of `"US"`.
|
|
50
|
+
// Later it turned out that such "overly smart" behavior turned out to be just confusing,
|
|
51
|
+
// so this "overly smart" country detection was reverted to returning the "main" country
|
|
52
|
+
// for the "country calling code".
|
|
53
|
+
// https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/154
|
|
54
|
+
//
|
|
55
|
+
return country;
|
|
56
|
+
//
|
|
57
|
+
// The "overly smart" behavior code:
|
|
58
|
+
//
|
|
59
|
+
// if (defaultCountry) {
|
|
60
|
+
// if (country === defaultCountry) {
|
|
61
|
+
// return country
|
|
62
|
+
// } else {
|
|
63
|
+
// matchingCountries.push(country)
|
|
64
|
+
// }
|
|
65
|
+
// } else {
|
|
66
|
+
// return country
|
|
67
|
+
// }
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// // Return the first ("main") one of the `matchingCountries`.
|
|
72
|
+
// if (matchingCountries.length > 0) {
|
|
73
|
+
// return matchingCountries[0]
|
|
74
|
+
// }
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=getCountryByNationalNumber.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getCountryByNationalNumber.js","names":["Metadata","getNumberType","getCountryByNationalNumber","nationalPhoneNumber","countries","metadata","country","selectNumberingPlan","leadingDigits","search","phone","undefined"],"sources":["../../source/helpers/getCountryByNationalNumber.js"],"sourcesContent":["import Metadata from '../metadata.js'\r\nimport getNumberType from './getNumberType.js'\r\n\r\n// Returns the exact country that the `nationalPhoneNumber` belongs to\r\n// in cases of ambiguity, i.e. when multiple countries share the same \"country calling code\".\r\nexport default function getCountryByNationalNumber(nationalPhoneNumber, {\r\n\tcountries,\r\n\tmetadata\r\n}) {\r\n\t// Re-create `metadata` because it will be selecting a `country`.\r\n\tmetadata = new Metadata(metadata)\r\n\r\n\t// const matchingCountries = []\r\n\r\n\tfor (const country of countries) {\r\n\t\tmetadata.selectNumberingPlan(country)\r\n\t\t// \"Leading digits\" patterns are only defined for about 20% of all countries.\r\n\t\t// By definition, matching \"leading digits\" is a sufficient but not a necessary\r\n\t\t// condition for a phone number to belong to a country.\r\n\t\t// The point of \"leading digits\" check is that it's the fastest one to get a match.\r\n\t\t// https://gitlab.com/catamphetamine/libphonenumber-js/blob/master/METADATA.md#leading_digits\r\n\t\t// I'd suppose that \"leading digits\" patterns are mutually exclusive for different countries\r\n\t\t// because of the intended use of that feature.\r\n\t\tif (metadata.leadingDigits()) {\r\n\t\t\tif (nationalPhoneNumber &&\r\n\t\t\t\tnationalPhoneNumber.search(metadata.leadingDigits()) === 0) {\r\n\t\t\t\treturn country\r\n\t\t\t}\r\n\t\t}\r\n\t\t// Else perform full validation with all of those\r\n\t\t// fixed-line/mobile/etc regular expressions.\r\n\t\telse if (getNumberType({ phone: nationalPhoneNumber, country }, undefined, metadata.metadata)) {\r\n\t\t\t// When multiple countries share the same \"country calling code\",\r\n\t\t\t// type patterns aren't guaranteed to be unique among them.\r\n\t\t\t// For example, both `US` and `CA` have the same pattern for `toll_free` numbers.\r\n\t\t\t// https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/103#note_1417147572\r\n\t\t\t//\r\n\t\t\t// That means that this `if` condition could be `true` for multiple countries from the list.\r\n\t\t\t// Currently, it just returns the first one, which is also the \"main\" country for the \"country calling code\".\r\n\t\t\t// In an example with `toll_free` numbers above, `\"US\"` would be returned even though\r\n\t\t\t// it could as well be `\"CA\"`.\r\n\t\t\t//\r\n\t\t\t// There was also a time when this attempted to be overly smart\r\n\t\t\t// and kept track of all such multiple matching countries\r\n\t\t\t// and then picked the one that matched the `defaultCountry`, if provided.\r\n\t\t\t// For example, with `toll_free` numbers above, and with `defaultCountry: \"CA\"`,\r\n\t\t\t// it would've returned `\"CA\"` instead of `\"US\"`.\r\n\t\t\t// Later it turned out that such \"overly smart\" behavior turned out to be just confusing,\r\n\t\t\t// so this \"overly smart\" country detection was reverted to returning the \"main\" country\r\n\t\t\t// for the \"country calling code\".\r\n\t\t\t// https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/154\r\n\t\t\t//\r\n\t\t\treturn country\r\n\t\t\t//\r\n\t\t\t// The \"overly smart\" behavior code:\r\n\t\t\t//\r\n\t\t\t// if (defaultCountry) {\r\n\t\t\t// \tif (country === defaultCountry) {\r\n\t\t\t// \t\treturn country\r\n\t\t\t// \t} else {\r\n\t\t\t// \t\tmatchingCountries.push(country)\r\n\t\t\t// \t}\r\n\t\t\t// } else {\r\n\t\t\t// \treturn country\r\n\t\t\t// }\r\n\t\t}\r\n\t}\r\n\r\n\t// // Return the first (\"main\") one of the `matchingCountries`.\r\n\t// if (matchingCountries.length > 0) {\r\n\t// \treturn matchingCountries[0]\r\n\t// }\r\n}"],"mappings":"AAAA,OAAOA,QAAQ,MAAM,gBAAgB;AACrC,OAAOC,aAAa,MAAM,oBAAoB;;AAE9C;AACA;AACA,eAAe,SAASC,0BAA0BA,CAACC,mBAAmB,EAAE;EACvEC,SAAS;EACTC;AACD,CAAC,EAAE;EACF;EACAA,QAAQ,GAAG,IAAIL,QAAQ,CAACK,QAAQ,CAAC;;EAEjC;;EAEA,KAAK,MAAMC,OAAO,IAAIF,SAAS,EAAE;IAChCC,QAAQ,CAACE,mBAAmB,CAACD,OAAO,CAAC;IACrC;IACA;IACA;IACA;IACA;IACA;IACA;IACA,IAAID,QAAQ,CAACG,aAAa,CAAC,CAAC,EAAE;MAC7B,IAAIL,mBAAmB,IACtBA,mBAAmB,CAACM,MAAM,CAACJ,QAAQ,CAACG,aAAa,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE;QAC5D,OAAOF,OAAO;MACf;IACD;IACA;IACA;IAAA,KACK,IAAIL,aAAa,CAAC;MAAES,KAAK,EAAEP,mBAAmB;MAAEG;IAAQ,CAAC,EAAEK,SAAS,EAAEN,QAAQ,CAACA,QAAQ,CAAC,EAAE;MAC9F;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA,OAAOC,OAAO;MACd;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA;MACA;IACD;EACD;;EAEA;EACA;EACA;EACA;AACD","ignoreList":[]}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import Metadata from '../metadata.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Pattern that makes it easy to distinguish whether a region has a single
|
|
5
|
+
* international dialing prefix or not. If a region has a single international
|
|
6
|
+
* prefix (e.g. 011 in USA), it will be represented as a string that contains
|
|
7
|
+
* a sequence of ASCII digits, and possibly a tilde, which signals waiting for
|
|
8
|
+
* the tone. If there are multiple available international prefixes in a
|
|
9
|
+
* region, they will be represented as a regex string that always contains one
|
|
10
|
+
* or more characters that are not ASCII digits or a tilde.
|
|
11
|
+
*/
|
|
12
|
+
const SINGLE_IDD_PREFIX_REG_EXP = /^[\d]+(?:[~\u2053\u223C\uFF5E][\d]+)?$/;
|
|
13
|
+
|
|
14
|
+
// If the `country` supports IDD calling, it returns a preferred IDD prefix.
|
|
15
|
+
// If the `country` doesn't support IDD calling, it returns `undefined`.
|
|
16
|
+
export default function getIddPrefix(country, callingCode, metadata) {
|
|
17
|
+
const countryMetadata = new Metadata(metadata);
|
|
18
|
+
countryMetadata.selectNumberingPlan(country, callingCode);
|
|
19
|
+
if (countryMetadata.defaultIDDPrefix()) {
|
|
20
|
+
return countryMetadata.defaultIDDPrefix();
|
|
21
|
+
}
|
|
22
|
+
if (SINGLE_IDD_PREFIX_REG_EXP.test(countryMetadata.IDDPrefix())) {
|
|
23
|
+
return countryMetadata.IDDPrefix();
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=getIddPrefix.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getIddPrefix.js","names":["Metadata","SINGLE_IDD_PREFIX_REG_EXP","getIddPrefix","country","callingCode","metadata","countryMetadata","selectNumberingPlan","defaultIDDPrefix","test","IDDPrefix"],"sources":["../../source/helpers/getIddPrefix.js"],"sourcesContent":["import Metadata from '../metadata.js'\r\n\r\n/**\r\n * Pattern that makes it easy to distinguish whether a region has a single\r\n * international dialing prefix or not. If a region has a single international\r\n * prefix (e.g. 011 in USA), it will be represented as a string that contains\r\n * a sequence of ASCII digits, and possibly a tilde, which signals waiting for\r\n * the tone. If there are multiple available international prefixes in a\r\n * region, they will be represented as a regex string that always contains one\r\n * or more characters that are not ASCII digits or a tilde.\r\n */\r\nconst SINGLE_IDD_PREFIX_REG_EXP = /^[\\d]+(?:[~\\u2053\\u223C\\uFF5E][\\d]+)?$/\r\n\r\n// If the `country` supports IDD calling, it returns a preferred IDD prefix.\r\n// If the `country` doesn't support IDD calling, it returns `undefined`.\r\nexport default function getIddPrefix(country, callingCode, metadata) {\r\n\tconst countryMetadata = new Metadata(metadata)\r\n\tcountryMetadata.selectNumberingPlan(country, callingCode)\r\n\tif (countryMetadata.defaultIDDPrefix()) {\r\n\t\treturn countryMetadata.defaultIDDPrefix()\r\n\t}\r\n\tif (SINGLE_IDD_PREFIX_REG_EXP.test(countryMetadata.IDDPrefix())) {\r\n\t\treturn countryMetadata.IDDPrefix()\r\n\t}\r\n}\r\n"],"mappings":"AAAA,OAAOA,QAAQ,MAAM,gBAAgB;;AAErC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,yBAAyB,GAAG,wCAAwC;;AAE1E;AACA;AACA,eAAe,SAASC,YAAYA,CAACC,OAAO,EAAEC,WAAW,EAAEC,QAAQ,EAAE;EACpE,MAAMC,eAAe,GAAG,IAAIN,QAAQ,CAACK,QAAQ,CAAC;EAC9CC,eAAe,CAACC,mBAAmB,CAACJ,OAAO,EAAEC,WAAW,CAAC;EACzD,IAAIE,eAAe,CAACE,gBAAgB,CAAC,CAAC,EAAE;IACvC,OAAOF,eAAe,CAACE,gBAAgB,CAAC,CAAC;EAC1C;EACA,IAAIP,yBAAyB,CAACQ,IAAI,CAACH,eAAe,CAACI,SAAS,CAAC,CAAC,CAAC,EAAE;IAChE,OAAOJ,eAAe,CAACI,SAAS,CAAC,CAAC;EACnC;AACD","ignoreList":[]}
|