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
package/source/format_.js
CHANGED
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
//
|
|
4
4
|
// https://github.com/googlei18n/libphonenumber/commits/master/javascript/i18n/phonenumbers/phonenumberutil.js
|
|
5
5
|
|
|
6
|
-
import
|
|
7
|
-
import
|
|
6
|
+
import matchesEntirely from './helpers/matchesEntirely'
|
|
7
|
+
import formatNationalNumberUsingFormat from './helpers/formatNationalNumberUsingFormat'
|
|
8
8
|
import Metadata, { getCountryCallingCode } from './metadata'
|
|
9
|
-
import
|
|
10
|
-
import { formatRFC3966 } from './RFC3966'
|
|
9
|
+
import getIddPrefix from './helpers/getIddPrefix'
|
|
10
|
+
import { formatRFC3966 } from './helpers/RFC3966'
|
|
11
11
|
|
|
12
12
|
const DEFAULT_OPTIONS = {
|
|
13
13
|
formatExtension: (formattedNumber, extension, metadata) => `${formattedNumber}${metadata.ext()}${extension}`
|
|
@@ -109,51 +109,6 @@ export default function formatNumber(input, format, options, metadata) {
|
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
-
// This was originally set to $1 but there are some countries for which the
|
|
113
|
-
// first group is not used in the national pattern (e.g. Argentina) so the $1
|
|
114
|
-
// group does not match correctly. Therefore, we use `\d`, so that the first
|
|
115
|
-
// group actually used in the pattern will be matched.
|
|
116
|
-
export const FIRST_GROUP_PATTERN = /(\$\d)/
|
|
117
|
-
|
|
118
|
-
export function formatNationalNumberUsingFormat(
|
|
119
|
-
number,
|
|
120
|
-
format,
|
|
121
|
-
{
|
|
122
|
-
useInternationalFormat,
|
|
123
|
-
withNationalPrefix,
|
|
124
|
-
carrierCode,
|
|
125
|
-
metadata
|
|
126
|
-
}
|
|
127
|
-
) {
|
|
128
|
-
const formattedNumber = number.replace(
|
|
129
|
-
new RegExp(format.pattern()),
|
|
130
|
-
useInternationalFormat
|
|
131
|
-
? format.internationalFormat()
|
|
132
|
-
: (
|
|
133
|
-
// This library doesn't use `domestic_carrier_code_formatting_rule`,
|
|
134
|
-
// because that one is only used when formatting phone numbers
|
|
135
|
-
// for dialing from a mobile phone, and this is not a dialing library.
|
|
136
|
-
// carrierCode && format.domesticCarrierCodeFormattingRule()
|
|
137
|
-
// // First, replace the $CC in the formatting rule with the desired carrier code.
|
|
138
|
-
// // Then, replace the $FG in the formatting rule with the first group
|
|
139
|
-
// // and the carrier code combined in the appropriate way.
|
|
140
|
-
// ? format.format().replace(FIRST_GROUP_PATTERN, format.domesticCarrierCodeFormattingRule().replace('$CC', carrierCode))
|
|
141
|
-
// : (
|
|
142
|
-
// withNationalPrefix && format.nationalPrefixFormattingRule()
|
|
143
|
-
// ? format.format().replace(FIRST_GROUP_PATTERN, format.nationalPrefixFormattingRule())
|
|
144
|
-
// : format.format()
|
|
145
|
-
// )
|
|
146
|
-
withNationalPrefix && format.nationalPrefixFormattingRule()
|
|
147
|
-
? format.format().replace(FIRST_GROUP_PATTERN, format.nationalPrefixFormattingRule())
|
|
148
|
-
: format.format()
|
|
149
|
-
)
|
|
150
|
-
)
|
|
151
|
-
if (useInternationalFormat) {
|
|
152
|
-
return applyInternationalSeparatorStyle(formattedNumber)
|
|
153
|
-
}
|
|
154
|
-
return formattedNumber
|
|
155
|
-
}
|
|
156
|
-
|
|
157
112
|
function formatNationalNumber(number, carrierCode, formatAs, metadata, options) {
|
|
158
113
|
const format = chooseFormatForNumber(metadata.formats(), number)
|
|
159
114
|
if (!format) {
|
|
@@ -189,40 +144,6 @@ function chooseFormatForNumber(availableFormats, nationalNnumber) {
|
|
|
189
144
|
}
|
|
190
145
|
}
|
|
191
146
|
|
|
192
|
-
// Removes brackets and replaces dashes with spaces.
|
|
193
|
-
//
|
|
194
|
-
// E.g. "(999) 111-22-33" -> "999 111 22 33"
|
|
195
|
-
//
|
|
196
|
-
// For some reason Google's metadata contains `<intlFormat/>`s with brackets and dashes.
|
|
197
|
-
// Meanwhile, there's no single opinion about using punctuation in international phone numbers.
|
|
198
|
-
//
|
|
199
|
-
// For example, Google's `<intlFormat/>` for USA is `+1 213-373-4253`.
|
|
200
|
-
// And here's a quote from WikiPedia's "North American Numbering Plan" page:
|
|
201
|
-
// https://en.wikipedia.org/wiki/North_American_Numbering_Plan
|
|
202
|
-
//
|
|
203
|
-
// "The country calling code for all countries participating in the NANP is 1.
|
|
204
|
-
// In international format, an NANP number should be listed as +1 301 555 01 00,
|
|
205
|
-
// where 301 is an area code (Maryland)."
|
|
206
|
-
//
|
|
207
|
-
// I personally prefer the international format without any punctuation.
|
|
208
|
-
// For example, brackets are remnants of the old age, meaning that the
|
|
209
|
-
// phone number part in brackets (so called "area code") can be omitted
|
|
210
|
-
// if dialing within the same "area".
|
|
211
|
-
// And hyphens were clearly introduced for splitting local numbers into memorizable groups.
|
|
212
|
-
// For example, remembering "5553535" is difficult but "555-35-35" is much simpler.
|
|
213
|
-
// Imagine a man taking a bus from home to work and seeing an ad with a phone number.
|
|
214
|
-
// He has a couple of seconds to memorize that number until it passes by.
|
|
215
|
-
// If it were spaces instead of hyphens the man wouldn't necessarily get it,
|
|
216
|
-
// but with hyphens instead of spaces the grouping is more explicit.
|
|
217
|
-
// I personally think that hyphens introduce visual clutter,
|
|
218
|
-
// so I prefer replacing them with spaces in international numbers.
|
|
219
|
-
// In the modern age all output is done on displays where spaces are clearly distinguishable
|
|
220
|
-
// so hyphens can be safely replaced with spaces without losing any legibility.
|
|
221
|
-
//
|
|
222
|
-
export function applyInternationalSeparatorStyle(local) {
|
|
223
|
-
return local.replace(new RegExp(`[${VALID_PUNCTUATION}]+`, 'g'), ' ').trim()
|
|
224
|
-
}
|
|
225
|
-
|
|
226
147
|
function addExtension(formattedNumber, ext, metadata, formatExtension) {
|
|
227
148
|
return ext ? formatExtension(formattedNumber, ext, metadata) : formattedNumber
|
|
228
149
|
}
|
|
@@ -254,8 +175,8 @@ function formatIDD(
|
|
|
254
175
|
//
|
|
255
176
|
return formattedNumber
|
|
256
177
|
}
|
|
257
|
-
const
|
|
258
|
-
if (
|
|
259
|
-
return `${
|
|
178
|
+
const iddPrefix = getIddPrefix(fromCountry, undefined, metadata.metadata)
|
|
179
|
+
if (iddPrefix) {
|
|
180
|
+
return `${iddPrefix} ${countryCallingCode} ${formatNationalNumber(nationalNumber, null, 'INTERNATIONAL', metadata)}`
|
|
260
181
|
}
|
|
261
182
|
}
|
package/source/getNumberType.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import isViablePhoneNumber from './isViablePhoneNumber'
|
|
2
|
-
import _getNumberType from './
|
|
1
|
+
import isViablePhoneNumber from './helpers/isViablePhoneNumber'
|
|
2
|
+
import _getNumberType from './helpers/getNumberType'
|
|
3
3
|
import parse from './parse_'
|
|
4
4
|
|
|
5
5
|
// Finds out national phone number type (fixed line, mobile, etc)
|
|
@@ -6,19 +6,16 @@ import isViablePhoneNumber from './isViablePhoneNumber'
|
|
|
6
6
|
* @param {string} text - Phone URI (RFC 3966).
|
|
7
7
|
* @return {object} `{ ?number, ?ext }`.
|
|
8
8
|
*/
|
|
9
|
-
export function parseRFC3966(text)
|
|
10
|
-
{
|
|
9
|
+
export function parseRFC3966(text) {
|
|
11
10
|
let number
|
|
12
11
|
let ext
|
|
13
12
|
|
|
14
13
|
// Replace "tel:" with "tel=" for parsing convenience.
|
|
15
14
|
text = text.replace(/^tel:/, 'tel=')
|
|
16
15
|
|
|
17
|
-
for (const part of text.split(';'))
|
|
18
|
-
{
|
|
16
|
+
for (const part of text.split(';')) {
|
|
19
17
|
const [name, value] = part.split('=')
|
|
20
|
-
switch (name)
|
|
21
|
-
{
|
|
18
|
+
switch (name) {
|
|
22
19
|
case 'tel':
|
|
23
20
|
number = value
|
|
24
21
|
break
|
|
@@ -36,8 +33,7 @@ export function parseRFC3966(text)
|
|
|
36
33
|
}
|
|
37
34
|
|
|
38
35
|
// If the phone number is not viable, then abort.
|
|
39
|
-
if (!isViablePhoneNumber(number))
|
|
40
|
-
{
|
|
36
|
+
if (!isViablePhoneNumber(number)) {
|
|
41
37
|
return {}
|
|
42
38
|
}
|
|
43
39
|
|
|
@@ -52,17 +48,12 @@ export function parseRFC3966(text)
|
|
|
52
48
|
* @param {object} - `{ ?number, ?extension }`.
|
|
53
49
|
* @return {string} Phone URI (RFC 3966).
|
|
54
50
|
*/
|
|
55
|
-
export function formatRFC3966({ number, ext })
|
|
56
|
-
{
|
|
57
|
-
if (!number)
|
|
58
|
-
{
|
|
51
|
+
export function formatRFC3966({ number, ext }) {
|
|
52
|
+
if (!number) {
|
|
59
53
|
return ''
|
|
60
54
|
}
|
|
61
|
-
|
|
62
|
-
if (number[0] !== '+')
|
|
63
|
-
{
|
|
55
|
+
if (number[0] !== '+') {
|
|
64
56
|
throw new Error(`"formatRFC3966()" expects "number" to be in E.164 format.`)
|
|
65
57
|
}
|
|
66
|
-
|
|
67
58
|
return `tel:${number}${ext ? ';ext=' + ext : ''}`
|
|
68
59
|
}
|
|
File without changes
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { VALID_PUNCTUATION } from '../constants'
|
|
2
|
+
|
|
3
|
+
// Removes brackets and replaces dashes with spaces.
|
|
4
|
+
//
|
|
5
|
+
// E.g. "(999) 111-22-33" -> "999 111 22 33"
|
|
6
|
+
//
|
|
7
|
+
// For some reason Google's metadata contains `<intlFormat/>`s with brackets and dashes.
|
|
8
|
+
// Meanwhile, there's no single opinion about using punctuation in international phone numbers.
|
|
9
|
+
//
|
|
10
|
+
// For example, Google's `<intlFormat/>` for USA is `+1 213-373-4253`.
|
|
11
|
+
// And here's a quote from WikiPedia's "North American Numbering Plan" page:
|
|
12
|
+
// https://en.wikipedia.org/wiki/North_American_Numbering_Plan
|
|
13
|
+
//
|
|
14
|
+
// "The country calling code for all countries participating in the NANP is 1.
|
|
15
|
+
// In international format, an NANP number should be listed as +1 301 555 01 00,
|
|
16
|
+
// where 301 is an area code (Maryland)."
|
|
17
|
+
//
|
|
18
|
+
// I personally prefer the international format without any punctuation.
|
|
19
|
+
// For example, brackets are remnants of the old age, meaning that the
|
|
20
|
+
// phone number part in brackets (so called "area code") can be omitted
|
|
21
|
+
// if dialing within the same "area".
|
|
22
|
+
// And hyphens were clearly introduced for splitting local numbers into memorizable groups.
|
|
23
|
+
// For example, remembering "5553535" is difficult but "555-35-35" is much simpler.
|
|
24
|
+
// Imagine a man taking a bus from home to work and seeing an ad with a phone number.
|
|
25
|
+
// He has a couple of seconds to memorize that number until it passes by.
|
|
26
|
+
// If it were spaces instead of hyphens the man wouldn't necessarily get it,
|
|
27
|
+
// but with hyphens instead of spaces the grouping is more explicit.
|
|
28
|
+
// I personally think that hyphens introduce visual clutter,
|
|
29
|
+
// so I prefer replacing them with spaces in international numbers.
|
|
30
|
+
// In the modern age all output is done on displays where spaces are clearly distinguishable
|
|
31
|
+
// so hyphens can be safely replaced with spaces without losing any legibility.
|
|
32
|
+
//
|
|
33
|
+
export default function applyInternationalSeparatorStyle(formattedNumber) {
|
|
34
|
+
return formattedNumber.replace(new RegExp(`[${VALID_PUNCTUATION}]+`, 'g'), ' ').trim()
|
|
35
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import applyInternationalSeparatorStyle from './applyInternationalSeparatorStyle'
|
|
2
|
+
|
|
3
|
+
describe('applyInternationalSeparatorStyle', () => {
|
|
4
|
+
it('should change Google\'s international format style', () => {
|
|
5
|
+
applyInternationalSeparatorStyle('(xxx) xxx-xx-xx').should.equal('xxx xxx xx xx')
|
|
6
|
+
applyInternationalSeparatorStyle('(xxx)xxx').should.equal('xxx xxx')
|
|
7
|
+
})
|
|
8
|
+
})
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { VALID_DIGITS } from '
|
|
1
|
+
import { VALID_DIGITS } from '../../constants'
|
|
2
2
|
|
|
3
3
|
// The RFC 3966 format for extensions.
|
|
4
4
|
const RFC3966_EXTN_PREFIX = ';ext='
|
|
@@ -17,7 +17,7 @@ const getExtensionDigitsPattern = (maxLength) => `([${VALID_DIGITS}]{1,${maxLeng
|
|
|
17
17
|
* https://github.com/google/libphonenumber/blob/55b2646ec9393f4d3d6661b9c82ef9e258e8b829/javascript/i18n/phonenumbers/phonenumberutil.js#L759-L766
|
|
18
18
|
* @return {string} RegEx pattern to capture extensions.
|
|
19
19
|
*/
|
|
20
|
-
export function createExtensionPattern(purpose) {
|
|
20
|
+
export default function createExtensionPattern(purpose) {
|
|
21
21
|
// We cap the maximum length of an extension based on the ambiguity of the way
|
|
22
22
|
// the extension is prefixed. As per ITU, the officially allowed length for
|
|
23
23
|
// extensions is actually 40, but we don't support this since we haven't seen real
|
|
@@ -109,51 +109,4 @@ export function createExtensionPattern(purpose) {
|
|
|
109
109
|
+ americanStyleExtnWithSuffix + "|"
|
|
110
110
|
+ autoDiallingExtn + "|"
|
|
111
111
|
+ onlyCommasExtn;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Regexp of all possible ways to write extensions, for use when parsing. This
|
|
116
|
-
* will be run as a case-insensitive regexp match. Wide character versions are
|
|
117
|
-
* also provided after each ASCII version. There are three regular expressions
|
|
118
|
-
* here. The first covers RFC 3966 format, where the extension is added using
|
|
119
|
-
* ';ext='. The second more generic one starts with optional white space and
|
|
120
|
-
* ends with an optional full stop (.), followed by zero or more spaces/tabs
|
|
121
|
-
* /commas and then the numbers themselves. The other one covers the special
|
|
122
|
-
* case of American numbers where the extension is written with a hash at the
|
|
123
|
-
* end, such as '- 503#'. Note that the only capturing groups should be around
|
|
124
|
-
* the digits that you want to capture as part of the extension, or else parsing
|
|
125
|
-
* will fail! We allow two options for representing the accented o - the
|
|
126
|
-
* character itself, and one in the unicode decomposed form with the combining
|
|
127
|
-
* acute accent.
|
|
128
|
-
*/
|
|
129
|
-
export const EXTN_PATTERNS_FOR_PARSING = createExtensionPattern('parsing')
|
|
130
|
-
|
|
131
|
-
export const EXTN_PATTERNS_FOR_MATCHING = createExtensionPattern('matching')
|
|
132
|
-
|
|
133
|
-
// Regexp of all known extension prefixes used by different regions followed by
|
|
134
|
-
// 1 or more valid digits, for use when parsing.
|
|
135
|
-
const EXTN_PATTERN = new RegExp('(?:' + createExtensionPattern() + ')$', 'i')
|
|
136
|
-
|
|
137
|
-
// Strips any extension (as in, the part of the number dialled after the call is
|
|
138
|
-
// connected, usually indicated with extn, ext, x or similar) from the end of
|
|
139
|
-
// the number, and returns it.
|
|
140
|
-
export function extractExtension(number) {
|
|
141
|
-
const start = number.search(EXTN_PATTERN)
|
|
142
|
-
if (start < 0) {
|
|
143
|
-
return {}
|
|
144
|
-
}
|
|
145
|
-
// If we find a potential extension, and the number preceding this is a viable
|
|
146
|
-
// number, we assume it is an extension.
|
|
147
|
-
const numberWithoutExtension = number.slice(0, start)
|
|
148
|
-
const matches = number.match(EXTN_PATTERN)
|
|
149
|
-
let i = 1
|
|
150
|
-
while (i < matches.length) {
|
|
151
|
-
if (matches[i]) {
|
|
152
|
-
return {
|
|
153
|
-
number: numberWithoutExtension,
|
|
154
|
-
ext: matches[i]
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
i++
|
|
158
|
-
}
|
|
159
112
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import createExtensionPattern from './createExtensionPattern'
|
|
2
|
+
|
|
3
|
+
// Regexp of all known extension prefixes used by different regions followed by
|
|
4
|
+
// 1 or more valid digits, for use when parsing.
|
|
5
|
+
const EXTN_PATTERN = new RegExp('(?:' + createExtensionPattern() + ')$', 'i')
|
|
6
|
+
|
|
7
|
+
// Strips any extension (as in, the part of the number dialled after the call is
|
|
8
|
+
// connected, usually indicated with extn, ext, x or similar) from the end of
|
|
9
|
+
// the number, and returns it.
|
|
10
|
+
export default function extractExtension(number) {
|
|
11
|
+
const start = number.search(EXTN_PATTERN)
|
|
12
|
+
if (start < 0) {
|
|
13
|
+
return {}
|
|
14
|
+
}
|
|
15
|
+
// If we find a potential extension, and the number preceding this is a viable
|
|
16
|
+
// number, we assume it is an extension.
|
|
17
|
+
const numberWithoutExtension = number.slice(0, start)
|
|
18
|
+
const matches = number.match(EXTN_PATTERN)
|
|
19
|
+
let i = 1
|
|
20
|
+
while (i < matches.length) {
|
|
21
|
+
if (matches[i]) {
|
|
22
|
+
return {
|
|
23
|
+
number: numberWithoutExtension,
|
|
24
|
+
ext: matches[i]
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
i++
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import stripIddPrefix from './stripIddPrefix'
|
|
2
|
+
import extractCountryCallingCodeFromInternationalNumberWithoutPlusSign from './extractCountryCallingCodeFromInternationalNumberWithoutPlusSign'
|
|
3
|
+
import Metadata from '../metadata'
|
|
4
|
+
import { MAX_LENGTH_COUNTRY_CODE } from '../constants'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Converts a phone number digits (possibly with a `+`)
|
|
8
|
+
* into a calling code and the rest phone number digits.
|
|
9
|
+
* The "rest phone number digits" could include
|
|
10
|
+
* a national prefix, carrier code, and national
|
|
11
|
+
* (significant) number.
|
|
12
|
+
* @param {string} number — Phone number digits (possibly with a `+`).
|
|
13
|
+
* @param {string} [country] — Default country.
|
|
14
|
+
* @param {string} [callingCode] — Default calling code (some phone numbering plans are non-geographic).
|
|
15
|
+
* @param {object} metadata
|
|
16
|
+
* @return {object} `{ countryCallingCode: string?, number: string }`
|
|
17
|
+
* @example
|
|
18
|
+
* // Returns `{ countryCallingCode: "1", number: "2133734253" }`.
|
|
19
|
+
* extractCountryCallingCode('2133734253', 'US', null, metadata)
|
|
20
|
+
* extractCountryCallingCode('2133734253', null, '1', metadata)
|
|
21
|
+
* extractCountryCallingCode('+12133734253', null, null, metadata)
|
|
22
|
+
* extractCountryCallingCode('+12133734253', 'RU', null, metadata)
|
|
23
|
+
*/
|
|
24
|
+
export default function extractCountryCallingCode(
|
|
25
|
+
number,
|
|
26
|
+
country,
|
|
27
|
+
callingCode,
|
|
28
|
+
metadata
|
|
29
|
+
) {
|
|
30
|
+
if (!number) {
|
|
31
|
+
return {}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// If this is not an international phone number,
|
|
35
|
+
// then either extract an "IDD" prefix, or extract a
|
|
36
|
+
// country calling code from a number by autocorrecting it
|
|
37
|
+
// by prepending a leading `+` in cases when it starts
|
|
38
|
+
// with the country calling code.
|
|
39
|
+
// https://wikitravel.org/en/International_dialling_prefix
|
|
40
|
+
// https://github.com/catamphetamine/libphonenumber-js/issues/376
|
|
41
|
+
if (number[0] !== '+') {
|
|
42
|
+
// Convert an "out-of-country" dialing phone number
|
|
43
|
+
// to a proper international phone number.
|
|
44
|
+
const numberWithoutIDD = stripIddPrefix(number, country, callingCode, metadata)
|
|
45
|
+
// If an IDD prefix was stripped then
|
|
46
|
+
// convert the number to international one
|
|
47
|
+
// for subsequent parsing.
|
|
48
|
+
if (numberWithoutIDD && numberWithoutIDD !== number) {
|
|
49
|
+
number = '+' + numberWithoutIDD
|
|
50
|
+
} else {
|
|
51
|
+
// Check to see if the number starts with the country calling code
|
|
52
|
+
// for the default country. If so, we remove the country calling code,
|
|
53
|
+
// and do some checks on the validity of the number before and after.
|
|
54
|
+
// https://github.com/catamphetamine/libphonenumber-js/issues/376
|
|
55
|
+
if (country || callingCode) {
|
|
56
|
+
const {
|
|
57
|
+
countryCallingCode,
|
|
58
|
+
number: shorterNumber
|
|
59
|
+
} = extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(
|
|
60
|
+
number,
|
|
61
|
+
country,
|
|
62
|
+
callingCode,
|
|
63
|
+
metadata
|
|
64
|
+
)
|
|
65
|
+
if (countryCallingCode) {
|
|
66
|
+
return {
|
|
67
|
+
countryCallingCode,
|
|
68
|
+
number: shorterNumber
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return { number }
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Fast abortion: country codes do not begin with a '0'
|
|
77
|
+
if (number[1] === '0') {
|
|
78
|
+
return {}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
metadata = new Metadata(metadata)
|
|
82
|
+
|
|
83
|
+
// The thing with country phone codes
|
|
84
|
+
// is that they are orthogonal to each other
|
|
85
|
+
// i.e. there's no such country phone code A
|
|
86
|
+
// for which country phone code B exists
|
|
87
|
+
// where B starts with A.
|
|
88
|
+
// Therefore, while scanning digits,
|
|
89
|
+
// if a valid country code is found,
|
|
90
|
+
// that means that it is the country code.
|
|
91
|
+
//
|
|
92
|
+
let i = 2
|
|
93
|
+
while (i - 1 <= MAX_LENGTH_COUNTRY_CODE && i <= number.length) {
|
|
94
|
+
const countryCallingCode = number.slice(1, i)
|
|
95
|
+
if (metadata.hasCallingCode(countryCallingCode)) {
|
|
96
|
+
metadata.selectNumberingPlan(countryCallingCode)
|
|
97
|
+
return {
|
|
98
|
+
countryCallingCode,
|
|
99
|
+
number: number.slice(i)
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
i++
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return {}
|
|
106
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import extractCountryCallingCode from './extractCountryCallingCode'
|
|
2
|
+
import metadata from '../../metadata.min'
|
|
3
|
+
|
|
4
|
+
describe('extractCountryCallingCode', () => {
|
|
5
|
+
it('should extract country calling code from a number', () => {
|
|
6
|
+
extractCountryCallingCode('+78005553535', null, null, metadata).should.deep.equal({
|
|
7
|
+
countryCallingCode: '7',
|
|
8
|
+
number: '8005553535'
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
extractCountryCallingCode('+7800', null, null, metadata).should.deep.equal({
|
|
12
|
+
countryCallingCode: '7',
|
|
13
|
+
number: '800'
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
extractCountryCallingCode('', null, null, metadata).should.deep.equal({})
|
|
17
|
+
})
|
|
18
|
+
})
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import Metadata from '../metadata'
|
|
2
|
+
import matchesEntirely from './matchesEntirely'
|
|
3
|
+
import extractNationalNumber from './extractNationalNumber'
|
|
4
|
+
import checkNumberLength from './checkNumberLength'
|
|
5
|
+
import getCountryCallingCode from '../getCountryCallingCode'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Sometimes some people incorrectly input international phone numbers
|
|
9
|
+
* without the leading `+`. This function corrects such input.
|
|
10
|
+
* @param {string} number — Phone number digits.
|
|
11
|
+
* @param {string?} country
|
|
12
|
+
* @param {string?} callingCode
|
|
13
|
+
* @param {object} metadata
|
|
14
|
+
* @return {object} `{ countryCallingCode: string?, number: string }`.
|
|
15
|
+
*/
|
|
16
|
+
export default function extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(
|
|
17
|
+
number,
|
|
18
|
+
country,
|
|
19
|
+
callingCode,
|
|
20
|
+
metadata
|
|
21
|
+
) {
|
|
22
|
+
const countryCallingCode = country ? getCountryCallingCode(country, metadata) : callingCode
|
|
23
|
+
if (number.indexOf(countryCallingCode) === 0) {
|
|
24
|
+
metadata = new Metadata(metadata)
|
|
25
|
+
metadata.selectNumberingPlan(country, callingCode)
|
|
26
|
+
const possibleShorterNumber = number.slice(countryCallingCode.length)
|
|
27
|
+
const {
|
|
28
|
+
nationalNumber: possibleShorterNationalNumber,
|
|
29
|
+
} = extractNationalNumber(
|
|
30
|
+
possibleShorterNumber,
|
|
31
|
+
metadata
|
|
32
|
+
)
|
|
33
|
+
const {
|
|
34
|
+
nationalNumber
|
|
35
|
+
} = extractNationalNumber(
|
|
36
|
+
number,
|
|
37
|
+
metadata
|
|
38
|
+
)
|
|
39
|
+
// If the number was not valid before but is valid now,
|
|
40
|
+
// or if it was too long before, we consider the number
|
|
41
|
+
// with the country calling code stripped to be a better result
|
|
42
|
+
// and keep that instead.
|
|
43
|
+
// For example, in Germany (+49), `49` is a valid area code,
|
|
44
|
+
// so if a number starts with `49`, it could be both a valid
|
|
45
|
+
// national German number or an international number without
|
|
46
|
+
// a leading `+`.
|
|
47
|
+
if (
|
|
48
|
+
(
|
|
49
|
+
!matchesEntirely(nationalNumber, metadata.nationalNumberPattern())
|
|
50
|
+
&&
|
|
51
|
+
matchesEntirely(possibleShorterNationalNumber, metadata.nationalNumberPattern())
|
|
52
|
+
)
|
|
53
|
+
||
|
|
54
|
+
checkNumberLength(nationalNumber, metadata) === 'TOO_LONG'
|
|
55
|
+
) {
|
|
56
|
+
return {
|
|
57
|
+
countryCallingCode,
|
|
58
|
+
number: possibleShorterNumber
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return { number }
|
|
63
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import extractNationalNumberFromPossiblyIncompleteNumber from './extractNationalNumberFromPossiblyIncompleteNumber'
|
|
2
|
+
import matchesEntirely from './matchesEntirely'
|
|
3
|
+
import checkNumberLength from './checkNumberLength'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Strips national prefix and carrier code from a complete phone number.
|
|
7
|
+
* The difference from the non-"FromCompleteNumber" function is that
|
|
8
|
+
* it won't extract national prefix if the resultant number is too short
|
|
9
|
+
* to be a complete number for the selected phone numbering plan.
|
|
10
|
+
* @param {string} number — Complete phone number digits.
|
|
11
|
+
* @param {Metadata} metadata — Metadata with a phone numbering plan selected.
|
|
12
|
+
* @return {object} `{ nationalNumber: string, carrierCode: string? }`.
|
|
13
|
+
*/
|
|
14
|
+
export default function extractNationalNumber(number, metadata) {
|
|
15
|
+
// Parsing national prefixes and carrier codes
|
|
16
|
+
// is only required for local phone numbers
|
|
17
|
+
// but some people don't understand that
|
|
18
|
+
// and sometimes write international phone numbers
|
|
19
|
+
// with national prefixes (or maybe even carrier codes).
|
|
20
|
+
// http://ucken.blogspot.ru/2016/03/trunk-prefixes-in-skype4b.html
|
|
21
|
+
// Google's original library forgives such mistakes
|
|
22
|
+
// and so does this library, because it has been requested:
|
|
23
|
+
// https://github.com/catamphetamine/libphonenumber-js/issues/127
|
|
24
|
+
const {
|
|
25
|
+
nationalNumber,
|
|
26
|
+
carrierCode
|
|
27
|
+
} = extractNationalNumberFromPossiblyIncompleteNumber(
|
|
28
|
+
number,
|
|
29
|
+
metadata
|
|
30
|
+
)
|
|
31
|
+
if (!shouldExtractNationalPrefix(number, nationalNumber, metadata)) {
|
|
32
|
+
// Don't strip the national prefix.
|
|
33
|
+
return { nationalNumber: number }
|
|
34
|
+
}
|
|
35
|
+
// If a national prefix has been extracted, check to see
|
|
36
|
+
// if the resultant number isn't too short.
|
|
37
|
+
// Same code in Google's `libphonenumber`:
|
|
38
|
+
// https://github.com/google/libphonenumber/blob/e326fa1fc4283bb05eb35cb3c15c18f98a31af33/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L3291-L3302
|
|
39
|
+
// For some reason, they do this check right after the `national_number_pattern` check
|
|
40
|
+
// this library does in `shouldExtractNationalPrefix()` function.
|
|
41
|
+
// Why is there a second "resultant" number validity check?
|
|
42
|
+
// They don't provide an explanation.
|
|
43
|
+
// This library just copies the behavior.
|
|
44
|
+
if (number.length !== nationalNumber.length + (carrierCode ? carrierCode.length : 0)) {
|
|
45
|
+
// If not using legacy generated metadata (before version `1.0.18`)
|
|
46
|
+
// then it has "possible lengths", so use those to validate the number length.
|
|
47
|
+
if (metadata.possibleLengths()) {
|
|
48
|
+
// "We require that the NSN remaining after stripping the national prefix and
|
|
49
|
+
// carrier code be long enough to be a possible length for the region.
|
|
50
|
+
// Otherwise, we don't do the stripping, since the original number could be
|
|
51
|
+
// a valid short number."
|
|
52
|
+
// https://github.com/google/libphonenumber/blob/876268eb1ad6cdc1b7b5bef17fc5e43052702d57/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L3236-L3250
|
|
53
|
+
switch (checkNumberLength(nationalNumber, metadata)) {
|
|
54
|
+
case 'TOO_SHORT':
|
|
55
|
+
case 'INVALID_LENGTH':
|
|
56
|
+
// case 'IS_POSSIBLE_LOCAL_ONLY':
|
|
57
|
+
// Don't strip the national prefix.
|
|
58
|
+
return { nationalNumber: number }
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return { nationalNumber, carrierCode }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// In some countries, the same digit could be a national prefix
|
|
66
|
+
// or a leading digit of a valid phone number.
|
|
67
|
+
// For example, in Russia, national prefix is `8`,
|
|
68
|
+
// and also `800 555 35 35` is a valid number
|
|
69
|
+
// in which `8` is not a national prefix, but the first digit
|
|
70
|
+
// of a national (significant) number.
|
|
71
|
+
// Same's with Belarus:
|
|
72
|
+
// `82004910060` is a valid national (significant) number,
|
|
73
|
+
// but `2004910060` is not.
|
|
74
|
+
// To support such cases (to prevent the code from always stripping
|
|
75
|
+
// national prefix), a condition is imposed: a national prefix
|
|
76
|
+
// is not extracted when the original number is "viable" and the
|
|
77
|
+
// resultant number is not, a "viable" national number being the one
|
|
78
|
+
// that matches `national_number_pattern`.
|
|
79
|
+
function shouldExtractNationalPrefix(number, nationalSignificantNumber, metadata) {
|
|
80
|
+
// The equivalent in Google's code is:
|
|
81
|
+
// https://github.com/google/libphonenumber/blob/e326fa1fc4283bb05eb35cb3c15c18f98a31af33/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L2969-L3004
|
|
82
|
+
if (matchesEntirely(number, metadata.nationalNumberPattern()) &&
|
|
83
|
+
!matchesEntirely(nationalSignificantNumber, metadata.nationalNumberPattern())) {
|
|
84
|
+
return false
|
|
85
|
+
}
|
|
86
|
+
// Just "possible" number check would be more relaxed, so it's not used.
|
|
87
|
+
// if (isPossibleNumber(number, metadata) &&
|
|
88
|
+
// !isPossibleNumber(numberWithNationalPrefixExtracted, metadata)) {
|
|
89
|
+
// return false
|
|
90
|
+
// }
|
|
91
|
+
return true
|
|
92
|
+
}
|