libphonenumber-js 1.9.2 → 1.9.6
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 +23 -0
- package/README.md +34 -9
- package/build/AsYouType.js +16 -21
- package/build/AsYouType.js.map +1 -1
- package/build/AsYouType.test.js +12 -3
- package/build/AsYouType.test.js.map +1 -1
- package/build/AsYouTypeFormatter.complete.js +4 -4
- package/build/AsYouTypeFormatter.complete.js.map +1 -1
- package/build/AsYouTypeFormatter.js +25 -15
- package/build/AsYouTypeFormatter.js.map +1 -1
- package/build/AsYouTypeParser.js +70 -37
- package/build/AsYouTypeParser.js.map +1 -1
- package/build/AsYouTypeState.js +1 -0
- package/build/AsYouTypeState.js.map +1 -1
- package/build/PhoneNumber.js +2 -2
- package/build/PhoneNumber.js.map +1 -1
- package/build/PhoneNumberMatcher.js +20 -16
- package/build/PhoneNumberMatcher.js.map +1 -1
- package/build/findNumbers/Leniency.js +1 -1
- package/build/findNumbers/Leniency.js.map +1 -1
- package/build/findNumbers.test.js +31 -22
- package/build/findNumbers.test.js.map +1 -1
- package/build/findPhoneNumbers_.js +21 -4
- package/build/findPhoneNumbers_.js.map +1 -1
- package/build/format.test.js +0 -6
- package/build/format.test.js.map +1 -1
- package/build/formatNumberForMobileDialing.js +3 -3
- package/build/formatNumberForMobileDialing.js.map +1 -1
- package/build/format_.js +15 -84
- package/build/format_.js.map +1 -1
- package/build/getNumberType.js +3 -3
- package/build/getNumberType.js.map +1 -1
- package/build/{RFC3966.js → helpers/RFC3966.js} +0 -0
- package/build/helpers/RFC3966.js.map +1 -0
- package/build/{RFC3966.test.js → helpers/RFC3966.test.js} +0 -0
- package/build/helpers/RFC3966.test.js.map +1 -0
- package/build/helpers/applyInternationalSeparatorStyle.js +43 -0
- package/build/helpers/applyInternationalSeparatorStyle.js.map +1 -0
- package/build/helpers/applyInternationalSeparatorStyle.test.js +13 -0
- package/build/helpers/applyInternationalSeparatorStyle.test.js.map +1 -0
- package/build/{checkNumberLength.js → helpers/checkNumberLength.js} +4 -2
- package/build/helpers/checkNumberLength.js.map +1 -0
- package/build/{checkNumberLength.test.js → helpers/checkNumberLength.test.js} +2 -2
- package/build/helpers/checkNumberLength.test.js.map +1 -0
- package/build/{extension.js → helpers/extension/createExtensionPattern.js} +3 -56
- package/build/helpers/extension/createExtensionPattern.js.map +1 -0
- package/build/helpers/extension/extractExtension.js +42 -0
- package/build/helpers/extension/extractExtension.js.map +1 -0
- package/build/helpers/extractCountryCallingCode.js +114 -0
- package/build/helpers/extractCountryCallingCode.js.map +1 -0
- package/build/helpers/extractCountryCallingCode.test.js +22 -0
- package/build/helpers/extractCountryCallingCode.test.js.map +1 -0
- package/build/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +63 -0
- package/build/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map +1 -0
- package/build/helpers/extractNationalNumber.js +110 -0
- package/build/helpers/extractNationalNumber.js.map +1 -0
- package/build/helpers/extractNationalNumberFromPossiblyIncompleteNumber.js +114 -0
- package/build/helpers/extractNationalNumberFromPossiblyIncompleteNumber.js.map +1 -0
- package/build/helpers/extractNationalNumberFromPossiblyIncompleteNumber.test.js +22 -0
- package/build/helpers/extractNationalNumberFromPossiblyIncompleteNumber.test.js.map +1 -0
- package/build/helpers/formatNationalNumberUsingFormat.js +46 -0
- package/build/helpers/formatNationalNumberUsingFormat.js.map +1 -0
- package/build/helpers/getCountryByCallingCode.js +73 -0
- package/build/helpers/getCountryByCallingCode.js.map +1 -0
- package/build/helpers/getIddPrefix.js +34 -0
- package/build/helpers/getIddPrefix.js.map +1 -0
- package/build/{getNumberType_.js → helpers/getNumberType.js} +12 -12
- package/build/helpers/getNumberType.js.map +1 -0
- package/build/{isViablePhoneNumber.js → helpers/isViablePhoneNumber.js} +5 -3
- package/build/helpers/isViablePhoneNumber.js.map +1 -0
- package/build/helpers/matchesEntirely.js +19 -0
- package/build/helpers/matchesEntirely.js.map +1 -0
- package/build/helpers/matchesEntirely.test.js +15 -0
- package/build/helpers/matchesEntirely.test.js.map +1 -0
- package/build/{util.js → helpers/mergeArrays.js} +3 -18
- package/build/helpers/mergeArrays.js.map +1 -0
- package/build/helpers/mergeArrays.test.js +12 -0
- package/build/helpers/mergeArrays.test.js.map +1 -0
- package/build/{parseDigits.js → helpers/parseDigits.js} +0 -0
- package/build/helpers/parseDigits.js.map +1 -0
- package/build/{parseDigits.test.js → helpers/parseDigits.test.js} +0 -0
- package/build/helpers/parseDigits.test.js.map +1 -0
- package/build/helpers/stripIddPrefix.js +46 -0
- package/build/helpers/stripIddPrefix.js.map +1 -0
- package/build/helpers/stripIddPrefix.test.js +23 -0
- package/build/helpers/stripIddPrefix.test.js.map +1 -0
- package/build/isPossibleNumber_.js +1 -1
- package/build/isPossibleNumber_.js.map +1 -1
- package/build/isValidNumberForRegion.js +1 -1
- package/build/isValidNumberForRegion.js.map +1 -1
- package/build/parse.test.js +1 -25
- package/build/parse.test.js.map +1 -1
- package/build/parseIncompletePhoneNumber.js +9 -6
- package/build/parseIncompletePhoneNumber.js.map +1 -1
- package/build/parse_.js +19 -425
- package/build/parse_.js.map +1 -1
- package/build/validate_.js +4 -4
- package/build/validate_.js.map +1 -1
- package/bundle/libphonenumber-js.min.js +1 -1
- package/bundle/libphonenumber-js.min.js.map +1 -1
- package/bundle/libphonenumber-max.js +1 -1
- package/bundle/libphonenumber-max.js.map +1 -1
- package/bundle/libphonenumber-min.js +1 -1
- package/bundle/libphonenumber-min.js.map +1 -1
- package/bundle/libphonenumber-mobile.js +1 -1
- package/bundle/libphonenumber-mobile.js.map +1 -1
- package/core/index.commonjs.js +5 -3
- package/core/index.d.ts +9 -1
- package/core/index.js +6 -2
- package/custom.d.ts +9 -1
- package/custom.js +5 -6
- package/es6/AsYouType.js +17 -22
- package/es6/AsYouType.js.map +1 -1
- package/es6/AsYouType.test.js +12 -3
- package/es6/AsYouType.test.js.map +1 -1
- package/es6/AsYouTypeFormatter.complete.js +3 -3
- package/es6/AsYouTypeFormatter.complete.js.map +1 -1
- package/es6/AsYouTypeFormatter.js +22 -13
- package/es6/AsYouTypeFormatter.js.map +1 -1
- package/es6/AsYouTypeParser.js +65 -32
- package/es6/AsYouTypeParser.js.map +1 -1
- package/es6/AsYouTypeState.js +1 -0
- package/es6/AsYouTypeState.js.map +1 -1
- package/es6/PhoneNumber.js +1 -1
- package/es6/PhoneNumber.js.map +1 -1
- package/es6/PhoneNumberMatcher.js +18 -15
- package/es6/PhoneNumberMatcher.js.map +1 -1
- package/es6/findNumbers/Leniency.js +1 -1
- package/es6/findNumbers/Leniency.js.map +1 -1
- package/es6/findNumbers.test.js +10 -1
- package/es6/findNumbers.test.js.map +1 -1
- package/es6/findPhoneNumbers_.js +19 -2
- package/es6/findPhoneNumbers_.js.map +1 -1
- package/es6/format.test.js +0 -5
- package/es6/format.test.js.map +1 -1
- package/es6/formatNumberForMobileDialing.js +2 -2
- package/es6/formatNumberForMobileDialing.js.map +1 -1
- package/es6/format_.js +11 -76
- package/es6/format_.js.map +1 -1
- package/es6/getNumberType.js +2 -2
- package/es6/getNumberType.js.map +1 -1
- package/es6/{RFC3966.js → helpers/RFC3966.js} +0 -0
- package/es6/helpers/RFC3966.js.map +1 -0
- package/es6/{RFC3966.test.js → helpers/RFC3966.test.js} +0 -0
- package/es6/helpers/RFC3966.test.js.map +1 -0
- package/es6/helpers/applyInternationalSeparatorStyle.js +35 -0
- package/es6/helpers/applyInternationalSeparatorStyle.js.map +1 -0
- package/es6/helpers/applyInternationalSeparatorStyle.test.js +8 -0
- package/es6/helpers/applyInternationalSeparatorStyle.test.js.map +1 -0
- package/es6/{checkNumberLength.js → helpers/checkNumberLength.js} +1 -1
- package/es6/helpers/checkNumberLength.js.map +1 -0
- package/es6/{checkNumberLength.test.js → helpers/checkNumberLength.test.js} +2 -2
- package/es6/helpers/checkNumberLength.test.js.map +1 -0
- package/es6/{extension.js → helpers/extension/createExtensionPattern.js} +3 -51
- package/es6/helpers/extension/createExtensionPattern.js.map +1 -0
- package/es6/helpers/extension/extractExtension.js +32 -0
- package/es6/helpers/extension/extractExtension.js.map +1 -0
- package/es6/helpers/extractCountryCallingCode.js +102 -0
- package/es6/helpers/extractCountryCallingCode.js.map +1 -0
- package/es6/helpers/extractCountryCallingCode.test.js +16 -0
- package/es6/helpers/extractCountryCallingCode.test.js.map +1 -0
- package/es6/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +50 -0
- package/es6/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map +1 -0
- package/es6/helpers/extractNationalNumber.js +98 -0
- package/es6/helpers/extractNationalNumber.js.map +1 -0
- package/es6/helpers/extractNationalNumberFromPossiblyIncompleteNumber.js +107 -0
- package/es6/helpers/extractNationalNumberFromPossiblyIncompleteNumber.js.map +1 -0
- package/es6/helpers/extractNationalNumberFromPossiblyIncompleteNumber.test.js +15 -0
- package/es6/helpers/extractNationalNumberFromPossiblyIncompleteNumber.test.js.map +1 -0
- package/es6/helpers/formatNationalNumberUsingFormat.js +33 -0
- package/es6/helpers/formatNationalNumberUsingFormat.js.map +1 -0
- package/es6/helpers/getCountryByCallingCode.js +61 -0
- package/es6/helpers/getCountryByCallingCode.js.map +1 -0
- package/es6/helpers/getIddPrefix.js +25 -0
- package/es6/helpers/getIddPrefix.js.map +1 -0
- package/es6/{getNumberType_.js → helpers/getNumberType.js} +9 -9
- package/es6/helpers/getNumberType.js.map +1 -0
- package/es6/{isViablePhoneNumber.js → helpers/isViablePhoneNumber.js} +2 -2
- package/es6/helpers/isViablePhoneNumber.js.map +1 -0
- package/es6/helpers/matchesEntirely.js +12 -0
- package/es6/helpers/matchesEntirely.js.map +1 -0
- package/es6/helpers/matchesEntirely.test.js +10 -0
- package/es6/helpers/matchesEntirely.test.js.map +1 -0
- package/es6/{util.js → helpers/mergeArrays.js} +3 -16
- package/es6/helpers/mergeArrays.js.map +1 -0
- package/es6/helpers/mergeArrays.test.js +7 -0
- package/es6/helpers/mergeArrays.test.js.map +1 -0
- package/es6/{parseDigits.js → helpers/parseDigits.js} +0 -0
- package/es6/helpers/parseDigits.js.map +1 -0
- package/es6/{parseDigits.test.js → helpers/parseDigits.test.js} +0 -0
- package/es6/helpers/parseDigits.test.js.map +1 -0
- package/es6/helpers/stripIddPrefix.js +34 -0
- package/es6/helpers/stripIddPrefix.js.map +1 -0
- package/es6/helpers/stripIddPrefix.test.js +17 -0
- package/es6/helpers/stripIddPrefix.test.js.map +1 -0
- package/es6/isPossibleNumber_.js +1 -1
- package/es6/isPossibleNumber_.js.map +1 -1
- package/es6/isValidNumberForRegion.js +1 -1
- package/es6/isValidNumberForRegion.js.map +1 -1
- package/es6/parse.test.js +1 -24
- package/es6/parse.test.js.map +1 -1
- package/es6/parseIncompletePhoneNumber.js +9 -6
- package/es6/parseIncompletePhoneNumber.js.map +1 -1
- package/es6/parse_.js +12 -411
- package/es6/parse_.js.map +1 -1
- package/es6/validate_.js +2 -2
- package/es6/validate_.js.map +1 -1
- package/index.common.js +71 -183
- package/index.d.ts +9 -5
- package/index.es6.exports/PhoneNumberSearch.js +11 -0
- package/index.es6.exports/findPhoneNumbers.js +7 -0
- package/index.es6.exports/format.js +7 -0
- package/index.es6.exports/getNumberType.js +7 -0
- package/index.es6.exports/isPossibleNumber.js +7 -0
- package/index.es6.exports/isValidNumber.js +7 -0
- package/index.es6.exports/isValidNumberForRegion.js +7 -0
- package/index.es6.exports/parse.js +7 -0
- package/index.es6.exports/searchPhoneNumbers.js +7 -0
- package/index.es6.js +46 -254
- package/max/exports/AsYouType.js +9 -0
- package/max/exports/Metadata.js +9 -0
- package/max/exports/PhoneNumberMatcher.js +8 -0
- package/max/exports/findNumbers.js +6 -0
- package/max/exports/findPhoneNumbersInText.js +6 -0
- package/max/exports/formatIncompletePhoneNumber.js +6 -0
- package/max/exports/getCountries.js +6 -0
- package/max/exports/getCountryCallingCode.js +6 -0
- package/max/exports/getExampleNumber.js +6 -0
- package/max/exports/getExtPrefix.js +6 -0
- package/max/exports/isSupportedCountry.js +6 -0
- package/max/exports/parsePhoneNumberFromString.js +6 -0
- package/max/exports/parsePhoneNumberWithError.js +6 -0
- package/max/exports/searchNumbers.js +6 -0
- package/max/exports/searchPhoneNumbersInText.js +6 -0
- package/max/index.commonjs.js +1 -0
- package/max/index.js +23 -103
- package/max/metadata.js +11 -0
- package/metadata.full.json +1 -1
- package/metadata.full.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/exports/AsYouType.js +9 -0
- package/min/exports/Metadata.js +9 -0
- package/min/exports/PhoneNumberMatcher.js +8 -0
- package/min/exports/findNumbers.js +6 -0
- package/min/exports/findPhoneNumbersInText.js +6 -0
- package/min/exports/formatIncompletePhoneNumber.js +6 -0
- package/min/exports/getCountries.js +6 -0
- package/min/exports/getCountryCallingCode.js +6 -0
- package/min/exports/getExampleNumber.js +6 -0
- package/min/exports/getExtPrefix.js +6 -0
- package/min/exports/isSupportedCountry.js +6 -0
- package/min/exports/parsePhoneNumberFromString.js +6 -0
- package/min/exports/parsePhoneNumberWithError.js +6 -0
- package/min/exports/searchNumbers.js +6 -0
- package/min/exports/searchPhoneNumbersInText.js +6 -0
- package/min/index.commonjs.js +7 -0
- package/min/index.d.ts +11 -1
- package/min/index.js +23 -103
- package/min/metadata.js +11 -0
- package/mobile/exports/AsYouType.js +9 -0
- package/mobile/exports/Metadata.js +9 -0
- package/mobile/exports/PhoneNumberMatcher.js +8 -0
- package/mobile/exports/findNumbers.js +6 -0
- package/mobile/exports/findPhoneNumbersInText.js +6 -0
- package/mobile/exports/formatIncompletePhoneNumber.js +6 -0
- package/mobile/exports/getCountries.js +6 -0
- package/mobile/exports/getCountryCallingCode.js +6 -0
- package/mobile/exports/getExampleNumber.js +6 -0
- package/mobile/exports/getExtPrefix.js +6 -0
- package/mobile/exports/isSupportedCountry.js +6 -0
- package/mobile/exports/parsePhoneNumberFromString.js +6 -0
- package/mobile/exports/parsePhoneNumberWithError.js +6 -0
- package/mobile/exports/searchNumbers.js +6 -0
- package/mobile/exports/searchPhoneNumbersInText.js +6 -0
- package/mobile/index.commonjs.js +1 -0
- package/mobile/index.js +23 -103
- package/mobile/metadata.js +11 -0
- package/package.json +3 -2
- package/runnable/generate-country-codes.js +13 -0
- package/source/AsYouType.js +13 -23
- package/source/AsYouType.test.js +13 -3
- package/source/AsYouTypeFormatter.complete.js +3 -3
- package/source/AsYouTypeFormatter.js +21 -28
- package/source/AsYouTypeParser.js +65 -48
- package/source/AsYouTypeState.js +1 -0
- package/source/PhoneNumber.js +1 -1
- package/source/PhoneNumberMatcher.js +28 -35
- package/source/findNumbers/Leniency.js +1 -1
- package/source/findNumbers.test.js +14 -1
- package/source/findPhoneNumbers_.js +19 -2
- package/source/format.test.js +0 -6
- package/source/formatNumberForMobileDialing.js +2 -2
- package/source/format_.js +7 -86
- package/source/getNumberType.js +2 -2
- package/source/{RFC3966.js → helpers/RFC3966.js} +7 -16
- package/source/{RFC3966.test.js → helpers/RFC3966.test.js} +0 -0
- package/source/helpers/applyInternationalSeparatorStyle.js +35 -0
- package/source/helpers/applyInternationalSeparatorStyle.test.js +8 -0
- package/source/{checkNumberLength.js → helpers/checkNumberLength.js} +1 -1
- package/source/{checkNumberLength.test.js → helpers/checkNumberLength.test.js} +2 -2
- package/source/{extension.js → helpers/extension/createExtensionPattern.js} +2 -49
- package/source/helpers/extension/extractExtension.js +29 -0
- package/source/helpers/extractCountryCallingCode.js +106 -0
- package/source/helpers/extractCountryCallingCode.test.js +18 -0
- package/source/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +63 -0
- package/source/helpers/extractNationalNumber.js +92 -0
- package/source/helpers/extractNationalNumberFromPossiblyIncompleteNumber.js +104 -0
- package/source/helpers/extractNationalNumberFromPossiblyIncompleteNumber.test.js +15 -0
- package/source/helpers/formatNationalNumberUsingFormat.js +46 -0
- package/source/helpers/getCountryByCallingCode.js +44 -0
- package/source/helpers/getIddPrefix.js +23 -0
- package/source/{getNumberType_.js → helpers/getNumberType.js} +14 -28
- package/source/{isViablePhoneNumber.js → helpers/isViablePhoneNumber.js} +3 -4
- package/source/helpers/matchesEntirely.js +11 -0
- package/source/helpers/matchesEntirely.test.js +11 -0
- package/source/helpers/mergeArrays.js +24 -0
- package/source/helpers/mergeArrays.test.js +7 -0
- package/source/{parseDigits.js → helpers/parseDigits.js} +2 -4
- package/source/{parseDigits.test.js → helpers/parseDigits.test.js} +0 -0
- package/source/helpers/stripIddPrefix.js +30 -0
- package/source/helpers/stripIddPrefix.test.js +21 -0
- package/source/isPossibleNumber_.js +1 -1
- package/source/isValidNumberForRegion.js +1 -1
- package/source/parse.test.js +1 -28
- package/source/parseIncompletePhoneNumber.js +9 -6
- package/source/parse_.js +12 -405
- package/source/validate_.js +2 -2
- package/types.d.ts +16 -1
- package/build/IDD.js +0 -70
- package/build/IDD.js.map +0 -1
- package/build/IDD.test.js +0 -23
- package/build/IDD.test.js.map +0 -1
- package/build/RFC3966.js.map +0 -1
- package/build/RFC3966.test.js.map +0 -1
- package/build/checkNumberLength.js.map +0 -1
- package/build/checkNumberLength.test.js.map +0 -1
- package/build/extension.js.map +0 -1
- package/build/getNumberType_.js.map +0 -1
- package/build/isViablePhoneNumber.js.map +0 -1
- package/build/parseDigits.js.map +0 -1
- package/build/parseDigits.test.js.map +0 -1
- package/build/util.js.map +0 -1
- package/build/util.test.js +0 -16
- package/build/util.test.js.map +0 -1
- package/es6/IDD.js +0 -57
- package/es6/IDD.js.map +0 -1
- package/es6/IDD.test.js +0 -17
- package/es6/IDD.test.js.map +0 -1
- package/es6/RFC3966.js.map +0 -1
- package/es6/RFC3966.test.js.map +0 -1
- package/es6/checkNumberLength.js.map +0 -1
- package/es6/checkNumberLength.test.js.map +0 -1
- package/es6/extension.js.map +0 -1
- package/es6/getNumberType_.js.map +0 -1
- package/es6/isViablePhoneNumber.js.map +0 -1
- package/es6/parseDigits.js.map +0 -1
- package/es6/parseDigits.test.js.map +0 -1
- package/es6/util.js.map +0 -1
- package/es6/util.test.js +0 -13
- package/es6/util.test.js.map +0 -1
- package/source/IDD.js +0 -52
- package/source/IDD.test.js +0 -21
- package/source/util.js +0 -37
- package/source/util.test.js +0 -15
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = extractCountryCallingCode;
|
|
7
|
+
|
|
8
|
+
var _stripIddPrefix = _interopRequireDefault(require("./stripIddPrefix"));
|
|
9
|
+
|
|
10
|
+
var _extractCountryCallingCodeFromInternationalNumberWithoutPlusSign = _interopRequireDefault(require("./extractCountryCallingCodeFromInternationalNumberWithoutPlusSign"));
|
|
11
|
+
|
|
12
|
+
var _metadata = _interopRequireDefault(require("../metadata"));
|
|
13
|
+
|
|
14
|
+
var _constants = require("../constants");
|
|
15
|
+
|
|
16
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Converts a phone number digits (possibly with a `+`)
|
|
20
|
+
* into a calling code and the rest phone number digits.
|
|
21
|
+
* The "rest phone number digits" could include
|
|
22
|
+
* a national prefix, carrier code, and national
|
|
23
|
+
* (significant) number.
|
|
24
|
+
* @param {string} number — Phone number digits (possibly with a `+`).
|
|
25
|
+
* @param {string} [country] — Default country.
|
|
26
|
+
* @param {string} [callingCode] — Default calling code (some phone numbering plans are non-geographic).
|
|
27
|
+
* @param {object} metadata
|
|
28
|
+
* @return {object} `{ countryCallingCode: string?, number: string }`
|
|
29
|
+
* @example
|
|
30
|
+
* // Returns `{ countryCallingCode: "1", number: "2133734253" }`.
|
|
31
|
+
* extractCountryCallingCode('2133734253', 'US', null, metadata)
|
|
32
|
+
* extractCountryCallingCode('2133734253', null, '1', metadata)
|
|
33
|
+
* extractCountryCallingCode('+12133734253', null, null, metadata)
|
|
34
|
+
* extractCountryCallingCode('+12133734253', 'RU', null, metadata)
|
|
35
|
+
*/
|
|
36
|
+
function extractCountryCallingCode(number, country, callingCode, metadata) {
|
|
37
|
+
if (!number) {
|
|
38
|
+
return {};
|
|
39
|
+
} // If this is not an international phone number,
|
|
40
|
+
// then either extract an "IDD" prefix, or extract a
|
|
41
|
+
// country calling code from a number by autocorrecting it
|
|
42
|
+
// by prepending a leading `+` in cases when it starts
|
|
43
|
+
// with the country calling code.
|
|
44
|
+
// https://wikitravel.org/en/International_dialling_prefix
|
|
45
|
+
// https://github.com/catamphetamine/libphonenumber-js/issues/376
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
if (number[0] !== '+') {
|
|
49
|
+
// Convert an "out-of-country" dialing phone number
|
|
50
|
+
// to a proper international phone number.
|
|
51
|
+
var numberWithoutIDD = (0, _stripIddPrefix["default"])(number, country, callingCode, metadata); // If an IDD prefix was stripped then
|
|
52
|
+
// convert the number to international one
|
|
53
|
+
// for subsequent parsing.
|
|
54
|
+
|
|
55
|
+
if (numberWithoutIDD && numberWithoutIDD !== number) {
|
|
56
|
+
number = '+' + numberWithoutIDD;
|
|
57
|
+
} else {
|
|
58
|
+
// Check to see if the number starts with the country calling code
|
|
59
|
+
// for the default country. If so, we remove the country calling code,
|
|
60
|
+
// and do some checks on the validity of the number before and after.
|
|
61
|
+
// https://github.com/catamphetamine/libphonenumber-js/issues/376
|
|
62
|
+
if (country || callingCode) {
|
|
63
|
+
var _extractCountryCallin = (0, _extractCountryCallingCodeFromInternationalNumberWithoutPlusSign["default"])(number, country, callingCode, metadata),
|
|
64
|
+
countryCallingCode = _extractCountryCallin.countryCallingCode,
|
|
65
|
+
shorterNumber = _extractCountryCallin.number;
|
|
66
|
+
|
|
67
|
+
if (countryCallingCode) {
|
|
68
|
+
return {
|
|
69
|
+
countryCallingCode: countryCallingCode,
|
|
70
|
+
number: shorterNumber
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
number: number
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
} // Fast abortion: country codes do not begin with a '0'
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
if (number[1] === '0') {
|
|
83
|
+
return {};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
metadata = new _metadata["default"](metadata); // The thing with country phone codes
|
|
87
|
+
// is that they are orthogonal to each other
|
|
88
|
+
// i.e. there's no such country phone code A
|
|
89
|
+
// for which country phone code B exists
|
|
90
|
+
// where B starts with A.
|
|
91
|
+
// Therefore, while scanning digits,
|
|
92
|
+
// if a valid country code is found,
|
|
93
|
+
// that means that it is the country code.
|
|
94
|
+
//
|
|
95
|
+
|
|
96
|
+
var i = 2;
|
|
97
|
+
|
|
98
|
+
while (i - 1 <= _constants.MAX_LENGTH_COUNTRY_CODE && i <= number.length) {
|
|
99
|
+
var _countryCallingCode = number.slice(1, i);
|
|
100
|
+
|
|
101
|
+
if (metadata.hasCallingCode(_countryCallingCode)) {
|
|
102
|
+
metadata.selectNumberingPlan(_countryCallingCode);
|
|
103
|
+
return {
|
|
104
|
+
countryCallingCode: _countryCallingCode,
|
|
105
|
+
number: number.slice(i)
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
i++;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return {};
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=extractCountryCallingCode.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../source/helpers/extractCountryCallingCode.js"],"names":["extractCountryCallingCode","number","country","callingCode","metadata","numberWithoutIDD","countryCallingCode","shorterNumber","Metadata","i","MAX_LENGTH_COUNTRY_CODE","length","slice","hasCallingCode","selectNumberingPlan"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AACA;;;;AAEA;;;;;;;;;;;;;;;;;;AAkBe,SAASA,yBAAT,CACdC,MADc,EAEdC,OAFc,EAGdC,WAHc,EAIdC,QAJc,EAKb;AACD,MAAI,CAACH,MAAL,EAAa;AACZ,WAAO,EAAP;AACA,GAHA,CAKD;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,MAAIA,MAAM,CAAC,CAAD,CAAN,KAAc,GAAlB,EAAuB;AACtB;AACA;AACA,QAAMI,gBAAgB,GAAG,gCAAeJ,MAAf,EAAuBC,OAAvB,EAAgCC,WAAhC,EAA6CC,QAA7C,CAAzB,CAHsB,CAItB;AACA;AACA;;AACA,QAAIC,gBAAgB,IAAIA,gBAAgB,KAAKJ,MAA7C,EAAqD;AACpDA,MAAAA,MAAM,GAAG,MAAMI,gBAAf;AACA,KAFD,MAEO;AACN;AACA;AACA;AACA;AACA,UAAIH,OAAO,IAAIC,WAAf,EAA4B;AAAA,oCAIvB,iFACHF,MADG,EAEHC,OAFG,EAGHC,WAHG,EAIHC,QAJG,CAJuB;AAAA,YAE1BE,kBAF0B,yBAE1BA,kBAF0B;AAAA,YAGlBC,aAHkB,yBAG1BN,MAH0B;;AAU3B,YAAIK,kBAAJ,EAAwB;AACvB,iBAAO;AACNA,YAAAA,kBAAkB,EAAlBA,kBADM;AAENL,YAAAA,MAAM,EAAEM;AAFF,WAAP;AAIA;AACD;;AACD,aAAO;AAAEN,QAAAA,MAAM,EAANA;AAAF,OAAP;AACA;AACD,GA7CA,CA+CD;;;AACA,MAAIA,MAAM,CAAC,CAAD,CAAN,KAAc,GAAlB,EAAuB;AACtB,WAAO,EAAP;AACA;;AAEDG,EAAAA,QAAQ,GAAG,IAAII,oBAAJ,CAAaJ,QAAb,CAAX,CApDC,CAsDD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,MAAIK,CAAC,GAAG,CAAR;;AACA,SAAOA,CAAC,GAAG,CAAJ,IAASC,kCAAT,IAAoCD,CAAC,IAAIR,MAAM,CAACU,MAAvD,EAA+D;AAC9D,QAAML,mBAAkB,GAAGL,MAAM,CAACW,KAAP,CAAa,CAAb,EAAgBH,CAAhB,CAA3B;;AACA,QAAIL,QAAQ,CAACS,cAAT,CAAwBP,mBAAxB,CAAJ,EAAiD;AAChDF,MAAAA,QAAQ,CAACU,mBAAT,CAA6BR,mBAA7B;AACA,aAAO;AACNA,QAAAA,kBAAkB,EAAlBA,mBADM;AAENL,QAAAA,MAAM,EAAEA,MAAM,CAACW,KAAP,CAAaH,CAAb;AAFF,OAAP;AAIA;;AACDA,IAAAA,CAAC;AACD;;AAED,SAAO,EAAP;AACA","sourcesContent":["import stripIddPrefix from './stripIddPrefix'\r\nimport extractCountryCallingCodeFromInternationalNumberWithoutPlusSign from './extractCountryCallingCodeFromInternationalNumberWithoutPlusSign'\r\nimport Metadata from '../metadata'\r\nimport { MAX_LENGTH_COUNTRY_CODE } from '../constants'\r\n\r\n/**\r\n * Converts a phone number digits (possibly with a `+`)\r\n * into a calling code and the rest phone number digits.\r\n * The \"rest phone number digits\" could include\r\n * a national prefix, carrier code, and national\r\n * (significant) number.\r\n * @param {string} number — Phone number digits (possibly with a `+`).\r\n * @param {string} [country] — Default country.\r\n * @param {string} [callingCode] — Default calling code (some phone numbering plans are non-geographic).\r\n * @param {object} metadata\r\n * @return {object} `{ countryCallingCode: string?, number: string }`\r\n * @example\r\n * // Returns `{ countryCallingCode: \"1\", number: \"2133734253\" }`.\r\n * extractCountryCallingCode('2133734253', 'US', null, metadata)\r\n * extractCountryCallingCode('2133734253', null, '1', metadata)\r\n * extractCountryCallingCode('+12133734253', null, null, metadata)\r\n * extractCountryCallingCode('+12133734253', 'RU', null, metadata)\r\n */\r\nexport default function extractCountryCallingCode(\r\n\tnumber,\r\n\tcountry,\r\n\tcallingCode,\r\n\tmetadata\r\n) {\r\n\tif (!number) {\r\n\t\treturn {}\r\n\t}\r\n\r\n\t// If this is not an international phone number,\r\n\t// then either extract an \"IDD\" prefix, or extract a\r\n\t// country calling code from a number by autocorrecting it\r\n\t// by prepending a leading `+` in cases when it starts\r\n\t// with the country calling code.\r\n\t// https://wikitravel.org/en/International_dialling_prefix\r\n\t// https://github.com/catamphetamine/libphonenumber-js/issues/376\r\n\tif (number[0] !== '+') {\r\n\t\t// Convert an \"out-of-country\" dialing phone number\r\n\t\t// to a proper international phone number.\r\n\t\tconst numberWithoutIDD = stripIddPrefix(number, country, callingCode, metadata)\r\n\t\t// If an IDD prefix was stripped then\r\n\t\t// convert the number to international one\r\n\t\t// for subsequent parsing.\r\n\t\tif (numberWithoutIDD && numberWithoutIDD !== number) {\r\n\t\t\tnumber = '+' + numberWithoutIDD\r\n\t\t} else {\r\n\t\t\t// Check to see if the number starts with the country calling code\r\n\t\t\t// for the default country. If so, we remove the country calling code,\r\n\t\t\t// and do some checks on the validity of the number before and after.\r\n\t\t\t// https://github.com/catamphetamine/libphonenumber-js/issues/376\r\n\t\t\tif (country || callingCode) {\r\n\t\t\t\tconst {\r\n\t\t\t\t\tcountryCallingCode,\r\n\t\t\t\t\tnumber: shorterNumber\r\n\t\t\t\t} = extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(\r\n\t\t\t\t\tnumber,\r\n\t\t\t\t\tcountry,\r\n\t\t\t\t\tcallingCode,\r\n\t\t\t\t\tmetadata\r\n\t\t\t\t)\r\n\t\t\t\tif (countryCallingCode) {\r\n\t\t\t\t\treturn {\r\n\t\t\t\t\t\tcountryCallingCode,\r\n\t\t\t\t\t\tnumber: shorterNumber\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\treturn { number }\r\n\t\t}\r\n\t}\r\n\r\n\t// Fast abortion: country codes do not begin with a '0'\r\n\tif (number[1] === '0') {\r\n\t\treturn {}\r\n\t}\r\n\r\n\tmetadata = new Metadata(metadata)\r\n\r\n\t// The thing with country phone codes\r\n\t// is that they are orthogonal to each other\r\n\t// i.e. there's no such country phone code A\r\n\t// for which country phone code B exists\r\n\t// where B starts with A.\r\n\t// Therefore, while scanning digits,\r\n\t// if a valid country code is found,\r\n\t// that means that it is the country code.\r\n\t//\r\n\tlet i = 2\r\n\twhile (i - 1 <= MAX_LENGTH_COUNTRY_CODE && i <= number.length) {\r\n\t\tconst countryCallingCode = number.slice(1, i)\r\n\t\tif (metadata.hasCallingCode(countryCallingCode)) {\r\n\t\t\tmetadata.selectNumberingPlan(countryCallingCode)\r\n\t\t\treturn {\r\n\t\t\t\tcountryCallingCode,\r\n\t\t\t\tnumber: number.slice(i)\r\n\t\t\t}\r\n\t\t}\r\n\t\ti++\r\n\t}\r\n\r\n\treturn {}\r\n}"],"file":"extractCountryCallingCode.js"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _extractCountryCallingCode = _interopRequireDefault(require("./extractCountryCallingCode"));
|
|
4
|
+
|
|
5
|
+
var _metadata = _interopRequireDefault(require("../../metadata.min"));
|
|
6
|
+
|
|
7
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
8
|
+
|
|
9
|
+
describe('extractCountryCallingCode', function () {
|
|
10
|
+
it('should extract country calling code from a number', function () {
|
|
11
|
+
(0, _extractCountryCallingCode["default"])('+78005553535', null, null, _metadata["default"]).should.deep.equal({
|
|
12
|
+
countryCallingCode: '7',
|
|
13
|
+
number: '8005553535'
|
|
14
|
+
});
|
|
15
|
+
(0, _extractCountryCallingCode["default"])('+7800', null, null, _metadata["default"]).should.deep.equal({
|
|
16
|
+
countryCallingCode: '7',
|
|
17
|
+
number: '800'
|
|
18
|
+
});
|
|
19
|
+
(0, _extractCountryCallingCode["default"])('', null, null, _metadata["default"]).should.deep.equal({});
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
//# sourceMappingURL=extractCountryCallingCode.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../source/helpers/extractCountryCallingCode.test.js"],"names":["describe","it","metadata","should","deep","equal","countryCallingCode","number"],"mappings":";;AAAA;;AACA;;;;AAEAA,QAAQ,CAAC,2BAAD,EAA8B,YAAM;AAC3CC,EAAAA,EAAE,CAAC,mDAAD,EAAsD,YAAM;AAC7D,+CAA0B,cAA1B,EAA0C,IAA1C,EAAgD,IAAhD,EAAsDC,oBAAtD,EAAgEC,MAAhE,CAAuEC,IAAvE,CAA4EC,KAA5E,CAAkF;AACjFC,MAAAA,kBAAkB,EAAE,GAD6D;AAEjFC,MAAAA,MAAM,EAAE;AAFyE,KAAlF;AAKA,+CAA0B,OAA1B,EAAmC,IAAnC,EAAyC,IAAzC,EAA+CL,oBAA/C,EAAyDC,MAAzD,CAAgEC,IAAhE,CAAqEC,KAArE,CAA2E;AAC1EC,MAAAA,kBAAkB,EAAE,GADsD;AAE1EC,MAAAA,MAAM,EAAE;AAFkE,KAA3E;AAKA,+CAA0B,EAA1B,EAA8B,IAA9B,EAAoC,IAApC,EAA0CL,oBAA1C,EAAoDC,MAApD,CAA2DC,IAA3D,CAAgEC,KAAhE,CAAsE,EAAtE;AACA,GAZC,CAAF;AAaA,CAdO,CAAR","sourcesContent":["import extractCountryCallingCode from './extractCountryCallingCode'\r\nimport metadata from '../../metadata.min'\r\n\r\ndescribe('extractCountryCallingCode', () => {\r\n\tit('should extract country calling code from a number', () => {\r\n\t\textractCountryCallingCode('+78005553535', null, null, metadata).should.deep.equal({\r\n\t\t\tcountryCallingCode: '7',\r\n\t\t\tnumber: '8005553535'\r\n\t\t})\r\n\r\n\t\textractCountryCallingCode('+7800', null, null, metadata).should.deep.equal({\r\n\t\t\tcountryCallingCode: '7',\r\n\t\t\tnumber: '800'\r\n\t\t})\r\n\r\n\t\textractCountryCallingCode('', null, null, metadata).should.deep.equal({})\r\n\t})\r\n})\r\n"],"file":"extractCountryCallingCode.test.js"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = extractCountryCallingCodeFromInternationalNumberWithoutPlusSign;
|
|
7
|
+
|
|
8
|
+
var _metadata = _interopRequireDefault(require("../metadata"));
|
|
9
|
+
|
|
10
|
+
var _matchesEntirely = _interopRequireDefault(require("./matchesEntirely"));
|
|
11
|
+
|
|
12
|
+
var _extractNationalNumber = _interopRequireDefault(require("./extractNationalNumber"));
|
|
13
|
+
|
|
14
|
+
var _checkNumberLength = _interopRequireDefault(require("./checkNumberLength"));
|
|
15
|
+
|
|
16
|
+
var _getCountryCallingCode = _interopRequireDefault(require("../getCountryCallingCode"));
|
|
17
|
+
|
|
18
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Sometimes some people incorrectly input international phone numbers
|
|
22
|
+
* without the leading `+`. This function corrects such input.
|
|
23
|
+
* @param {string} number — Phone number digits.
|
|
24
|
+
* @param {string?} country
|
|
25
|
+
* @param {string?} callingCode
|
|
26
|
+
* @param {object} metadata
|
|
27
|
+
* @return {object} `{ countryCallingCode: string?, number: string }`.
|
|
28
|
+
*/
|
|
29
|
+
function extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(number, country, callingCode, metadata) {
|
|
30
|
+
var countryCallingCode = country ? (0, _getCountryCallingCode["default"])(country, metadata) : callingCode;
|
|
31
|
+
|
|
32
|
+
if (number.indexOf(countryCallingCode) === 0) {
|
|
33
|
+
metadata = new _metadata["default"](metadata);
|
|
34
|
+
metadata.selectNumberingPlan(country, callingCode);
|
|
35
|
+
var possibleShorterNumber = number.slice(countryCallingCode.length);
|
|
36
|
+
|
|
37
|
+
var _extractNationalNumbe = (0, _extractNationalNumber["default"])(possibleShorterNumber, metadata),
|
|
38
|
+
possibleShorterNationalNumber = _extractNationalNumbe.nationalNumber;
|
|
39
|
+
|
|
40
|
+
var _extractNationalNumbe2 = (0, _extractNationalNumber["default"])(number, metadata),
|
|
41
|
+
nationalNumber = _extractNationalNumbe2.nationalNumber; // If the number was not valid before but is valid now,
|
|
42
|
+
// or if it was too long before, we consider the number
|
|
43
|
+
// with the country calling code stripped to be a better result
|
|
44
|
+
// and keep that instead.
|
|
45
|
+
// For example, in Germany (+49), `49` is a valid area code,
|
|
46
|
+
// so if a number starts with `49`, it could be both a valid
|
|
47
|
+
// national German number or an international number without
|
|
48
|
+
// a leading `+`.
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
if (!(0, _matchesEntirely["default"])(nationalNumber, metadata.nationalNumberPattern()) && (0, _matchesEntirely["default"])(possibleShorterNationalNumber, metadata.nationalNumberPattern()) || (0, _checkNumberLength["default"])(nationalNumber, metadata) === 'TOO_LONG') {
|
|
52
|
+
return {
|
|
53
|
+
countryCallingCode: countryCallingCode,
|
|
54
|
+
number: possibleShorterNumber
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
number: number
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../source/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js"],"names":["extractCountryCallingCodeFromInternationalNumberWithoutPlusSign","number","country","callingCode","metadata","countryCallingCode","indexOf","Metadata","selectNumberingPlan","possibleShorterNumber","slice","length","possibleShorterNationalNumber","nationalNumber","nationalNumberPattern"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AACA;;AACA;;;;AAEA;;;;;;;;;AASe,SAASA,+DAAT,CACdC,MADc,EAEdC,OAFc,EAGdC,WAHc,EAIdC,QAJc,EAKb;AACD,MAAMC,kBAAkB,GAAGH,OAAO,GAAG,uCAAsBA,OAAtB,EAA+BE,QAA/B,CAAH,GAA8CD,WAAhF;;AACA,MAAIF,MAAM,CAACK,OAAP,CAAeD,kBAAf,MAAuC,CAA3C,EAA8C;AAC7CD,IAAAA,QAAQ,GAAG,IAAIG,oBAAJ,CAAaH,QAAb,CAAX;AACAA,IAAAA,QAAQ,CAACI,mBAAT,CAA6BN,OAA7B,EAAsCC,WAAtC;AACA,QAAMM,qBAAqB,GAAGR,MAAM,CAACS,KAAP,CAAaL,kBAAkB,CAACM,MAAhC,CAA9B;;AAH6C,gCAMzC,uCACHF,qBADG,EAEHL,QAFG,CANyC;AAAA,QAK5BQ,6BAL4B,yBAK5CC,cAL4C;;AAAA,iCAYzC,uCACHZ,MADG,EAEHG,QAFG,CAZyC;AAAA,QAW5CS,cAX4C,0BAW5CA,cAX4C,EAgB7C;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,QAEE,CAAC,iCAAgBA,cAAhB,EAAgCT,QAAQ,CAACU,qBAAT,EAAhC,CAAD,IAEA,iCAAgBF,6BAAhB,EAA+CR,QAAQ,CAACU,qBAAT,EAA/C,CAHD,IAMA,mCAAkBD,cAAlB,EAAkCT,QAAlC,MAAgD,UAPjD,EAQE;AACD,aAAO;AACNC,QAAAA,kBAAkB,EAAlBA,kBADM;AAENJ,QAAAA,MAAM,EAAEQ;AAFF,OAAP;AAIA;AACD;;AACD,SAAO;AAAER,IAAAA,MAAM,EAANA;AAAF,GAAP;AACA","sourcesContent":["import Metadata from '../metadata'\r\nimport matchesEntirely from './matchesEntirely'\r\nimport extractNationalNumber from './extractNationalNumber'\r\nimport checkNumberLength from './checkNumberLength'\r\nimport getCountryCallingCode from '../getCountryCallingCode'\r\n\r\n/**\r\n * Sometimes some people incorrectly input international phone numbers\r\n * without the leading `+`. This function corrects such input.\r\n * @param {string} number — Phone number digits.\r\n * @param {string?} country\r\n * @param {string?} callingCode\r\n * @param {object} metadata\r\n * @return {object} `{ countryCallingCode: string?, number: string }`.\r\n */\r\nexport default function extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(\r\n\tnumber,\r\n\tcountry,\r\n\tcallingCode,\r\n\tmetadata\r\n) {\r\n\tconst countryCallingCode = country ? getCountryCallingCode(country, metadata) : callingCode\r\n\tif (number.indexOf(countryCallingCode) === 0) {\r\n\t\tmetadata = new Metadata(metadata)\r\n\t\tmetadata.selectNumberingPlan(country, callingCode)\r\n\t\tconst possibleShorterNumber = number.slice(countryCallingCode.length)\r\n\t\tconst {\r\n\t\t\tnationalNumber: possibleShorterNationalNumber,\r\n\t\t} = extractNationalNumber(\r\n\t\t\tpossibleShorterNumber,\r\n\t\t\tmetadata\r\n\t\t)\r\n\t\tconst {\r\n\t\t\tnationalNumber\r\n\t\t} = extractNationalNumber(\r\n\t\t\tnumber,\r\n\t\t\tmetadata\r\n\t\t)\r\n\t\t// If the number was not valid before but is valid now,\r\n\t\t// or if it was too long before, we consider the number\r\n\t\t// with the country calling code stripped to be a better result\r\n\t\t// and keep that instead.\r\n\t\t// For example, in Germany (+49), `49` is a valid area code,\r\n\t\t// so if a number starts with `49`, it could be both a valid\r\n\t\t// national German number or an international number without\r\n\t\t// a leading `+`.\r\n\t\tif (\r\n\t\t\t(\r\n\t\t\t\t!matchesEntirely(nationalNumber, metadata.nationalNumberPattern())\r\n\t\t\t\t&&\r\n\t\t\t\tmatchesEntirely(possibleShorterNationalNumber, metadata.nationalNumberPattern())\r\n\t\t\t)\r\n\t\t\t||\r\n\t\t\tcheckNumberLength(nationalNumber, metadata) === 'TOO_LONG'\r\n\t\t) {\r\n\t\t\treturn {\r\n\t\t\t\tcountryCallingCode,\r\n\t\t\t\tnumber: possibleShorterNumber\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn { number }\r\n}"],"file":"extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = extractNationalNumber;
|
|
7
|
+
|
|
8
|
+
var _extractNationalNumberFromPossiblyIncompleteNumber = _interopRequireDefault(require("./extractNationalNumberFromPossiblyIncompleteNumber"));
|
|
9
|
+
|
|
10
|
+
var _matchesEntirely = _interopRequireDefault(require("./matchesEntirely"));
|
|
11
|
+
|
|
12
|
+
var _checkNumberLength = _interopRequireDefault(require("./checkNumberLength"));
|
|
13
|
+
|
|
14
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Strips national prefix and carrier code from a complete phone number.
|
|
18
|
+
* The difference from the non-"FromCompleteNumber" function is that
|
|
19
|
+
* it won't extract national prefix if the resultant number is too short
|
|
20
|
+
* to be a complete number for the selected phone numbering plan.
|
|
21
|
+
* @param {string} number — Complete phone number digits.
|
|
22
|
+
* @param {Metadata} metadata — Metadata with a phone numbering plan selected.
|
|
23
|
+
* @return {object} `{ nationalNumber: string, carrierCode: string? }`.
|
|
24
|
+
*/
|
|
25
|
+
function extractNationalNumber(number, metadata) {
|
|
26
|
+
// Parsing national prefixes and carrier codes
|
|
27
|
+
// is only required for local phone numbers
|
|
28
|
+
// but some people don't understand that
|
|
29
|
+
// and sometimes write international phone numbers
|
|
30
|
+
// with national prefixes (or maybe even carrier codes).
|
|
31
|
+
// http://ucken.blogspot.ru/2016/03/trunk-prefixes-in-skype4b.html
|
|
32
|
+
// Google's original library forgives such mistakes
|
|
33
|
+
// and so does this library, because it has been requested:
|
|
34
|
+
// https://github.com/catamphetamine/libphonenumber-js/issues/127
|
|
35
|
+
var _extractNationalNumbe = (0, _extractNationalNumberFromPossiblyIncompleteNumber["default"])(number, metadata),
|
|
36
|
+
nationalNumber = _extractNationalNumbe.nationalNumber,
|
|
37
|
+
carrierCode = _extractNationalNumbe.carrierCode;
|
|
38
|
+
|
|
39
|
+
if (!shouldExtractNationalPrefix(number, nationalNumber, metadata)) {
|
|
40
|
+
// Don't strip the national prefix.
|
|
41
|
+
return {
|
|
42
|
+
nationalNumber: number
|
|
43
|
+
};
|
|
44
|
+
} // If a national prefix has been extracted, check to see
|
|
45
|
+
// if the resultant number isn't too short.
|
|
46
|
+
// Same code in Google's `libphonenumber`:
|
|
47
|
+
// https://github.com/google/libphonenumber/blob/e326fa1fc4283bb05eb35cb3c15c18f98a31af33/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L3291-L3302
|
|
48
|
+
// For some reason, they do this check right after the `national_number_pattern` check
|
|
49
|
+
// this library does in `shouldExtractNationalPrefix()` function.
|
|
50
|
+
// Why is there a second "resultant" number validity check?
|
|
51
|
+
// They don't provide an explanation.
|
|
52
|
+
// This library just copies the behavior.
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
if (number.length !== nationalNumber.length + (carrierCode ? carrierCode.length : 0)) {
|
|
56
|
+
// If not using legacy generated metadata (before version `1.0.18`)
|
|
57
|
+
// then it has "possible lengths", so use those to validate the number length.
|
|
58
|
+
if (metadata.possibleLengths()) {
|
|
59
|
+
// "We require that the NSN remaining after stripping the national prefix and
|
|
60
|
+
// carrier code be long enough to be a possible length for the region.
|
|
61
|
+
// Otherwise, we don't do the stripping, since the original number could be
|
|
62
|
+
// a valid short number."
|
|
63
|
+
// https://github.com/google/libphonenumber/blob/876268eb1ad6cdc1b7b5bef17fc5e43052702d57/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L3236-L3250
|
|
64
|
+
switch ((0, _checkNumberLength["default"])(nationalNumber, metadata)) {
|
|
65
|
+
case 'TOO_SHORT':
|
|
66
|
+
case 'INVALID_LENGTH':
|
|
67
|
+
// case 'IS_POSSIBLE_LOCAL_ONLY':
|
|
68
|
+
// Don't strip the national prefix.
|
|
69
|
+
return {
|
|
70
|
+
nationalNumber: number
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
nationalNumber: nationalNumber,
|
|
78
|
+
carrierCode: carrierCode
|
|
79
|
+
};
|
|
80
|
+
} // In some countries, the same digit could be a national prefix
|
|
81
|
+
// or a leading digit of a valid phone number.
|
|
82
|
+
// For example, in Russia, national prefix is `8`,
|
|
83
|
+
// and also `800 555 35 35` is a valid number
|
|
84
|
+
// in which `8` is not a national prefix, but the first digit
|
|
85
|
+
// of a national (significant) number.
|
|
86
|
+
// Same's with Belarus:
|
|
87
|
+
// `82004910060` is a valid national (significant) number,
|
|
88
|
+
// but `2004910060` is not.
|
|
89
|
+
// To support such cases (to prevent the code from always stripping
|
|
90
|
+
// national prefix), a condition is imposed: a national prefix
|
|
91
|
+
// is not extracted when the original number is "viable" and the
|
|
92
|
+
// resultant number is not, a "viable" national number being the one
|
|
93
|
+
// that matches `national_number_pattern`.
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
function shouldExtractNationalPrefix(number, nationalSignificantNumber, metadata) {
|
|
97
|
+
// The equivalent in Google's code is:
|
|
98
|
+
// https://github.com/google/libphonenumber/blob/e326fa1fc4283bb05eb35cb3c15c18f98a31af33/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L2969-L3004
|
|
99
|
+
if ((0, _matchesEntirely["default"])(number, metadata.nationalNumberPattern()) && !(0, _matchesEntirely["default"])(nationalSignificantNumber, metadata.nationalNumberPattern())) {
|
|
100
|
+
return false;
|
|
101
|
+
} // Just "possible" number check would be more relaxed, so it's not used.
|
|
102
|
+
// if (isPossibleNumber(number, metadata) &&
|
|
103
|
+
// !isPossibleNumber(numberWithNationalPrefixExtracted, metadata)) {
|
|
104
|
+
// return false
|
|
105
|
+
// }
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=extractNationalNumber.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../source/helpers/extractNationalNumber.js"],"names":["extractNationalNumber","number","metadata","nationalNumber","carrierCode","shouldExtractNationalPrefix","length","possibleLengths","nationalSignificantNumber","nationalNumberPattern"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;;;AAEA;;;;;;;;;AASe,SAASA,qBAAT,CAA+BC,MAA/B,EAAuCC,QAAvC,EAAiD;AAC/D;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAT+D,8BAa3D,mEACHD,MADG,EAEHC,QAFG,CAb2D;AAAA,MAW9DC,cAX8D,yBAW9DA,cAX8D;AAAA,MAY9DC,WAZ8D,yBAY9DA,WAZ8D;;AAiB/D,MAAI,CAACC,2BAA2B,CAACJ,MAAD,EAASE,cAAT,EAAyBD,QAAzB,CAAhC,EAAoE;AACnE;AACA,WAAO;AAAEC,MAAAA,cAAc,EAAEF;AAAlB,KAAP;AACA,GApB8D,CAqB/D;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,MAAIA,MAAM,CAACK,MAAP,KAAkBH,cAAc,CAACG,MAAf,IAAyBF,WAAW,GAAGA,WAAW,CAACE,MAAf,GAAwB,CAA5D,CAAtB,EAAsF;AACrF;AACA;AACA,QAAIJ,QAAQ,CAACK,eAAT,EAAJ,EAAgC;AAC/B;AACA;AACA;AACA;AACA;AACA,cAAQ,mCAAkBJ,cAAlB,EAAkCD,QAAlC,CAAR;AACC,aAAK,WAAL;AACA,aAAK,gBAAL;AACA;AACC;AACA,iBAAO;AAAEC,YAAAA,cAAc,EAAEF;AAAlB,WAAP;AALF;AAOA;AACD;;AACD,SAAO;AAAEE,IAAAA,cAAc,EAAdA,cAAF;AAAkBC,IAAAA,WAAW,EAAXA;AAAlB,GAAP;AACA,C,CAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASC,2BAAT,CAAqCJ,MAArC,EAA6CO,yBAA7C,EAAwEN,QAAxE,EAAkF;AACjF;AACA;AACA,MAAI,iCAAgBD,MAAhB,EAAwBC,QAAQ,CAACO,qBAAT,EAAxB,KACH,CAAC,iCAAgBD,yBAAhB,EAA2CN,QAAQ,CAACO,qBAAT,EAA3C,CADF,EACgF;AAC/E,WAAO,KAAP;AACA,GANgF,CAOjF;AACA;AACA;AACA;AACA;;;AACA,SAAO,IAAP;AACA","sourcesContent":["import extractNationalNumberFromPossiblyIncompleteNumber from './extractNationalNumberFromPossiblyIncompleteNumber'\r\nimport matchesEntirely from './matchesEntirely'\r\nimport checkNumberLength from './checkNumberLength'\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 {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, 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\tnationalNumber,\r\n\t\tcarrierCode\r\n\t} = extractNationalNumberFromPossiblyIncompleteNumber(\r\n\t\tnumber,\r\n\t\tmetadata\r\n\t)\r\n\tif (!shouldExtractNationalPrefix(number, nationalNumber, metadata)) {\r\n\t\t// Don't strip the national prefix.\r\n\t\treturn { nationalNumber: number }\r\n\t}\r\n\t// If a national prefix has been extracted, check to see\r\n\t// if the resultant number isn't too short.\r\n\t// Same code in Google's `libphonenumber`:\r\n\t// https://github.com/google/libphonenumber/blob/e326fa1fc4283bb05eb35cb3c15c18f98a31af33/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L3291-L3302\r\n\t// For some reason, they do this check right after the `national_number_pattern` check\r\n\t// this library does in `shouldExtractNationalPrefix()` function.\r\n\t// Why is there a second \"resultant\" number validity check?\r\n\t// They don't provide an explanation.\r\n\t// This library just copies the behavior.\r\n\tif (number.length !== nationalNumber.length + (carrierCode ? carrierCode.length : 0)) {\r\n\t\t// If not using legacy generated metadata (before version `1.0.18`)\r\n\t\t// then it has \"possible lengths\", so use those to validate the number length.\r\n\t\tif (metadata.possibleLengths()) {\r\n\t\t\t// \"We require that the NSN remaining after stripping the national prefix and\r\n\t\t\t// carrier code be long enough to be a possible length for the region.\r\n\t\t\t// Otherwise, we don't do the stripping, since the original number could be\r\n\t\t\t// a valid short number.\"\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\tswitch (checkNumberLength(nationalNumber, metadata)) {\r\n\t\t\t\tcase 'TOO_SHORT':\r\n\t\t\t\tcase 'INVALID_LENGTH':\r\n\t\t\t\t// case 'IS_POSSIBLE_LOCAL_ONLY':\r\n\t\t\t\t\t// Don't strip the national prefix.\r\n\t\t\t\t\treturn { nationalNumber: number }\r\n\t\t\t}\r\n\t\t}\r\n\t}\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 shouldExtractNationalPrefix(number, nationalSignificantNumber, 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(number, metadata.nationalNumberPattern()) &&\r\n\t\t!matchesEntirely(nationalSignificantNumber, metadata.nationalNumberPattern())) {\r\n\t\treturn false\r\n\t}\r\n\t// Just \"possible\" number check would be more relaxed, so it's not used.\r\n\t// if (isPossibleNumber(number, metadata) &&\r\n\t// \t!isPossibleNumber(numberWithNationalPrefixExtracted, metadata)) {\r\n\t// \treturn false\r\n\t// }\r\n\treturn true\r\n}"],"file":"extractNationalNumber.js"}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = extractNationalNumberFromPossiblyIncompleteNumber;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Strips any national prefix (such as 0, 1) present in a
|
|
10
|
+
* (possibly incomplete) number provided.
|
|
11
|
+
* "Carrier codes" are only used in Colombia and Brazil,
|
|
12
|
+
* and only when dialing within those countries from a mobile phone to a fixed line number.
|
|
13
|
+
* Sometimes it won't actually strip national prefix
|
|
14
|
+
* and will instead prepend some digits to the `number`:
|
|
15
|
+
* for example, when number `2345678` is passed with `VI` country selected,
|
|
16
|
+
* it will return `{ number: "3402345678" }`, because `340` area code is prepended.
|
|
17
|
+
* @param {string} number — National number digits.
|
|
18
|
+
* @param {object} metadata — Metadata with country selected.
|
|
19
|
+
* @return {object} `{ nationalNumber: string, nationalPrefix: string? carrierCode: string? }`.
|
|
20
|
+
*/
|
|
21
|
+
function extractNationalNumberFromPossiblyIncompleteNumber(number, metadata) {
|
|
22
|
+
if (number && metadata.numberingPlan.nationalPrefixForParsing()) {
|
|
23
|
+
// See METADATA.md for the description of
|
|
24
|
+
// `national_prefix_for_parsing` and `national_prefix_transform_rule`.
|
|
25
|
+
// Attempt to parse the first digits as a national prefix.
|
|
26
|
+
var prefixPattern = new RegExp('^(?:' + metadata.numberingPlan.nationalPrefixForParsing() + ')');
|
|
27
|
+
var prefixMatch = prefixPattern.exec(number);
|
|
28
|
+
|
|
29
|
+
if (prefixMatch) {
|
|
30
|
+
var nationalNumber;
|
|
31
|
+
var carrierCode; // https://gitlab.com/catamphetamine/libphonenumber-js/-/blob/master/METADATA.md#national_prefix_for_parsing--national_prefix_transform_rule
|
|
32
|
+
// If a `national_prefix_for_parsing` has any "capturing groups"
|
|
33
|
+
// then it means that the national (significant) number is equal to
|
|
34
|
+
// those "capturing groups" transformed via `national_prefix_transform_rule`,
|
|
35
|
+
// and nothing could be said about the actual national prefix:
|
|
36
|
+
// what is it and was it even there.
|
|
37
|
+
// If a `national_prefix_for_parsing` doesn't have any "capturing groups",
|
|
38
|
+
// then everything it matches is a national prefix.
|
|
39
|
+
// To determine whether `national_prefix_for_parsing` matched any
|
|
40
|
+
// "capturing groups", the value of the result of calling `.exec()`
|
|
41
|
+
// is looked at, and if it has non-undefined values where there're
|
|
42
|
+
// "capturing groups" in the regular expression, then it means
|
|
43
|
+
// that "capturing groups" have been matched.
|
|
44
|
+
// It's not possible to tell whether there'll be any "capturing gropus"
|
|
45
|
+
// before the matching process, because a `national_prefix_for_parsing`
|
|
46
|
+
// could exhibit both behaviors.
|
|
47
|
+
|
|
48
|
+
var capturedGroupsCount = prefixMatch.length - 1;
|
|
49
|
+
var hasCapturedGroups = capturedGroupsCount > 0 && prefixMatch[capturedGroupsCount];
|
|
50
|
+
|
|
51
|
+
if (metadata.nationalPrefixTransformRule() && hasCapturedGroups) {
|
|
52
|
+
nationalNumber = number.replace(prefixPattern, metadata.nationalPrefixTransformRule()); // If there's more than one captured group,
|
|
53
|
+
// then carrier code is the second one.
|
|
54
|
+
|
|
55
|
+
if (capturedGroupsCount > 1) {
|
|
56
|
+
carrierCode = prefixMatch[1];
|
|
57
|
+
}
|
|
58
|
+
} // If there're no "capturing groups",
|
|
59
|
+
// or if there're "capturing groups" but no
|
|
60
|
+
// `national_prefix_transform_rule`,
|
|
61
|
+
// then just strip the national prefix from the number,
|
|
62
|
+
// and possibly a carrier code.
|
|
63
|
+
// Seems like there could be more.
|
|
64
|
+
else {
|
|
65
|
+
// `prefixBeforeNationalNumber` is the whole substring matched by
|
|
66
|
+
// the `national_prefix_for_parsing` regular expression.
|
|
67
|
+
// There seem to be no guarantees that it's just a national prefix.
|
|
68
|
+
// For example, if there's a carrier code, it's gonna be a
|
|
69
|
+
// part of `prefixBeforeNationalNumber` too.
|
|
70
|
+
var prefixBeforeNationalNumber = prefixMatch[0];
|
|
71
|
+
nationalNumber = number.slice(prefixBeforeNationalNumber.length); // If there's at least one captured group,
|
|
72
|
+
// then carrier code is the first one.
|
|
73
|
+
|
|
74
|
+
if (hasCapturedGroups) {
|
|
75
|
+
carrierCode = prefixMatch[1];
|
|
76
|
+
}
|
|
77
|
+
} // Tries to guess whether a national prefix was present in the input.
|
|
78
|
+
// This is not something copy-pasted from Google's library:
|
|
79
|
+
// they don't seem to have an equivalent for that.
|
|
80
|
+
// So this isn't an "officially approved" way of doing something like that.
|
|
81
|
+
// But since there seems no other existing method, this library uses it.
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
var nationalPrefix;
|
|
85
|
+
|
|
86
|
+
if (hasCapturedGroups) {
|
|
87
|
+
var possiblePositionOfTheFirstCapturedGroup = number.indexOf(prefixMatch[1]);
|
|
88
|
+
var possibleNationalPrefix = number.slice(0, possiblePositionOfTheFirstCapturedGroup); // Example: an Argentinian (AR) phone number `0111523456789`.
|
|
89
|
+
// `prefixMatch[0]` is `01115`, and `$1` is `11`,
|
|
90
|
+
// and the rest of the phone number is `23456789`.
|
|
91
|
+
// The national number is transformed via `9$1` to `91123456789`.
|
|
92
|
+
// National prefix `0` is detected being present at the start.
|
|
93
|
+
// if (possibleNationalPrefix.indexOf(metadata.numberingPlan.nationalPrefix()) === 0) {
|
|
94
|
+
|
|
95
|
+
if (possibleNationalPrefix === metadata.numberingPlan.nationalPrefix()) {
|
|
96
|
+
nationalPrefix = metadata.numberingPlan.nationalPrefix();
|
|
97
|
+
}
|
|
98
|
+
} else {
|
|
99
|
+
nationalPrefix = prefixMatch[0];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
nationalNumber: nationalNumber,
|
|
104
|
+
nationalPrefix: nationalPrefix,
|
|
105
|
+
carrierCode: carrierCode
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
nationalNumber: number
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=extractNationalNumberFromPossiblyIncompleteNumber.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../source/helpers/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"],"mappings":";;;;;;;AAAA;;;;;;;;;;;;;AAae,SAASA,iDAAT,CAA2DC,MAA3D,EAAmEC,QAAnE,EAA6E;AAC3F,MAAID,MAAM,IAAIC,QAAQ,CAACC,aAAT,CAAuBC,wBAAvB,EAAd,EAAiE;AAChE;AACA;AACA;AACA,QAAMC,aAAa,GAAG,IAAIC,MAAJ,CAAW,SAASJ,QAAQ,CAACC,aAAT,CAAuBC,wBAAvB,EAAT,GAA6D,GAAxE,CAAtB;AACA,QAAMG,WAAW,GAAGF,aAAa,CAACG,IAAd,CAAmBP,MAAnB,CAApB;;AACA,QAAIM,WAAJ,EAAiB;AAChB,UAAIE,cAAJ;AACA,UAAIC,WAAJ,CAFgB,CAGhB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,UAAMC,mBAAmB,GAAGJ,WAAW,CAACK,MAAZ,GAAqB,CAAjD;AACA,UAAMC,iBAAiB,GAAGF,mBAAmB,GAAG,CAAtB,IAA2BJ,WAAW,CAACI,mBAAD,CAAhE;;AACA,UAAIT,QAAQ,CAACY,2BAAT,MAA0CD,iBAA9C,EAAiE;AAChEJ,QAAAA,cAAc,GAAGR,MAAM,CAACc,OAAP,CAChBV,aADgB,EAEhBH,QAAQ,CAACY,2BAAT,EAFgB,CAAjB,CADgE,CAKhE;AACA;;AACA,YAAIH,mBAAmB,GAAG,CAA1B,EAA6B;AAC5BD,UAAAA,WAAW,GAAGH,WAAW,CAAC,CAAD,CAAzB;AACA;AACD,OAVD,CAWA;AACA;AACA;AACA;AACA;AACA;AAhBA,WAiBK;AACJ;AACA;AACA;AACA;AACA;AACA,cAAMS,0BAA0B,GAAGT,WAAW,CAAC,CAAD,CAA9C;AACAE,UAAAA,cAAc,GAAGR,MAAM,CAACgB,KAAP,CAAaD,0BAA0B,CAACJ,MAAxC,CAAjB,CAPI,CAQJ;AACA;;AACA,cAAIC,iBAAJ,EAAuB;AACtBH,YAAAA,WAAW,GAAGH,WAAW,CAAC,CAAD,CAAzB;AACA;AACD,SAnDe,CAoDhB;AACA;AACA;AACA;AACA;;;AACA,UAAIW,cAAJ;;AACA,UAAIL,iBAAJ,EAAuB;AACtB,YAAMM,uCAAuC,GAAGlB,MAAM,CAACmB,OAAP,CAAeb,WAAW,CAAC,CAAD,CAA1B,CAAhD;AACA,YAAMc,sBAAsB,GAAGpB,MAAM,CAACgB,KAAP,CAAa,CAAb,EAAgBE,uCAAhB,CAA/B,CAFsB,CAGtB;AACA;AACA;AACA;AACA;AACA;;AACA,YAAIE,sBAAsB,KAAKnB,QAAQ,CAACC,aAAT,CAAuBe,cAAvB,EAA/B,EAAwE;AACvEA,UAAAA,cAAc,GAAGhB,QAAQ,CAACC,aAAT,CAAuBe,cAAvB,EAAjB;AACA;AACD,OAZD,MAYO;AACNA,QAAAA,cAAc,GAAGX,WAAW,CAAC,CAAD,CAA5B;AACA;;AACD,aAAO;AACNE,QAAAA,cAAc,EAAdA,cADM;AAENS,QAAAA,cAAc,EAAdA,cAFM;AAGNR,QAAAA,WAAW,EAAXA;AAHM,OAAP;AAKA;AACD;;AACC,SAAO;AACND,IAAAA,cAAc,EAAER;AADV,GAAP;AAGF","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? }`.\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}"],"file":"extractNationalNumberFromPossiblyIncompleteNumber.js"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _metadata = _interopRequireDefault(require("../metadata"));
|
|
4
|
+
|
|
5
|
+
var _metadataMin = _interopRequireDefault(require("../../metadata.min.json"));
|
|
6
|
+
|
|
7
|
+
var _extractNationalNumberFromPossiblyIncompleteNumber = _interopRequireDefault(require("./extractNationalNumberFromPossiblyIncompleteNumber"));
|
|
8
|
+
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
10
|
+
|
|
11
|
+
describe('extractNationalNumberFromPossiblyIncompleteNumber', function () {
|
|
12
|
+
it('should parse a carrier code when there is no national prefix transform rule', function () {
|
|
13
|
+
var meta = new _metadata["default"](_metadataMin["default"]);
|
|
14
|
+
meta.country('AU');
|
|
15
|
+
(0, _extractNationalNumberFromPossiblyIncompleteNumber["default"])('18311800123', meta).should.deep.equal({
|
|
16
|
+
nationalPrefix: undefined,
|
|
17
|
+
carrierCode: '1831',
|
|
18
|
+
nationalNumber: '1800123'
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
//# sourceMappingURL=extractNationalNumberFromPossiblyIncompleteNumber.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../source/helpers/extractNationalNumberFromPossiblyIncompleteNumber.test.js"],"names":["describe","it","meta","Metadata","metadata","country","should","deep","equal","nationalPrefix","undefined","carrierCode","nationalNumber"],"mappings":";;AAAA;;AACA;;AACA;;;;AAEAA,QAAQ,CAAC,mDAAD,EAAsD,YAAM;AACnEC,EAAAA,EAAE,CAAC,6EAAD,EAAgF,YAAM;AACvF,QAAMC,IAAI,GAAG,IAAIC,oBAAJ,CAAaC,uBAAb,CAAb;AACAF,IAAAA,IAAI,CAACG,OAAL,CAAa,IAAb;AACA,uEAAkD,aAAlD,EAAiEH,IAAjE,EAAuEI,MAAvE,CAA8EC,IAA9E,CAAmFC,KAAnF,CAAyF;AACxFC,MAAAA,cAAc,EAAEC,SADwE;AAExFC,MAAAA,WAAW,EAAE,MAF2E;AAGxFC,MAAAA,cAAc,EAAE;AAHwE,KAAzF;AAKA,GARC,CAAF;AASA,CAVO,CAAR","sourcesContent":["import Metadata from '../metadata'\r\nimport metadata from '../../metadata.min.json'\r\nimport extractNationalNumberFromPossiblyIncompleteNumber from './extractNationalNumberFromPossiblyIncompleteNumber'\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\textractNationalNumberFromPossiblyIncompleteNumber('18311800123', meta).should.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})"],"file":"extractNationalNumberFromPossiblyIncompleteNumber.test.js"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = formatNationalNumberUsingFormat;
|
|
7
|
+
exports.FIRST_GROUP_PATTERN = void 0;
|
|
8
|
+
|
|
9
|
+
var _applyInternationalSeparatorStyle = _interopRequireDefault(require("./applyInternationalSeparatorStyle"));
|
|
10
|
+
|
|
11
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
12
|
+
|
|
13
|
+
// This was originally set to $1 but there are some countries for which the
|
|
14
|
+
// first group is not used in the national pattern (e.g. Argentina) so the $1
|
|
15
|
+
// group does not match correctly. Therefore, we use `\d`, so that the first
|
|
16
|
+
// group actually used in the pattern will be matched.
|
|
17
|
+
var FIRST_GROUP_PATTERN = /(\$\d)/;
|
|
18
|
+
exports.FIRST_GROUP_PATTERN = FIRST_GROUP_PATTERN;
|
|
19
|
+
|
|
20
|
+
function formatNationalNumberUsingFormat(number, format, _ref) {
|
|
21
|
+
var useInternationalFormat = _ref.useInternationalFormat,
|
|
22
|
+
withNationalPrefix = _ref.withNationalPrefix,
|
|
23
|
+
carrierCode = _ref.carrierCode,
|
|
24
|
+
metadata = _ref.metadata;
|
|
25
|
+
var formattedNumber = number.replace(new RegExp(format.pattern()), useInternationalFormat ? format.internationalFormat() : // This library doesn't use `domestic_carrier_code_formatting_rule`,
|
|
26
|
+
// because that one is only used when formatting phone numbers
|
|
27
|
+
// for dialing from a mobile phone, and this is not a dialing library.
|
|
28
|
+
// carrierCode && format.domesticCarrierCodeFormattingRule()
|
|
29
|
+
// // First, replace the $CC in the formatting rule with the desired carrier code.
|
|
30
|
+
// // Then, replace the $FG in the formatting rule with the first group
|
|
31
|
+
// // and the carrier code combined in the appropriate way.
|
|
32
|
+
// ? format.format().replace(FIRST_GROUP_PATTERN, format.domesticCarrierCodeFormattingRule().replace('$CC', carrierCode))
|
|
33
|
+
// : (
|
|
34
|
+
// withNationalPrefix && format.nationalPrefixFormattingRule()
|
|
35
|
+
// ? format.format().replace(FIRST_GROUP_PATTERN, format.nationalPrefixFormattingRule())
|
|
36
|
+
// : format.format()
|
|
37
|
+
// )
|
|
38
|
+
withNationalPrefix && format.nationalPrefixFormattingRule() ? format.format().replace(FIRST_GROUP_PATTERN, format.nationalPrefixFormattingRule()) : format.format());
|
|
39
|
+
|
|
40
|
+
if (useInternationalFormat) {
|
|
41
|
+
return (0, _applyInternationalSeparatorStyle["default"])(formattedNumber);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return formattedNumber;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=formatNationalNumberUsingFormat.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../source/helpers/formatNationalNumberUsingFormat.js"],"names":["FIRST_GROUP_PATTERN","formatNationalNumberUsingFormat","number","format","useInternationalFormat","withNationalPrefix","carrierCode","metadata","formattedNumber","replace","RegExp","pattern","internationalFormat","nationalPrefixFormattingRule"],"mappings":";;;;;;;;AAAA;;;;AAEA;AACA;AACA;AACA;AACO,IAAMA,mBAAmB,GAAG,QAA5B;;;AAEQ,SAASC,+BAAT,CACdC,MADc,EAEdC,MAFc,QASb;AAAA,MALAC,sBAKA,QALAA,sBAKA;AAAA,MAJAC,kBAIA,QAJAA,kBAIA;AAAA,MAHAC,WAGA,QAHAA,WAGA;AAAA,MAFAC,QAEA,QAFAA,QAEA;AACD,MAAMC,eAAe,GAAGN,MAAM,CAACO,OAAP,CACvB,IAAIC,MAAJ,CAAWP,MAAM,CAACQ,OAAP,EAAX,CADuB,EAEvBP,sBAAsB,GACnBD,MAAM,CAACS,mBAAP,EADmB,GAGpB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACAP,EAAAA,kBAAkB,IAAIF,MAAM,CAACU,4BAAP,EAAtB,GACGV,MAAM,CAACA,MAAP,GAAgBM,OAAhB,CAAwBT,mBAAxB,EAA6CG,MAAM,CAACU,4BAAP,EAA7C,CADH,GAEGV,MAAM,CAACA,MAAP,EApBkB,CAAxB;;AAuBA,MAAIC,sBAAJ,EAA4B;AAC3B,WAAO,kDAAiCI,eAAjC,CAAP;AACA;;AACD,SAAOA,eAAP;AACA","sourcesContent":["import applyInternationalSeparatorStyle from './applyInternationalSeparatorStyle'\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}"],"file":"formatNationalNumberUsingFormat.js"}
|