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,13 @@
|
|
|
1
|
+
import metadata from '../metadata.min.json'
|
|
2
|
+
import fs from 'fs'
|
|
3
|
+
|
|
4
|
+
const countryCodes = Object.keys(metadata.countries)
|
|
5
|
+
|
|
6
|
+
fs.writeFileSync(
|
|
7
|
+
'./types.d.ts',
|
|
8
|
+
fs.readFileSync('./types.d.ts', 'utf-8').replace(
|
|
9
|
+
/export type CountryCode = .*;/,
|
|
10
|
+
`export type CountryCode = ${countryCodes.map(_ => `'${_}'`).join(' | ')};`
|
|
11
|
+
),
|
|
12
|
+
'utf-8'
|
|
13
|
+
)
|
package/source/AsYouType.js
CHANGED
|
@@ -2,8 +2,8 @@ import Metadata from './metadata'
|
|
|
2
2
|
import PhoneNumber from './PhoneNumber'
|
|
3
3
|
import AsYouTypeState from './AsYouTypeState'
|
|
4
4
|
import AsYouTypeFormatter, { DIGIT_PLACEHOLDER } from './AsYouTypeFormatter'
|
|
5
|
-
import AsYouTypeParser, { extractFormattedDigitsAndPlus
|
|
6
|
-
import
|
|
5
|
+
import AsYouTypeParser, { extractFormattedDigitsAndPlus } from './AsYouTypeParser'
|
|
6
|
+
import getCountryByCallingCode from './helpers/getCountryByCallingCode'
|
|
7
7
|
|
|
8
8
|
const USE_NON_GEOGRAPHIC_COUNTRY_CODE = false
|
|
9
9
|
|
|
@@ -72,6 +72,7 @@ export default class AsYouType {
|
|
|
72
72
|
if (formattedNationalNumber === undefined) {
|
|
73
73
|
// See if another national (significant) number could be re-extracted.
|
|
74
74
|
if (this.parser.reExtractNationalSignificantNumber(this.state)) {
|
|
75
|
+
this.determineTheCountryIfNeeded()
|
|
75
76
|
// If it could, then re-try formatting the new national (significant) number.
|
|
76
77
|
const nationalDigits = this.state.getNationalDigits()
|
|
77
78
|
if (nationalDigits) {
|
|
@@ -196,7 +197,7 @@ export default class AsYouType {
|
|
|
196
197
|
// Prepends `+CountryCode ` in case of an international phone number
|
|
197
198
|
getFullNumber(formattedNationalNumber) {
|
|
198
199
|
if (this.isInternational()) {
|
|
199
|
-
const prefix = (text) => this.formatter.getInternationalPrefixBeforeCountryCallingCode(this.state
|
|
200
|
+
const prefix = (text) => this.formatter.getInternationalPrefixBeforeCountryCallingCode(this.state, {
|
|
200
201
|
spacing: text ? true : false
|
|
201
202
|
}) + text
|
|
202
203
|
const { callingCode } = this.state
|
|
@@ -251,7 +252,7 @@ export default class AsYouType {
|
|
|
251
252
|
// entered so far based on the country phone code
|
|
252
253
|
// and the national phone number.
|
|
253
254
|
determineTheCountry() {
|
|
254
|
-
this.state.setCountry(
|
|
255
|
+
this.state.setCountry(getCountryByCallingCode(
|
|
255
256
|
this.isInternational() ? this.state.callingCode : this.defaultCallingCode,
|
|
256
257
|
this.state.nationalSignificantNumber,
|
|
257
258
|
this.metadata
|
|
@@ -283,25 +284,6 @@ export default class AsYouType {
|
|
|
283
284
|
}
|
|
284
285
|
const countryCode = this.getCountry()
|
|
285
286
|
const callingCode = this.getCountryCallingCode() || this.defaultCallingCode
|
|
286
|
-
// Google's AsYouType formatter supports sort of an "autocorrection" feature
|
|
287
|
-
// when it "autocorrects" numbers that have been input for a country
|
|
288
|
-
// with that country's calling code.
|
|
289
|
-
// Such "autocorrection" feature looks weird, but different people have been requesting it:
|
|
290
|
-
// https://github.com/catamphetamine/libphonenumber-js/issues/376
|
|
291
|
-
// https://github.com/catamphetamine/libphonenumber-js/issues/375
|
|
292
|
-
// https://github.com/catamphetamine/libphonenumber-js/issues/316
|
|
293
|
-
if (!this.isInternational() && nationalSignificantNumber === this.state.digits) {
|
|
294
|
-
const autoCorrected = autoCorrectInternationalNumberWithoutPlus(
|
|
295
|
-
this.state.digits,
|
|
296
|
-
countryCode,
|
|
297
|
-
callingCode,
|
|
298
|
-
this.metadata
|
|
299
|
-
)
|
|
300
|
-
if (autoCorrected) {
|
|
301
|
-
nationalSignificantNumber = autoCorrected.nationalNumber
|
|
302
|
-
carrierCode = autoCorrected.carrierCode
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
287
|
const phoneNumber = new PhoneNumber(
|
|
306
288
|
countryCode || callingCode,
|
|
307
289
|
nationalSignificantNumber,
|
|
@@ -349,6 +331,14 @@ export default class AsYouType {
|
|
|
349
331
|
return this.state.nationalSignificantNumber
|
|
350
332
|
}
|
|
351
333
|
|
|
334
|
+
/**
|
|
335
|
+
* Returns the phone number characters entered by the user.
|
|
336
|
+
* @return {string}
|
|
337
|
+
*/
|
|
338
|
+
getChars() {
|
|
339
|
+
return (this.state.international ? '+' : '') + this.state.digits
|
|
340
|
+
}
|
|
341
|
+
|
|
352
342
|
/**
|
|
353
343
|
* Returns the template for the formatted phone number.
|
|
354
344
|
* @return {string}
|
package/source/AsYouType.test.js
CHANGED
|
@@ -9,7 +9,7 @@ class AsYouType extends AsYouType_ {
|
|
|
9
9
|
|
|
10
10
|
const USE_NON_GEOGRAPHIC_COUNTRY_CODE = false
|
|
11
11
|
|
|
12
|
-
describe('
|
|
12
|
+
describe('AsYouType', () => {
|
|
13
13
|
it('should use "national_prefix_formatting_rule"', () => {
|
|
14
14
|
// With national prefix (full).
|
|
15
15
|
new AsYouType('RU').input('88005553535').should.equal('8 (800) 555-35-35')
|
|
@@ -884,13 +884,13 @@ describe('as you type', () => {
|
|
|
884
884
|
it('should return PhoneNumber with autocorrected international numbers without leading +', () => {
|
|
885
885
|
// https://github.com/catamphetamine/libphonenumber-js/issues/316
|
|
886
886
|
const formatter = new AsYouType('FR')
|
|
887
|
-
formatter.input('33612902554').should.equal('
|
|
887
|
+
formatter.input('33612902554').should.equal('33 6 12 90 25 54')
|
|
888
888
|
formatter.getNumber().country.should.equal('FR')
|
|
889
889
|
formatter.getNumber().nationalNumber.should.equal('612902554')
|
|
890
890
|
formatter.getNumber().number.should.equal('+33612902554')
|
|
891
891
|
// Should also strip national prefix.
|
|
892
892
|
formatter.reset()
|
|
893
|
-
formatter.input('330612902554').should.equal('
|
|
893
|
+
formatter.input('330612902554').should.equal('33 06 12 90 25 54')
|
|
894
894
|
formatter.getNumber().country.should.equal('FR')
|
|
895
895
|
formatter.getNumber().nationalNumber.should.equal('612902554')
|
|
896
896
|
formatter.getNumber().number.should.equal('+33612902554')
|
|
@@ -1045,6 +1045,16 @@ describe('as you type', () => {
|
|
|
1045
1045
|
formatter.input('1').should.equal('14880011 1 21')
|
|
1046
1046
|
formatter.input('3').should.equal('14880011 1 213')
|
|
1047
1047
|
})
|
|
1048
|
+
|
|
1049
|
+
it('should return the phone number characters entered by the user', () => {
|
|
1050
|
+
const formatter = new AsYouType('RU')
|
|
1051
|
+
formatter.getChars().should.equal('')
|
|
1052
|
+
formatter.input('+123')
|
|
1053
|
+
formatter.getChars().should.equal('+123')
|
|
1054
|
+
formatter.reset()
|
|
1055
|
+
formatter.input('123')
|
|
1056
|
+
formatter.getChars().should.equal('123')
|
|
1057
|
+
})
|
|
1048
1058
|
})
|
|
1049
1059
|
|
|
1050
1060
|
function type(something) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import checkNumberLength from './checkNumberLength'
|
|
2
|
-
import parseDigits from './parseDigits'
|
|
3
|
-
import
|
|
1
|
+
import checkNumberLength from './helpers/checkNumberLength'
|
|
2
|
+
import parseDigits from './helpers/parseDigits'
|
|
3
|
+
import formatNationalNumberUsingFormat from './helpers/formatNationalNumberUsingFormat'
|
|
4
4
|
|
|
5
5
|
export default function formatCompleteNumber(state, format, {
|
|
6
6
|
metadata,
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import parseDigits from './parseDigits'
|
|
2
|
-
|
|
3
1
|
import {
|
|
4
2
|
DIGIT_PLACEHOLDER,
|
|
5
3
|
countOccurences,
|
|
@@ -10,20 +8,15 @@ import {
|
|
|
10
8
|
populateTemplateWithDigits
|
|
11
9
|
} from './AsYouTypeFormatter.util'
|
|
12
10
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
} from './AsYouTypeFormatter.
|
|
16
|
-
|
|
17
|
-
import formatCompleteNumber, { canFormatCompleteNumber } from './AsYouTypeFormatter.complete'
|
|
11
|
+
import formatCompleteNumber, {
|
|
12
|
+
canFormatCompleteNumber
|
|
13
|
+
} from './AsYouTypeFormatter.complete'
|
|
18
14
|
|
|
19
|
-
import
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
} from './
|
|
23
|
-
|
|
24
|
-
import {
|
|
25
|
-
VALID_PUNCTUATION
|
|
26
|
-
} from './constants'
|
|
15
|
+
import parseDigits from './helpers/parseDigits'
|
|
16
|
+
export { DIGIT_PLACEHOLDER } from './AsYouTypeFormatter.util'
|
|
17
|
+
import { FIRST_GROUP_PATTERN } from './helpers/formatNationalNumberUsingFormat'
|
|
18
|
+
import { VALID_PUNCTUATION } from './constants'
|
|
19
|
+
import applyInternationalSeparatorStyle from './helpers/applyInternationalSeparatorStyle'
|
|
27
20
|
|
|
28
21
|
// Used in phone number format template creation.
|
|
29
22
|
// Could be any digit, I guess.
|
|
@@ -412,10 +405,14 @@ export default class AsYouTypeFormatter {
|
|
|
412
405
|
return ''
|
|
413
406
|
}
|
|
414
407
|
|
|
415
|
-
getInternationalPrefixBeforeCountryCallingCode(IDDPrefix, options) {
|
|
416
|
-
|
|
417
|
-
options && options.spacing === false ? IDDPrefix : IDDPrefix + ' '
|
|
418
|
-
|
|
408
|
+
getInternationalPrefixBeforeCountryCallingCode({ IDDPrefix, missingPlus }, options) {
|
|
409
|
+
if (IDDPrefix) {
|
|
410
|
+
return options && options.spacing === false ? IDDPrefix : IDDPrefix + ' '
|
|
411
|
+
}
|
|
412
|
+
if (missingPlus) {
|
|
413
|
+
return ''
|
|
414
|
+
}
|
|
415
|
+
return '+'
|
|
419
416
|
}
|
|
420
417
|
|
|
421
418
|
getTemplate(state) {
|
|
@@ -427,7 +424,7 @@ export default class AsYouTypeFormatter {
|
|
|
427
424
|
// so trim all non-populated digits.
|
|
428
425
|
let index = -1
|
|
429
426
|
let i = 0
|
|
430
|
-
const internationalPrefix = state.international ? this.getInternationalPrefixBeforeCountryCallingCode(state
|
|
427
|
+
const internationalPrefix = state.international ? this.getInternationalPrefixBeforeCountryCallingCode(state, { spacing: false }) : ''
|
|
431
428
|
while (i < internationalPrefix.length + state.getDigitsWithoutInternationalPrefix().length) {
|
|
432
429
|
index = this.template.indexOf(DIGIT_PLACEHOLDER, index + 1)
|
|
433
430
|
i++
|
|
@@ -435,11 +432,7 @@ export default class AsYouTypeFormatter {
|
|
|
435
432
|
return cutAndStripNonPairedParens(this.template, index + 1)
|
|
436
433
|
}
|
|
437
434
|
|
|
438
|
-
setNationalNumberTemplate(template, {
|
|
439
|
-
international,
|
|
440
|
-
IDDPrefix,
|
|
441
|
-
callingCode
|
|
442
|
-
}) {
|
|
435
|
+
setNationalNumberTemplate(template, state) {
|
|
443
436
|
this.nationalNumberTemplate = template
|
|
444
437
|
this.populatedNationalNumberTemplate = template
|
|
445
438
|
// With a new formatting template, the matched position
|
|
@@ -450,10 +443,10 @@ export default class AsYouTypeFormatter {
|
|
|
450
443
|
// if the phone number being input is international:
|
|
451
444
|
// 'x' for the '+' sign, 'x'es for the country phone code,
|
|
452
445
|
// a spacebar and then the template for the formatted national number.
|
|
453
|
-
if (international) {
|
|
446
|
+
if (state.international) {
|
|
454
447
|
this.template =
|
|
455
|
-
this.getInternationalPrefixBeforeCountryCallingCode(
|
|
456
|
-
repeat(DIGIT_PLACEHOLDER, callingCode.length) +
|
|
448
|
+
this.getInternationalPrefixBeforeCountryCallingCode(state).replace(/[\d\+]/g, DIGIT_PLACEHOLDER) +
|
|
449
|
+
repeat(DIGIT_PLACEHOLDER, state.callingCode.length) +
|
|
457
450
|
' ' +
|
|
458
451
|
template
|
|
459
452
|
} else {
|
|
@@ -1,13 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
extractCountryCallingCode,
|
|
7
|
-
extractCountryCallingCodeFromInternationalNumberWithoutPlusSign
|
|
8
|
-
} from './parse_'
|
|
9
|
-
|
|
10
|
-
import { stripIDDPrefix } from './IDD'
|
|
1
|
+
import extractCountryCallingCode from './helpers/extractCountryCallingCode'
|
|
2
|
+
import extractCountryCallingCodeFromInternationalNumberWithoutPlusSign from './helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign'
|
|
3
|
+
import extractNationalNumberFromPossiblyIncompleteNumber from './helpers/extractNationalNumberFromPossiblyIncompleteNumber'
|
|
4
|
+
import stripIddPrefix from './helpers/stripIddPrefix'
|
|
5
|
+
import parseDigits from './helpers/parseDigits'
|
|
11
6
|
|
|
12
7
|
import {
|
|
13
8
|
VALID_DIGITS,
|
|
@@ -113,7 +108,7 @@ export default class AsYouTypeParser {
|
|
|
113
108
|
// An IDD prefix is extracted here, and then every time when
|
|
114
109
|
// there's a new digit and the number couldn't be formatted.
|
|
115
110
|
if (hasReceivedThreeLeadingDigits) {
|
|
116
|
-
this.
|
|
111
|
+
this.extractIddPrefix(state)
|
|
117
112
|
}
|
|
118
113
|
|
|
119
114
|
if (this.isWaitingForCountryCallingCode(state)) {
|
|
@@ -342,12 +337,24 @@ export default class AsYouTypeParser {
|
|
|
342
337
|
// but it's not a big deal, and in most cases there
|
|
343
338
|
// will be a suitable `format` when there're 3 leading digits.
|
|
344
339
|
//
|
|
345
|
-
if (this.
|
|
340
|
+
if (this.extractIddPrefix(state)) {
|
|
341
|
+
this.extractCallingCodeAndNationalSignificantNumber(state)
|
|
342
|
+
return true
|
|
343
|
+
}
|
|
344
|
+
// Google's AsYouType formatter supports sort of an "autocorrection" feature
|
|
345
|
+
// when it "autocorrects" numbers that have been input for a country
|
|
346
|
+
// with that country's calling code.
|
|
347
|
+
// Such "autocorrection" feature looks weird, but different people have been requesting it:
|
|
348
|
+
// https://github.com/catamphetamine/libphonenumber-js/issues/376
|
|
349
|
+
// https://github.com/catamphetamine/libphonenumber-js/issues/375
|
|
350
|
+
// https://github.com/catamphetamine/libphonenumber-js/issues/316
|
|
351
|
+
if (this.fixMissingPlus(state)) {
|
|
352
|
+
this.extractCallingCodeAndNationalSignificantNumber(state)
|
|
346
353
|
return true
|
|
347
354
|
}
|
|
348
355
|
}
|
|
349
356
|
|
|
350
|
-
|
|
357
|
+
extractIddPrefix(state) {
|
|
351
358
|
// An IDD prefix can't be present in a number written with a `+`.
|
|
352
359
|
// Also, don't re-extract an IDD prefix if has already been extracted.
|
|
353
360
|
const {
|
|
@@ -363,7 +370,7 @@ export default class AsYouTypeParser {
|
|
|
363
370
|
// dialing format instead of using the leading `+`.
|
|
364
371
|
// https://github.com/catamphetamine/libphonenumber-js/issues/185
|
|
365
372
|
// Detect such numbers.
|
|
366
|
-
const numberWithoutIDD =
|
|
373
|
+
const numberWithoutIDD = stripIddPrefix(
|
|
367
374
|
digits,
|
|
368
375
|
this.defaultCountry,
|
|
369
376
|
this.defaultCallingCode,
|
|
@@ -375,16 +382,53 @@ export default class AsYouTypeParser {
|
|
|
375
382
|
state.update({
|
|
376
383
|
IDDPrefix: digits.slice(0, digits.length - numberWithoutIDD.length)
|
|
377
384
|
})
|
|
378
|
-
|
|
379
|
-
// If a national (significant) number has been extracted before, reset it.
|
|
380
|
-
if (nationalSignificantNumber) {
|
|
381
|
-
state.resetNationalSignificantNumber()
|
|
382
|
-
this.onNationalSignificantNumberChange()
|
|
383
|
-
this.hasExtractedNationalSignificantNumber = undefined
|
|
384
|
-
}
|
|
385
|
+
this.startInternationalNumber(state)
|
|
385
386
|
return true
|
|
386
387
|
}
|
|
387
388
|
}
|
|
389
|
+
|
|
390
|
+
fixMissingPlus(state) {
|
|
391
|
+
if (!state.international) {
|
|
392
|
+
const {
|
|
393
|
+
countryCallingCode: newCallingCode,
|
|
394
|
+
number
|
|
395
|
+
} = extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(
|
|
396
|
+
state.digits,
|
|
397
|
+
this.defaultCountry,
|
|
398
|
+
this.defaultCallingCode,
|
|
399
|
+
this.metadata.metadata
|
|
400
|
+
)
|
|
401
|
+
if (newCallingCode) {
|
|
402
|
+
state.update({
|
|
403
|
+
missingPlus: true
|
|
404
|
+
})
|
|
405
|
+
this.startInternationalNumber(state)
|
|
406
|
+
return true
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
startInternationalNumber(state) {
|
|
412
|
+
state.startInternationalNumber()
|
|
413
|
+
// If a national (significant) number has been extracted before, reset it.
|
|
414
|
+
if (state.nationalSignificantNumber) {
|
|
415
|
+
state.resetNationalSignificantNumber()
|
|
416
|
+
this.onNationalSignificantNumberChange()
|
|
417
|
+
this.hasExtractedNationalSignificantNumber = undefined
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
extractCallingCodeAndNationalSignificantNumber(state) {
|
|
422
|
+
if (this.extractCountryCallingCode(state)) {
|
|
423
|
+
// `this.extractCallingCode()` is currently called when the number
|
|
424
|
+
// couldn't be formatted during the standard procedure.
|
|
425
|
+
// Normally, the national prefix would be re-extracted
|
|
426
|
+
// for an international number if such number couldn't be formatted,
|
|
427
|
+
// but since it's already not able to be formatted,
|
|
428
|
+
// there won't be yet another retry, so also extract national prefix here.
|
|
429
|
+
this.extractNationalSignificantNumber(state.getNationalDigits(), state.update)
|
|
430
|
+
}
|
|
431
|
+
}
|
|
388
432
|
}
|
|
389
433
|
|
|
390
434
|
/**
|
|
@@ -444,31 +488,4 @@ export function extractFormattedDigitsAndPlus(text) {
|
|
|
444
488
|
formattedDigits = ''
|
|
445
489
|
}
|
|
446
490
|
return [formattedDigits, hasPlus]
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
export function autoCorrectInternationalNumberWithoutPlus(
|
|
450
|
-
digits,
|
|
451
|
-
countryCode,
|
|
452
|
-
callingCode,
|
|
453
|
-
metadata
|
|
454
|
-
) {
|
|
455
|
-
// Google's AsYouType formatter supports sort of an "autocorrection" feature
|
|
456
|
-
// when it "autocorrects" numbers that have been input for a country
|
|
457
|
-
// with that country's calling code.
|
|
458
|
-
// Such "autocorrection" feature looks weird, but different people have been requesting it:
|
|
459
|
-
// https://github.com/catamphetamine/libphonenumber-js/issues/376
|
|
460
|
-
// https://github.com/catamphetamine/libphonenumber-js/issues/375
|
|
461
|
-
// https://github.com/catamphetamine/libphonenumber-js/issues/316
|
|
462
|
-
const {
|
|
463
|
-
countryCallingCode: newCallingCode,
|
|
464
|
-
number
|
|
465
|
-
} = extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(
|
|
466
|
-
digits,
|
|
467
|
-
countryCode,
|
|
468
|
-
callingCode,
|
|
469
|
-
metadata.metadata
|
|
470
|
-
)
|
|
471
|
-
if (newCallingCode) {
|
|
472
|
-
return extractNationalNumber(number, metadata)
|
|
473
|
-
}
|
|
474
491
|
}
|
package/source/AsYouTypeState.js
CHANGED
package/source/PhoneNumber.js
CHANGED
|
@@ -2,7 +2,7 @@ import Metadata from './metadata'
|
|
|
2
2
|
import isPossibleNumber from './isPossibleNumber_'
|
|
3
3
|
import isValidNumber from './validate_'
|
|
4
4
|
import isValidNumberForRegion from './isValidNumberForRegion_'
|
|
5
|
-
import getNumberType from './
|
|
5
|
+
import getNumberType from './helpers/getNumberType'
|
|
6
6
|
import formatNumber from './format_'
|
|
7
7
|
|
|
8
8
|
const USE_NON_GEOGRAPHIC_COUNTRY_CODE = false
|
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
VALID_PUNCTUATION
|
|
13
13
|
} from './constants'
|
|
14
14
|
|
|
15
|
-
import
|
|
15
|
+
import createExtensionPattern from './helpers/extension/createExtensionPattern'
|
|
16
16
|
|
|
17
17
|
import RegExpCache from './findNumbers/RegExpCache'
|
|
18
18
|
|
|
@@ -38,6 +38,8 @@ import { isSupportedCountry } from './metadata'
|
|
|
38
38
|
|
|
39
39
|
import parseNumber from './parse_'
|
|
40
40
|
|
|
41
|
+
const EXTN_PATTERNS_FOR_MATCHING = createExtensionPattern('matching')
|
|
42
|
+
|
|
41
43
|
/**
|
|
42
44
|
* Patterns used to extract phone numbers from a larger phone-number-like pattern. These are
|
|
43
45
|
* ordered according to specificity. For example, white-space is last since that is frequently
|
|
@@ -209,21 +211,18 @@ export default class PhoneNumberMatcher
|
|
|
209
211
|
* @param index the search index to start searching at
|
|
210
212
|
* @return the phone number match found, null if none can be found
|
|
211
213
|
*/
|
|
212
|
-
find()
|
|
213
|
-
{
|
|
214
|
+
find() {
|
|
214
215
|
// // Reset the regular expression.
|
|
215
216
|
// this.PATTERN.lastIndex = index
|
|
216
217
|
|
|
217
218
|
let matches
|
|
218
|
-
while ((this.maxTries > 0) && (matches = this.PATTERN.exec(this.text)) !== null)
|
|
219
|
-
{
|
|
219
|
+
while ((this.maxTries > 0) && (matches = this.PATTERN.exec(this.text)) !== null) {
|
|
220
220
|
let candidate = matches[0]
|
|
221
221
|
const offset = matches.index
|
|
222
222
|
|
|
223
223
|
candidate = parsePreCandidate(candidate)
|
|
224
224
|
|
|
225
|
-
if (isValidPreCandidate(candidate, offset, this.text))
|
|
226
|
-
{
|
|
225
|
+
if (isValidPreCandidate(candidate, offset, this.text)) {
|
|
227
226
|
const match =
|
|
228
227
|
// Try to come up with a valid match given the entire candidate.
|
|
229
228
|
this.parseAndVerify(candidate, offset, this.text)
|
|
@@ -256,28 +255,24 @@ export default class PhoneNumberMatcher
|
|
|
256
255
|
}
|
|
257
256
|
|
|
258
257
|
/**
|
|
259
|
-
* Attempts to extract a match from `
|
|
260
|
-
* if the
|
|
258
|
+
* Attempts to extract a match from `substring`
|
|
259
|
+
* if the substring itself does not qualify as a match.
|
|
261
260
|
*/
|
|
262
|
-
extractInnerMatch(
|
|
263
|
-
|
|
264
|
-
for (const innerMatchPattern of INNER_MATCHES)
|
|
265
|
-
{
|
|
261
|
+
extractInnerMatch(substring, offset, text) {
|
|
262
|
+
for (const innerMatchPattern of INNER_MATCHES) {
|
|
266
263
|
let isFirstMatch = true
|
|
267
|
-
let
|
|
268
|
-
const
|
|
269
|
-
while ((
|
|
270
|
-
|
|
271
|
-
if (isFirstMatch)
|
|
272
|
-
{
|
|
264
|
+
let candidateMatch
|
|
265
|
+
const innerMatchRegExp = new RegExp(innerMatchPattern, 'g')
|
|
266
|
+
while (this.maxTries > 0 && (candidateMatch = innerMatchRegExp.exec(substring)) !== null) {
|
|
267
|
+
if (isFirstMatch) {
|
|
273
268
|
// We should handle any group before this one too.
|
|
274
|
-
const
|
|
275
|
-
(
|
|
269
|
+
const candidate = trimAfterFirstMatch(
|
|
276
270
|
UNWANTED_END_CHAR_PATTERN,
|
|
277
|
-
|
|
271
|
+
substring.slice(0, candidateMatch.index)
|
|
278
272
|
)
|
|
279
273
|
|
|
280
|
-
const match = this.parseAndVerify(
|
|
274
|
+
const match = this.parseAndVerify(candidate, offset, text)
|
|
275
|
+
|
|
281
276
|
if (match) {
|
|
282
277
|
return match
|
|
283
278
|
}
|
|
@@ -286,16 +281,16 @@ export default class PhoneNumberMatcher
|
|
|
286
281
|
isFirstMatch = false
|
|
287
282
|
}
|
|
288
283
|
|
|
289
|
-
const
|
|
290
|
-
(
|
|
291
|
-
UNWANTED_END_CHAR_PATTERN,
|
|
292
|
-
matches[1]
|
|
293
|
-
)
|
|
284
|
+
const candidate = trimAfterFirstMatch(UNWANTED_END_CHAR_PATTERN, candidateMatch[1])
|
|
294
285
|
|
|
295
286
|
// Java code does `groupMatcher.start(1)` here,
|
|
296
|
-
// but there's no way in javascript to get a
|
|
297
|
-
// therefore using
|
|
298
|
-
|
|
287
|
+
// but there's no way in javascript to get a `candidate` start index,
|
|
288
|
+
// therefore resort to using this kind of an approximation.
|
|
289
|
+
// (`groupMatcher` is called `candidateInSubstringMatch` in this javascript port)
|
|
290
|
+
// https://stackoverflow.com/questions/15934353/get-index-of-each-capture-in-a-javascript-regex
|
|
291
|
+
const candidateIndexGuess = substring.indexOf(candidate, candidateMatch.index)
|
|
292
|
+
|
|
293
|
+
const match = this.parseAndVerify(candidate, offset + candidateIndexGuess, text)
|
|
299
294
|
if (match) {
|
|
300
295
|
return match
|
|
301
296
|
}
|
|
@@ -314,8 +309,7 @@ export default class PhoneNumberMatcher
|
|
|
314
309
|
* @param offset the offset of {@code candidate} within {@link #text}
|
|
315
310
|
* @return the parsed and validated phone number match, or null
|
|
316
311
|
*/
|
|
317
|
-
parseAndVerify(candidate, offset, text)
|
|
318
|
-
{
|
|
312
|
+
parseAndVerify(candidate, offset, text) {
|
|
319
313
|
if (!isValidCandidate(candidate, offset, text, this.options.leniency)) {
|
|
320
314
|
return
|
|
321
315
|
}
|
|
@@ -333,8 +327,7 @@ export default class PhoneNumberMatcher
|
|
|
333
327
|
return
|
|
334
328
|
}
|
|
335
329
|
|
|
336
|
-
if (this.leniency(number, candidate, this.metadata, this.regExpCache))
|
|
337
|
-
{
|
|
330
|
+
if (this.leniency(number, candidate, this.metadata, this.regExpCache)) {
|
|
338
331
|
// // We used parseAndKeepRawInput to create this number,
|
|
339
332
|
// // but for now we don't return the extra values parsed.
|
|
340
333
|
// // TODO: stop clearing all values here and switch all users over
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import findNumbers from './findNumbers'
|
|
2
|
-
import metadata from '../metadata.
|
|
2
|
+
import metadata from '../metadata.full.json'
|
|
3
3
|
|
|
4
4
|
describe('findNumbers', () => {
|
|
5
5
|
it('should find numbers', () => {
|
|
@@ -188,4 +188,17 @@ describe('findNumbers', () => {
|
|
|
188
188
|
// Shouldn't parse by default.
|
|
189
189
|
findNumbers('The UUID is CA801c26f98cd16e231354125ad046e40b.', 'FR', metadata).should.deep.equal([])
|
|
190
190
|
})
|
|
191
|
+
|
|
192
|
+
// https://gitlab.com/catamphetamine/libphonenumber-js/-/merge_requests/4
|
|
193
|
+
it('should return correct `startsAt` and `endsAt` when matching "inner" candidates in a could-be-a-candidate substring', () => {
|
|
194
|
+
findNumbers('39945926 77200596 16533084', 'ID', metadata)
|
|
195
|
+
.should
|
|
196
|
+
.deep
|
|
197
|
+
.equal([{
|
|
198
|
+
country: 'ID',
|
|
199
|
+
phone: '77200596',
|
|
200
|
+
startsAt: 9,
|
|
201
|
+
endsAt: 17
|
|
202
|
+
}])
|
|
203
|
+
})
|
|
191
204
|
})
|
|
@@ -8,14 +8,31 @@ import {
|
|
|
8
8
|
WHITESPACE
|
|
9
9
|
} from './constants'
|
|
10
10
|
|
|
11
|
-
import { EXTN_PATTERNS_FOR_PARSING } from './extension'
|
|
12
11
|
import parse from './parse_'
|
|
13
|
-
import { VALID_PHONE_NUMBER_WITH_EXTENSION } from './isViablePhoneNumber'
|
|
12
|
+
import { VALID_PHONE_NUMBER_WITH_EXTENSION } from './helpers/isViablePhoneNumber'
|
|
13
|
+
import createExtensionPattern from './helpers/extension/createExtensionPattern'
|
|
14
14
|
|
|
15
15
|
import parsePreCandidate from './findNumbers/parsePreCandidate'
|
|
16
16
|
import isValidPreCandidate from './findNumbers/isValidPreCandidate'
|
|
17
17
|
import isValidCandidate from './findNumbers/isValidCandidate'
|
|
18
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Regexp of all possible ways to write extensions, for use when parsing. This
|
|
21
|
+
* will be run as a case-insensitive regexp match. Wide character versions are
|
|
22
|
+
* also provided after each ASCII version. There are three regular expressions
|
|
23
|
+
* here. The first covers RFC 3966 format, where the extension is added using
|
|
24
|
+
* ';ext='. The second more generic one starts with optional white space and
|
|
25
|
+
* ends with an optional full stop (.), followed by zero or more spaces/tabs
|
|
26
|
+
* /commas and then the numbers themselves. The other one covers the special
|
|
27
|
+
* case of American numbers where the extension is written with a hash at the
|
|
28
|
+
* end, such as '- 503#'. Note that the only capturing groups should be around
|
|
29
|
+
* the digits that you want to capture as part of the extension, or else parsing
|
|
30
|
+
* will fail! We allow two options for representing the accented o - the
|
|
31
|
+
* character itself, and one in the unicode decomposed form with the combining
|
|
32
|
+
* acute accent.
|
|
33
|
+
*/
|
|
34
|
+
export const EXTN_PATTERNS_FOR_PARSING = createExtensionPattern('parsing')
|
|
35
|
+
|
|
19
36
|
const WHITESPACE_IN_THE_BEGINNING_PATTERN = new RegExp('^[' + WHITESPACE + ']+')
|
|
20
37
|
const PUNCTUATION_IN_THE_END_PATTERN = new RegExp('[' + VALID_PUNCTUATION + ']+$')
|
|
21
38
|
|
package/source/format.test.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import metadata from '../metadata.min.json'
|
|
2
|
-
import { applyInternationalSeparatorStyle } from './format_'
|
|
3
2
|
import _formatNumber from './format'
|
|
4
3
|
|
|
5
4
|
function formatNumber(...parameters) {
|
|
@@ -116,11 +115,6 @@ describe('format', () => {
|
|
|
116
115
|
expect(() => formatNumber({ phone: '123', country: 'USA' }, 'NATIONAL')).to.throw('Unknown country')
|
|
117
116
|
})
|
|
118
117
|
|
|
119
|
-
it('should change Google\'s international format style', () => {
|
|
120
|
-
applyInternationalSeparatorStyle('(xxx) xxx-xx-xx').should.equal('xxx xxx xx xx')
|
|
121
|
-
applyInternationalSeparatorStyle('(xxx)xxx').should.equal('xxx xxx')
|
|
122
|
-
})
|
|
123
|
-
|
|
124
118
|
it('should format phone number extensions', () => {
|
|
125
119
|
// National
|
|
126
120
|
formatNumber({
|
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
|
|
7
7
|
import Metadata from './metadata'
|
|
8
8
|
import format from './format_'
|
|
9
|
-
import getNumberType from './
|
|
10
|
-
import checkNumberLength from './checkNumberLength'
|
|
9
|
+
import getNumberType from './helpers/getNumberType'
|
|
10
|
+
import checkNumberLength from './helpers/checkNumberLength'
|
|
11
11
|
import getCountryCallingCode from './getCountryCallingCode'
|
|
12
12
|
|
|
13
13
|
const REGION_CODE_FOR_NON_GEO_ENTITY = '001'
|