libphonenumber-js 1.13.3 → 1.13.5
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/.gitlab-ci.yml +24 -9
- package/CHANGELOG.md +20 -15
- package/README.md +27 -10
- package/build/AsYouType.js +100 -27
- package/build/AsYouType.js.map +1 -1
- package/build/AsYouType.test.js.map +1 -1
- package/build/AsYouTypeFormatter.complete.js +2 -2
- package/build/AsYouTypeFormatter.complete.js.map +1 -1
- package/build/AsYouTypeFormatter.js +1 -1
- package/build/AsYouTypeFormatter.js.map +1 -1
- package/build/AsYouTypeState.js +15 -1
- package/build/AsYouTypeState.js.map +1 -1
- package/build/PhoneNumber.js +26 -39
- package/build/PhoneNumber.js.map +1 -1
- package/build/format.js +3 -3
- package/build/format.js.map +1 -1
- package/build/formatPhoneNumberForMobileDialing.js +4 -3
- package/build/formatPhoneNumberForMobileDialing.js.map +1 -1
- package/build/helpers/checkNumberLength.js +21 -6
- package/build/helpers/checkNumberLength.js.map +1 -1
- package/build/helpers/checkNumberLength.test.js.map +1 -1
- package/build/helpers/extractCountryCallingCode.js +16 -11
- package/build/helpers/extractCountryCallingCode.js.map +1 -1
- package/build/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +21 -10
- package/build/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map +1 -1
- package/build/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.test.js.map +1 -0
- package/build/helpers/extractNationalNumber.js +12 -3
- package/build/helpers/extractNationalNumber.js.map +1 -1
- package/build/helpers/extractNationalNumber.test.js.map +1 -1
- package/build/helpers/getCountryAndCallingCodeFromOneOfThem.js +35 -0
- package/build/helpers/getCountryAndCallingCodeFromOneOfThem.js.map +1 -0
- package/build/helpers/getCountryByCallingCode.js +10 -7
- package/build/helpers/getCountryByCallingCode.js.map +1 -1
- package/build/helpers/getCountryByNationalNumber.js +13 -9
- package/build/helpers/getCountryByNationalNumber.js.map +1 -1
- package/build/helpers/getIddPrefix.js +1 -1
- package/build/helpers/getIddPrefix.js.map +1 -1
- package/build/helpers/getNumberType.js +3 -3
- package/build/helpers/getNumberType.js.map +1 -1
- package/build/helpers/getPossibleCountriesForNumber.js +4 -7
- package/build/helpers/getPossibleCountriesForNumber.js.map +1 -1
- package/build/helpers/isCallingCode.js +11 -0
- package/build/helpers/isCallingCode.js.map +1 -0
- package/build/helpers/isCallingCode.test.js.map +1 -0
- package/build/helpers/isCountryCode.js +11 -0
- package/build/helpers/isCountryCode.js.map +1 -0
- package/build/helpers/isCountryCode.test.js.map +1 -0
- package/build/helpers/stripIddPrefix.js +9 -4
- package/build/helpers/stripIddPrefix.js.map +1 -1
- package/build/isPossible.js +14 -8
- package/build/isPossible.js.map +1 -1
- package/build/isValid.js +3 -3
- package/build/isValid.js.map +1 -1
- package/build/metadata.js +80 -31
- package/build/metadata.js.map +1 -1
- package/build/metadata.test.js.map +1 -1
- package/build/parse.js +36 -12
- package/build/parse.js.map +1 -1
- package/build/validatePhoneNumberLength.js +3 -3
- package/build/validatePhoneNumberLength.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.d.cts +1 -0
- package/core/index.d.ts +1 -0
- package/custom.d.ts +3 -1
- package/es6/AsYouType.js +100 -27
- package/es6/AsYouType.js.map +1 -1
- package/es6/AsYouType.test.js.map +1 -1
- package/es6/AsYouTypeFormatter.complete.js +2 -2
- package/es6/AsYouTypeFormatter.complete.js.map +1 -1
- package/es6/AsYouTypeFormatter.js +1 -1
- package/es6/AsYouTypeFormatter.js.map +1 -1
- package/es6/AsYouTypeState.js +15 -1
- package/es6/AsYouTypeState.js.map +1 -1
- package/es6/PhoneNumber.js +26 -39
- package/es6/PhoneNumber.js.map +1 -1
- package/es6/format.js +3 -3
- package/es6/format.js.map +1 -1
- package/es6/formatPhoneNumberForMobileDialing.js +4 -3
- package/es6/formatPhoneNumberForMobileDialing.js.map +1 -1
- package/es6/helpers/checkNumberLength.js +22 -6
- package/es6/helpers/checkNumberLength.js.map +1 -1
- package/es6/helpers/checkNumberLength.test.js.map +1 -1
- package/es6/helpers/extractCountryCallingCode.js +16 -11
- package/es6/helpers/extractCountryCallingCode.js.map +1 -1
- package/es6/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +21 -10
- package/es6/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map +1 -1
- package/es6/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.test.js.map +1 -0
- package/es6/helpers/extractNationalNumber.js +12 -3
- package/es6/helpers/extractNationalNumber.js.map +1 -1
- package/es6/helpers/extractNationalNumber.test.js.map +1 -1
- package/es6/helpers/getCountryAndCallingCodeFromOneOfThem.js +28 -0
- package/es6/helpers/getCountryAndCallingCodeFromOneOfThem.js.map +1 -0
- package/es6/helpers/getCountryByCallingCode.js +10 -7
- package/es6/helpers/getCountryByCallingCode.js.map +1 -1
- package/es6/helpers/getCountryByNationalNumber.js +13 -9
- package/es6/helpers/getCountryByNationalNumber.js.map +1 -1
- package/es6/helpers/getIddPrefix.js +1 -1
- package/es6/helpers/getIddPrefix.js.map +1 -1
- package/es6/helpers/getNumberType.js +3 -3
- package/es6/helpers/getNumberType.js.map +1 -1
- package/es6/helpers/getPossibleCountriesForNumber.js +4 -7
- package/es6/helpers/getPossibleCountriesForNumber.js.map +1 -1
- package/es6/helpers/isCallingCode.js +5 -0
- package/es6/helpers/isCallingCode.js.map +1 -0
- package/es6/helpers/isCallingCode.test.js.map +1 -0
- package/es6/helpers/isCountryCode.js +5 -0
- package/es6/helpers/isCountryCode.js.map +1 -0
- package/es6/helpers/isCountryCode.test.js.map +1 -0
- package/es6/helpers/stripIddPrefix.js +9 -4
- package/es6/helpers/stripIddPrefix.js.map +1 -1
- package/es6/isPossible.js +14 -8
- package/es6/isPossible.js.map +1 -1
- package/es6/isValid.js +3 -3
- package/es6/isValid.js.map +1 -1
- package/es6/metadata.js +80 -31
- package/es6/metadata.js.map +1 -1
- package/es6/metadata.test.js.map +1 -1
- package/es6/parse.js +36 -12
- package/es6/parse.js.map +1 -1
- package/es6/validatePhoneNumberLength.js +3 -3
- package/es6/validatePhoneNumberLength.js.map +1 -1
- package/es6-modern/AsYouType.js +100 -26
- package/es6-modern/AsYouType.js.map +1 -1
- package/es6-modern/AsYouType.test.js.map +1 -1
- package/es6-modern/AsYouTypeFormatter.complete.js +2 -2
- package/es6-modern/AsYouTypeFormatter.complete.js.map +1 -1
- package/es6-modern/AsYouTypeFormatter.js +1 -1
- package/es6-modern/AsYouTypeFormatter.js.map +1 -1
- package/es6-modern/AsYouTypeState.js +15 -1
- package/es6-modern/AsYouTypeState.js.map +1 -1
- package/es6-modern/PhoneNumber.js +25 -36
- package/es6-modern/PhoneNumber.js.map +1 -1
- package/es6-modern/format.js +3 -3
- package/es6-modern/format.js.map +1 -1
- package/es6-modern/formatPhoneNumberForMobileDialing.js +4 -3
- package/es6-modern/formatPhoneNumberForMobileDialing.js.map +1 -1
- package/es6-modern/helpers/checkNumberLength.js +22 -6
- package/es6-modern/helpers/checkNumberLength.js.map +1 -1
- package/es6-modern/helpers/checkNumberLength.test.js.map +1 -1
- package/es6-modern/helpers/extractCountryCallingCode.js +16 -11
- package/es6-modern/helpers/extractCountryCallingCode.js.map +1 -1
- package/es6-modern/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +21 -10
- package/es6-modern/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map +1 -1
- package/es6-modern/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.test.js.map +1 -0
- package/es6-modern/helpers/extractNationalNumber.js +12 -3
- package/es6-modern/helpers/extractNationalNumber.js.map +1 -1
- package/es6-modern/helpers/extractNationalNumber.test.js.map +1 -1
- package/es6-modern/helpers/getCountryAndCallingCodeFromOneOfThem.js +28 -0
- package/es6-modern/helpers/getCountryAndCallingCodeFromOneOfThem.js.map +1 -0
- package/es6-modern/helpers/getCountryByCallingCode.js +10 -7
- package/es6-modern/helpers/getCountryByCallingCode.js.map +1 -1
- package/es6-modern/helpers/getCountryByNationalNumber.js +13 -10
- package/es6-modern/helpers/getCountryByNationalNumber.js.map +1 -1
- package/es6-modern/helpers/getIddPrefix.js +1 -1
- package/es6-modern/helpers/getIddPrefix.js.map +1 -1
- package/es6-modern/helpers/getNumberType.js +3 -3
- package/es6-modern/helpers/getNumberType.js.map +1 -1
- package/es6-modern/helpers/getPossibleCountriesForNumber.js +4 -7
- package/es6-modern/helpers/getPossibleCountriesForNumber.js.map +1 -1
- package/es6-modern/helpers/isCallingCode.js +5 -0
- package/es6-modern/helpers/isCallingCode.js.map +1 -0
- package/es6-modern/helpers/isCallingCode.test.js.map +1 -0
- package/es6-modern/helpers/isCountryCode.js +5 -0
- package/es6-modern/helpers/isCountryCode.js.map +1 -0
- package/es6-modern/helpers/isCountryCode.test.js.map +1 -0
- package/es6-modern/helpers/stripIddPrefix.js +9 -4
- package/es6-modern/helpers/stripIddPrefix.js.map +1 -1
- package/es6-modern/isPossible.js +14 -8
- package/es6-modern/isPossible.js.map +1 -1
- package/es6-modern/isValid.js +3 -3
- package/es6-modern/isValid.js.map +1 -1
- package/es6-modern/metadata.js +78 -31
- package/es6-modern/metadata.js.map +1 -1
- package/es6-modern/metadata.test.js.map +1 -1
- package/es6-modern/parse.js +41 -12
- package/es6-modern/parse.js.map +1 -1
- package/es6-modern/validatePhoneNumberLength.js +3 -3
- package/es6-modern/validatePhoneNumberLength.js.map +1 -1
- package/index.d.cts +1 -0
- package/index.d.ts +1 -0
- package/max/index.d.cts +1 -0
- package/max/index.d.ts +1 -0
- package/min/index.d.cts +1 -0
- package/min/index.d.ts +1 -0
- package/mobile/index.d.cts +1 -0
- package/mobile/index.d.ts +1 -0
- package/package.json +1 -1
- package/source/AsYouType.js +108 -27
- package/source/AsYouTypeFormatter.complete.js +2 -2
- package/source/AsYouTypeFormatter.js +1 -1
- package/source/AsYouTypeState.js +15 -1
- package/source/PhoneNumber.js +24 -39
- package/source/format.js +3 -3
- package/source/formatPhoneNumberForMobileDialing.js +4 -3
- package/source/helpers/checkNumberLength.js +21 -6
- package/source/helpers/extractCountryCallingCode.js +16 -11
- package/source/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +20 -10
- package/source/helpers/extractNationalNumber.js +11 -3
- package/source/helpers/getCountryAndCallingCodeFromOneOfThem.js +31 -0
- package/source/helpers/getCountryByCallingCode.js +10 -7
- package/source/helpers/getCountryByNationalNumber.js +14 -11
- package/source/helpers/getIddPrefix.js +1 -1
- package/source/helpers/getNumberType.js +3 -3
- package/source/helpers/getPossibleCountriesForNumber.js +4 -7
- package/source/helpers/isCallingCode.js +5 -0
- package/source/helpers/isCountryCode.js +5 -0
- package/source/helpers/stripIddPrefix.js +10 -4
- package/source/isPossible.js +13 -7
- package/source/isValid.js +3 -3
- package/source/metadata.js +77 -33
- package/source/parse.js +41 -11
- package/source/validatePhoneNumberLength.js +3 -3
- package/types.d.cts +5 -34
- package/types.d.ts +5 -34
package/es6-modern/format.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"format.js","names":["matchesEntirely","formatNationalNumberUsingFormat","Metadata","getCountryCallingCode","getIddPrefix","formatRFC3966","DEFAULT_OPTIONS","formatExtension","formattedNumber","extension","metadata","ext","formatNumber","input","format","options","merge","country","hasCountry","Error","selectNumberingPlan","countryCallingCode","phone","nationalNumber","v2","number","formatNationalNumber","carrierCode","addExtension","fromCountry","formatIDD","formatAs","chooseFormatForNumber","formats","useInternationalFormat","withNationalPrefix","nationalPrefixIsOptionalWhenFormattingInNationalFormat","nationalPrefix","availableFormats","pickFirstMatchingElement","leadingDigitsPatterns","length","lastLeadingDigitsPattern","search","pattern","fromCountryCallingCode","iddPrefix","undefined","objects","i","key","elements","testFunction"],"sources":["../source/format.js"],"sourcesContent":["// This is a port of Google Android `libphonenumber`'s\r\n// `phonenumberutil.js` of December 31th, 2018.\r\n//\r\n// https://github.com/googlei18n/libphonenumber/commits/master/javascript/i18n/phonenumbers/phonenumberutil.js\r\n\r\nimport matchesEntirely from './helpers/matchesEntirely.js'\r\nimport formatNationalNumberUsingFormat from './helpers/formatNationalNumberUsingFormat.js'\r\nimport Metadata, { getCountryCallingCode } from './metadata.js'\r\nimport getIddPrefix from './helpers/getIddPrefix.js'\r\nimport { formatRFC3966 } from './helpers/RFC3966.js'\r\n\r\nconst DEFAULT_OPTIONS = {\r\n\tformatExtension: (formattedNumber, extension, metadata) => `${formattedNumber}${metadata.ext()}${extension}`\r\n}\r\n\r\n/**\r\n * Formats a phone number.\r\n *\r\n * format(phoneNumberInstance, 'INTERNATIONAL', { ..., v2: true }, metadata)\r\n * format(phoneNumberInstance, 'NATIONAL', { ..., v2: true }, metadata)\r\n *\r\n * format({ phone: '8005553535', country: 'RU' }, 'INTERNATIONAL', { ... }, metadata)\r\n * format({ phone: '8005553535', country: 'RU' }, 'NATIONAL', undefined, metadata)\r\n *\r\n * @param {object|PhoneNumber} input — If `options.v2: true` flag is passed, the `input` should be a `PhoneNumber` instance. Otherwise, it should be an object of shape `{ phone: '...', country: '...' }`.\r\n * @param {string} format\r\n * @param {object} [options]\r\n * @param {object} metadata\r\n * @return {string}\r\n */\r\nexport default function formatNumber(input, format, options, metadata) {\r\n\t// Apply default options.\r\n\tif (options) {\r\n\t\t// Using ES6 \"rest spread\" syntax here didn't work with `babel`/`istanbul`\r\n\t\t// for some weird reason: this line of code would cause the code coverage\r\n\t\t// to show as not 100%. That's because `babel`/`istanbul`, for some weird reason,\r\n\t\t// apparently doesn't know how to properly exclude Babel polyfills from code coverage.\r\n\t\t//\r\n\t\t// options = { ...DEFAULT_OPTIONS, ...options }\r\n\t\t//\r\n\t\toptions = merge({}, DEFAULT_OPTIONS, options)\r\n\t} else {\r\n\t\toptions = DEFAULT_OPTIONS\r\n\t}\r\n\r\n\tmetadata = new Metadata(metadata)\r\n\r\n\t// Normally, the `input` object is supposed to be a `PhoneNumber` class instance.\r\n\t// Also, according to the `PhoneNumber` class source code, `country` can't be \"001\".\r\n\t// It means that normally `input.country` or `input.countryCallingCode` is supposed\r\n\t// to be present because either of the two is always required to exist in a `PhoneNumber` instance.\r\n\t// This means that realistically, it's gonna step into either the first `if`\r\n\t// or the following `else if`, and normally it won't even reach the legacy-compatibility\r\n\t// `else return input.phone || ''` part.\r\n\t// So normally, it won't ever return an empty string here.\r\n\tif (input.country && input.country !== '001') {\r\n\t\t// Validate `input.country`.\r\n\t\tif (!metadata.hasCountry(input.country)) {\r\n\t\t\tthrow new Error(`Unknown country: ${input.country}`)\r\n\t\t}\r\n\t\tmetadata.selectNumberingPlan(input.country)\r\n\t}\r\n\telse if (input.countryCallingCode) {\r\n\t\tmetadata.selectNumberingPlan(input.countryCallingCode)\r\n\t}\r\n\telse return input.phone || ''\r\n\r\n\tconst countryCallingCode = metadata.countryCallingCode()\r\n\r\n\tconst nationalNumber = options.v2 ? input.nationalNumber : input.phone\r\n\r\n\t// This variable should have been declared inside `case`s\r\n\t// but Babel has a bug and it says \"duplicate variable declaration\".\r\n\tlet number\r\n\r\n\tswitch (format) {\r\n\t\tcase 'NATIONAL':\r\n\t\t\t// Normally, the `input` object is supposed to be a `PhoneNumber` class instance,\r\n\t\t\t// and a `PhoneNumber` class instance is always required to have a `nationalNumber`.\r\n\t\t\t// This means that the `if (!nationalNumber)` below is just for legacy-compatibility\r\n\t\t\t// and it normally can't really happen, so normally it won't ever return an empty string here.\r\n\t\t\tif (!nationalNumber) {\r\n\t\t\t\treturn ''\r\n\t\t\t}\r\n\t\t\tnumber = formatNationalNumber(nationalNumber, input.carrierCode, 'NATIONAL', metadata, options)\r\n\t\t\treturn addExtension(number, input.ext, metadata, options.formatExtension)\r\n\r\n\t\tcase 'INTERNATIONAL':\r\n\t\t\t// Legacy argument support.\r\n\t\t\t// (`{ country: ..., phone: '' }`)\r\n\t\t\tif (!nationalNumber) {\r\n\t\t\t\treturn `+${countryCallingCode}`\r\n\t\t\t}\r\n\t\t\tnumber = formatNationalNumber(nationalNumber, null, 'INTERNATIONAL', metadata, options)\r\n\t\t\tnumber = `+${countryCallingCode} ${number}`\r\n\t\t\treturn addExtension(number, input.ext, metadata, options.formatExtension)\r\n\r\n\t\tcase 'E.164':\r\n\t\t\t// `E.164` doesn't define \"phone number extensions\".\r\n\t\t\treturn `+${countryCallingCode}${nationalNumber}`\r\n\r\n\t\tcase 'RFC3966':\r\n\t\t\treturn formatRFC3966({\r\n\t\t\t\tnumber: `+${countryCallingCode}${nationalNumber}`,\r\n\t\t\t\text: input.ext\r\n\t\t\t})\r\n\r\n\t\t// For reference, here's Google's IDD formatter:\r\n\t\t// https://github.com/google/libphonenumber/blob/32719cf74e68796788d1ca45abc85dcdc63ba5b9/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L1546\r\n\t\t// Not saying that this IDD formatter replicates it 1:1, but it seems to work.\r\n\t\t// Who would even need to format phone numbers in IDD format anyway?\r\n\t\tcase 'IDD':\r\n\t\t\t// If the required `fromCountry` parameter is not passed, it will return `undefined`.\r\n\t\t\tif (!options.fromCountry) {\r\n\t\t\t\treturn\r\n\t\t\t\t// throw new Error('`fromCountry` option not passed for IDD-prefixed formatting.')\r\n\t\t\t}\r\n\t\t\tconst formattedNumber = formatIDD(\r\n\t\t\t\tnationalNumber,\r\n\t\t\t\tinput.carrierCode,\r\n\t\t\t\tcountryCallingCode,\r\n\t\t\t\toptions.fromCountry,\r\n\t\t\t\tmetadata\r\n\t\t\t)\r\n\t\t\t// If the country of the phone number doesn't support IDD calling, it will return `undefined`.\r\n\t\t\tif (!formattedNumber) {\r\n\t\t\t\treturn\r\n\t\t\t}\r\n\t\t\treturn addExtension(formattedNumber, input.ext, metadata, options.formatExtension)\r\n\r\n\t\tdefault:\r\n\t\t\tthrow new Error(`Unknown \"format\" argument passed to \"formatNumber()\": \"${format}\"`)\r\n\t}\r\n}\r\n\r\nfunction formatNationalNumber(number, carrierCode, formatAs, metadata, options) {\r\n\tconst format = chooseFormatForNumber(metadata.formats(), number)\r\n\tif (!format) {\r\n\t\treturn number\r\n\t}\r\n\treturn formatNationalNumberUsingFormat(\r\n\t\tnumber,\r\n\t\tformat,\r\n\t\t{\r\n\t\t\tuseInternationalFormat: formatAs === 'INTERNATIONAL',\r\n\t\t\twithNationalPrefix: format.nationalPrefixIsOptionalWhenFormattingInNationalFormat() && (options && options.nationalPrefix === false) ? false : true,\r\n\t\t\tcarrierCode,\r\n\t\t\tmetadata\r\n\t\t}\r\n\t)\r\n}\r\n\r\nexport function chooseFormatForNumber(availableFormats, nationalNumber) {\r\n\t// Using a `for ... of` loop here didn't work with `babel`/`istanbul`:\r\n\t// for some weird reason, it showed code coverage less than 100%.\r\n\t// That's because `babel`/`istanbul`, for some weird reason,\r\n\t// apparently doesn't know how to properly exclude Babel polyfills from code coverage.\r\n\t//\r\n\t// for (const format of availableFormats) { ... }\r\n\t//\r\n\treturn pickFirstMatchingElement(availableFormats, (format) => {\r\n\t\t// Validate leading digits.\r\n\t\t// The test case for \"else path\" could be found by searching for\r\n\t\t// \"format.leadingDigitsPatterns().length === 0\".\r\n\t\tif (format.leadingDigitsPatterns().length > 0) {\r\n\t\t\t// The last leading_digits_pattern is used here, as it is the most detailed\r\n\t\t\tconst lastLeadingDigitsPattern = format.leadingDigitsPatterns()[format.leadingDigitsPatterns().length - 1]\r\n\t\t\t// If leading digits don't match then move on to the next phone number format\r\n\t\t\tif (nationalNumber.search(lastLeadingDigitsPattern) !== 0) {\r\n\t\t\t\treturn false\r\n\t\t\t}\r\n\t\t}\r\n\t\t// Check that the national number matches the phone number format regular expression\r\n\t\treturn matchesEntirely(nationalNumber, format.pattern())\r\n\t})\r\n}\r\n\r\nfunction addExtension(formattedNumber, ext, metadata, formatExtension) {\r\n\treturn ext ? formatExtension(formattedNumber, ext, metadata) : formattedNumber\r\n}\r\n\r\nfunction formatIDD(\r\n\tnationalNumber,\r\n\tcarrierCode,\r\n\tcountryCallingCode,\r\n\tfromCountry,\r\n\tmetadata\r\n) {\r\n\tconst fromCountryCallingCode = getCountryCallingCode(fromCountry, metadata.metadata)\r\n\t// When calling within the same country calling code.\r\n\tif (fromCountryCallingCode === countryCallingCode) {\r\n\t\tconst formattedNumber = formatNationalNumber(nationalNumber, carrierCode, 'NATIONAL', metadata)\r\n\t\t// For NANPA regions, return the national format for these regions\r\n\t\t// but prefix it with the country calling code.\r\n\t\tif (countryCallingCode === '1') {\r\n\t\t\treturn countryCallingCode + ' ' + formattedNumber\r\n\t\t}\r\n\t\t// If regions share a country calling code, the country calling code need\r\n\t\t// not be dialled. This also applies when dialling within a region, so this\r\n\t\t// if clause covers both these cases. Technically this is the case for\r\n\t\t// dialling from La Reunion to other overseas departments of France (French\r\n\t\t// Guiana, Martinique, Guadeloupe), but not vice versa - so we don't cover\r\n\t\t// this edge case for now and for those cases return the version including\r\n\t\t// country calling code. Details here:\r\n\t\t// http://www.petitfute.com/voyage/225-info-pratiques-reunion\r\n\t\t//\r\n\t\treturn formattedNumber\r\n\t}\r\n\tconst iddPrefix = getIddPrefix(fromCountry, undefined, metadata.metadata)\r\n\tif (iddPrefix) {\r\n\t\treturn `${iddPrefix} ${countryCallingCode} ${formatNationalNumber(nationalNumber, null, 'INTERNATIONAL', metadata)}`\r\n\t}\r\n}\r\n\r\nfunction merge(...objects) {\r\n\tlet i = 1\r\n\twhile (i < objects.length) {\r\n\t\tif (objects[i]) {\r\n\t\t\tfor (const key in objects[i]) {\r\n\t\t\t\tobjects[0][key] = objects[i][key]\r\n\t\t\t}\r\n\t\t}\r\n\t\ti++\r\n\t}\r\n\treturn objects[0]\r\n}\r\n\r\nfunction pickFirstMatchingElement(elements, testFunction) {\r\n\tlet i = 0\r\n\twhile (i < elements.length) {\r\n\t\tif (testFunction(elements[i])) {\r\n\t\t\treturn elements[i]\r\n\t\t}\r\n\t\ti++\r\n\t}\r\n}"],"mappings":"AAAA;AACA;AACA;AACA;;AAEA,OAAOA,eAAe,MAAM,8BAA8B;AAC1D,OAAOC,+BAA+B,MAAM,8CAA8C;AAC1F,OAAOC,QAAQ,IAAIC,qBAAqB,QAAQ,eAAe;AAC/D,OAAOC,YAAY,MAAM,2BAA2B;AACpD,SAASC,aAAa,QAAQ,sBAAsB;AAEpD,MAAMC,eAAe,GAAG;EACvBC,eAAe,EAAEA,CAACC,eAAe,EAAEC,SAAS,EAAEC,QAAQ,KAAK,GAAGF,eAAe,GAAGE,QAAQ,CAACC,GAAG,CAAC,CAAC,GAAGF,SAAS;AAC3G,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASG,YAAYA,CAACC,KAAK,EAAEC,MAAM,EAAEC,OAAO,EAAEL,QAAQ,EAAE;EACtE;EACA,IAAIK,OAAO,EAAE;IACZ;IACA;IACA;IACA;IACA;IACA;IACA;IACAA,OAAO,GAAGC,KAAK,CAAC,CAAC,CAAC,EAAEV,eAAe,EAAES,OAAO,CAAC;EAC9C,CAAC,MAAM;IACNA,OAAO,GAAGT,eAAe;EAC1B;EAEAI,QAAQ,GAAG,IAAIR,QAAQ,CAACQ,QAAQ,CAAC;;EAEjC;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,IAAIG,KAAK,CAACI,OAAO,IAAIJ,KAAK,CAACI,OAAO,KAAK,KAAK,EAAE;IAC7C;IACA,IAAI,CAACP,QAAQ,CAACQ,UAAU,CAACL,KAAK,CAACI,OAAO,CAAC,EAAE;MACxC,MAAM,IAAIE,KAAK,CAAC,oBAAoBN,KAAK,CAACI,OAAO,EAAE,CAAC;IACrD;IACAP,QAAQ,CAACU,mBAAmB,CAAC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AAIK,oBAAoB,CAACH,cAAc,EAAE,IAAI,EAAE,eAAe,EAAEb,QAAQ,CAAC,EAAE;EACrH;AACD;AAEA,SAASM,KAAKA,CAAC,GAAGgC,OAAO,EAAE;EAC1B,IAAIC,CAAC,GAAG,CAAC;EACT,OAAOA,CAAC,GAAGD,OAAO,CAACP,MAAM,EAAE;IAC1B,IAAIO,OAAO,CAACC,CAAC,CAAC,EAAE;MACf,KAAK,MAAMC,GAAG,IAAIF,OAAO,CAACC,CAAC,CAAC,EAAE;QAC7BD,OAAO,CAAC,CAAC,CAAC,CAACE,GAAG,CAAC,GAAGF,OAAO,CAACC,CAAC,CAAC,CAACC,GAAG,CAAC;MAClC;IACD;IACAD,CAAC,EAAE;EACJ;EACA,OAAOD,OAAO,CAAC,CAAC,CAAC;AAClB;AAEA,SAAST,wBAAwBA,CAACY,QAAQ,EAAEC,YAAY,EAAE;EACzD,IAAIH,CAAC,GAAG,CAAC;EACT,OAAOA,CAAC,GAAGE,QAAQ,CAACV,MAAM,EAAE;IAC3B,IAAIW,YAAY,CAACD,QAAQ,CAACF,CAAC,CAAC,CAAC,EAAE;MAC9B,OAAOE,QAAQ,CAACF,CAAC,CAAC;IACnB;IACAA,CAAC,EAAE;EACJ;AACD","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"format.js","names":["matchesEntirely","formatNationalNumberUsingFormat","Metadata","getCountryCallingCode","getIddPrefix","formatRFC3966","DEFAULT_OPTIONS","formatExtension","formattedNumber","extension","metadata","ext","formatNumber","input","format","options","metadataJson","merge","country","hasCountry","Error","selectNumberingPlan","countryCallingCode","phone","nationalNumber","v2","number","formatNationalNumber","carrierCode","addExtension","fromCountry","formatIDD","formatAs","chooseFormatForNumber","formats","useInternationalFormat","withNationalPrefix","nationalPrefixIsOptionalWhenFormattingInNationalFormat","nationalPrefix","availableFormats","pickFirstMatchingElement","leadingDigitsPatterns","length","lastLeadingDigitsPattern","search","pattern","fromCountryCallingCode","iddPrefix","undefined","objects","i","key","elements","testFunction"],"sources":["../source/format.js"],"sourcesContent":["// This is a port of Google Android `libphonenumber`'s\r\n// `phonenumberutil.js` of December 31th, 2018.\r\n//\r\n// https://github.com/googlei18n/libphonenumber/commits/master/javascript/i18n/phonenumbers/phonenumberutil.js\r\n\r\nimport matchesEntirely from './helpers/matchesEntirely.js'\r\nimport formatNationalNumberUsingFormat from './helpers/formatNationalNumberUsingFormat.js'\r\nimport Metadata, { getCountryCallingCode } from './metadata.js'\r\nimport getIddPrefix from './helpers/getIddPrefix.js'\r\nimport { formatRFC3966 } from './helpers/RFC3966.js'\r\n\r\nconst DEFAULT_OPTIONS = {\r\n\tformatExtension: (formattedNumber, extension, metadata) => `${formattedNumber}${metadata.ext()}${extension}`\r\n}\r\n\r\n/**\r\n * Formats a phone number.\r\n *\r\n * format(phoneNumberInstance, 'INTERNATIONAL', { ..., v2: true }, metadata)\r\n * format(phoneNumberInstance, 'NATIONAL', { ..., v2: true }, metadata)\r\n *\r\n * format({ phone: '8005553535', country: 'RU' }, 'INTERNATIONAL', { ... }, metadata)\r\n * format({ phone: '8005553535', country: 'RU' }, 'NATIONAL', undefined, metadata)\r\n *\r\n * @param {object|PhoneNumber} input — If `options.v2: true` flag is passed, the `input` should be a `PhoneNumber` instance. Otherwise, it should be an object of shape `{ phone: '...', country: '...' }`.\r\n * @param {string} format\r\n * @param {object} [options]\r\n * @param {object} metadataJson\r\n * @return {string}\r\n */\r\nexport default function formatNumber(input, format, options, metadataJson) {\r\n\t// Apply default options.\r\n\tif (options) {\r\n\t\t// Using ES6 \"rest spread\" syntax here didn't work with `babel`/`istanbul`\r\n\t\t// for some weird reason: this line of code would cause the code coverage\r\n\t\t// to show as not 100%. That's because `babel`/`istanbul`, for some weird reason,\r\n\t\t// apparently doesn't know how to properly exclude Babel polyfills from code coverage.\r\n\t\t//\r\n\t\t// options = { ...DEFAULT_OPTIONS, ...options }\r\n\t\t//\r\n\t\toptions = merge({}, DEFAULT_OPTIONS, options)\r\n\t} else {\r\n\t\toptions = DEFAULT_OPTIONS\r\n\t}\r\n\r\n\tconst metadata = new Metadata(metadataJson)\r\n\r\n\t// Normally, the `input` object is supposed to be a `PhoneNumber` class instance.\r\n\t// Also, according to the `PhoneNumber` class source code, `country` can't be \"001\".\r\n\t// It means that normally `input.country` or `input.countryCallingCode` is supposed\r\n\t// to be present because either of the two is always required to exist in a `PhoneNumber` instance.\r\n\t// This means that realistically, it's gonna step into either the first `if`\r\n\t// or the following `else if`, and normally it won't even reach the legacy-compatibility\r\n\t// `else return input.phone || ''` part.\r\n\t// So normally, it won't ever return an empty string here.\r\n\tif (input.country && input.country !== '001') {\r\n\t\t// Validate `input.country`.\r\n\t\tif (!metadata.hasCountry(input.country)) {\r\n\t\t\tthrow new Error(`Unknown country: ${input.country}`)\r\n\t\t}\r\n\t\tmetadata.selectNumberingPlan(input.country)\r\n\t}\r\n\telse if (input.countryCallingCode) {\r\n\t\tmetadata.selectNumberingPlan(input.countryCallingCode)\r\n\t}\r\n\telse return input.phone || ''\r\n\r\n\tconst countryCallingCode = metadata.countryCallingCode()\r\n\r\n\tconst nationalNumber = options.v2 ? input.nationalNumber : input.phone\r\n\r\n\t// This variable should have been declared inside `case`s\r\n\t// but Babel has a bug and it says \"duplicate variable declaration\".\r\n\tlet number\r\n\r\n\tswitch (format) {\r\n\t\tcase 'NATIONAL':\r\n\t\t\t// Normally, the `input` object is supposed to be a `PhoneNumber` class instance,\r\n\t\t\t// and a `PhoneNumber` class instance is always required to have a `nationalNumber`.\r\n\t\t\t// This means that the `if (!nationalNumber)` below is just for legacy-compatibility\r\n\t\t\t// and it normally can't really happen, so normally it won't ever return an empty string here.\r\n\t\t\tif (!nationalNumber) {\r\n\t\t\t\treturn ''\r\n\t\t\t}\r\n\t\t\tnumber = formatNationalNumber(nationalNumber, input.carrierCode, 'NATIONAL', metadata, options)\r\n\t\t\treturn addExtension(number, input.ext, metadata, options.formatExtension)\r\n\r\n\t\tcase 'INTERNATIONAL':\r\n\t\t\t// Legacy argument support.\r\n\t\t\t// (`{ country: ..., phone: '' }`)\r\n\t\t\tif (!nationalNumber) {\r\n\t\t\t\treturn `+${countryCallingCode}`\r\n\t\t\t}\r\n\t\t\tnumber = formatNationalNumber(nationalNumber, null, 'INTERNATIONAL', metadata, options)\r\n\t\t\tnumber = `+${countryCallingCode} ${number}`\r\n\t\t\treturn addExtension(number, input.ext, metadata, options.formatExtension)\r\n\r\n\t\tcase 'E.164':\r\n\t\t\t// `E.164` doesn't define \"phone number extensions\".\r\n\t\t\treturn `+${countryCallingCode}${nationalNumber}`\r\n\r\n\t\tcase 'RFC3966':\r\n\t\t\treturn formatRFC3966({\r\n\t\t\t\tnumber: `+${countryCallingCode}${nationalNumber}`,\r\n\t\t\t\text: input.ext\r\n\t\t\t})\r\n\r\n\t\t// For reference, here's Google's IDD formatter:\r\n\t\t// https://github.com/google/libphonenumber/blob/32719cf74e68796788d1ca45abc85dcdc63ba5b9/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L1546\r\n\t\t// Not saying that this IDD formatter replicates it 1:1, but it seems to work.\r\n\t\t// Who would even need to format phone numbers in IDD format anyway?\r\n\t\tcase 'IDD':\r\n\t\t\t// If the required `fromCountry` parameter is not passed, it will return `undefined`.\r\n\t\t\tif (!options.fromCountry) {\r\n\t\t\t\treturn\r\n\t\t\t\t// throw new Error('`fromCountry` option not passed for IDD-prefixed formatting.')\r\n\t\t\t}\r\n\t\t\tconst formattedNumber = formatIDD(\r\n\t\t\t\tnationalNumber,\r\n\t\t\t\tinput.carrierCode,\r\n\t\t\t\tcountryCallingCode,\r\n\t\t\t\toptions.fromCountry,\r\n\t\t\t\tmetadata\r\n\t\t\t)\r\n\t\t\t// If the country of the phone number doesn't support IDD calling, it will return `undefined`.\r\n\t\t\tif (!formattedNumber) {\r\n\t\t\t\treturn\r\n\t\t\t}\r\n\t\t\treturn addExtension(formattedNumber, input.ext, metadata, options.formatExtension)\r\n\r\n\t\tdefault:\r\n\t\t\tthrow new Error(`Unknown \"format\" argument passed to \"formatNumber()\": \"${format}\"`)\r\n\t}\r\n}\r\n\r\nfunction formatNationalNumber(number, carrierCode, formatAs, metadata, options) {\r\n\tconst format = chooseFormatForNumber(metadata.formats(), number)\r\n\tif (!format) {\r\n\t\treturn number\r\n\t}\r\n\treturn formatNationalNumberUsingFormat(\r\n\t\tnumber,\r\n\t\tformat,\r\n\t\t{\r\n\t\t\tuseInternationalFormat: formatAs === 'INTERNATIONAL',\r\n\t\t\twithNationalPrefix: format.nationalPrefixIsOptionalWhenFormattingInNationalFormat() && (options && options.nationalPrefix === false) ? false : true,\r\n\t\t\tcarrierCode,\r\n\t\t\tmetadata\r\n\t\t}\r\n\t)\r\n}\r\n\r\nexport function chooseFormatForNumber(availableFormats, nationalNumber) {\r\n\t// Using a `for ... of` loop here didn't work with `babel`/`istanbul`:\r\n\t// for some weird reason, it showed code coverage less than 100%.\r\n\t// That's because `babel`/`istanbul`, for some weird reason,\r\n\t// apparently doesn't know how to properly exclude Babel polyfills from code coverage.\r\n\t//\r\n\t// for (const format of availableFormats) { ... }\r\n\t//\r\n\treturn pickFirstMatchingElement(availableFormats, (format) => {\r\n\t\t// Validate leading digits.\r\n\t\t// The test case for \"else path\" could be found by searching for\r\n\t\t// \"format.leadingDigitsPatterns().length === 0\".\r\n\t\tif (format.leadingDigitsPatterns().length > 0) {\r\n\t\t\t// The last leading_digits_pattern is used here, as it is the most detailed\r\n\t\t\tconst lastLeadingDigitsPattern = format.leadingDigitsPatterns()[format.leadingDigitsPatterns().length - 1]\r\n\t\t\t// If leading digits don't match then move on to the next phone number format\r\n\t\t\tif (nationalNumber.search(lastLeadingDigitsPattern) !== 0) {\r\n\t\t\t\treturn false\r\n\t\t\t}\r\n\t\t}\r\n\t\t// Check that the national number matches the phone number format regular expression\r\n\t\treturn matchesEntirely(nationalNumber, format.pattern())\r\n\t})\r\n}\r\n\r\nfunction addExtension(formattedNumber, ext, metadata, formatExtension) {\r\n\treturn ext ? formatExtension(formattedNumber, ext, metadata) : formattedNumber\r\n}\r\n\r\nfunction formatIDD(\r\n\tnationalNumber,\r\n\tcarrierCode,\r\n\tcountryCallingCode,\r\n\tfromCountry,\r\n\tmetadata\r\n) {\r\n\tconst fromCountryCallingCode = getCountryCallingCode(fromCountry, metadata.metadata)\r\n\t// When calling within the same country calling code.\r\n\tif (fromCountryCallingCode === countryCallingCode) {\r\n\t\tconst formattedNumber = formatNationalNumber(nationalNumber, carrierCode, 'NATIONAL', metadata)\r\n\t\t// For NANPA regions, return the national format for these regions\r\n\t\t// but prefix it with the country calling code.\r\n\t\tif (countryCallingCode === '1') {\r\n\t\t\treturn countryCallingCode + ' ' + formattedNumber\r\n\t\t}\r\n\t\t// If regions share a country calling code, the country calling code need\r\n\t\t// not be dialled. This also applies when dialling within a region, so this\r\n\t\t// if clause covers both these cases. Technically this is the case for\r\n\t\t// dialling from La Reunion to other overseas departments of France (French\r\n\t\t// Guiana, Martinique, Guadeloupe), but not vice versa - so we don't cover\r\n\t\t// this edge case for now and for those cases return the version including\r\n\t\t// country calling code. Details here:\r\n\t\t// http://www.petitfute.com/voyage/225-info-pratiques-reunion\r\n\t\t//\r\n\t\treturn formattedNumber\r\n\t}\r\n\tconst iddPrefix = getIddPrefix(fromCountry, undefined, metadata.metadata)\r\n\tif (iddPrefix) {\r\n\t\treturn `${iddPrefix} ${countryCallingCode} ${formatNationalNumber(nationalNumber, null, 'INTERNATIONAL', metadata)}`\r\n\t}\r\n}\r\n\r\nfunction merge(...objects) {\r\n\tlet i = 1\r\n\twhile (i < objects.length) {\r\n\t\tif (objects[i]) {\r\n\t\t\tfor (const key in objects[i]) {\r\n\t\t\t\tobjects[0][key] = objects[i][key]\r\n\t\t\t}\r\n\t\t}\r\n\t\ti++\r\n\t}\r\n\treturn objects[0]\r\n}\r\n\r\nfunction pickFirstMatchingElement(elements, testFunction) {\r\n\tlet i = 0\r\n\twhile (i < elements.length) {\r\n\t\tif (testFunction(elements[i])) {\r\n\t\t\treturn elements[i]\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EA,SAASO,KAAKA,CAAC,GAAGgC,OAAO,EAAE;EAC1B,IAAIC,CAAC,GAAG,CAAC;EACT,OAAOA,CAAC,GAAGD,OAAO,CAACP,MAAM,EAAE;IAC1B,IAAIO,OAAO,CAACC,CAAC,CAAC,EAAE;MACf,KAAK,MAAMC,GAAG,IAAIF,OAAO,CAACC,CAAC,CAAC,EAAE;QAC7BD,OAAO,CAAC,CAAC,CAAC,CAACE,GAAG,CAAC,GAAGF,OAAO,CAACC,CAAC,CAAC,CAACC,GAAG,CAAC;MAClC;IACD;IACAD,CAAC,EAAE;EACJ;EACA,OAAOD,OAAO,CAAC,CAAC,CAAC;AAClB;AAEA,SAAST,wBAAwBA,CAACY,QAAQ,EAAEC,YAAY,EAAE;EACzD,IAAIH,CAAC,GAAG,CAAC;EACT,OAAOA,CAAC,GAAGE,QAAQ,CAACV,MAAM,EAAE;IAC3B,IAAIW,YAAY,CAACD,QAAQ,CAACF,CAAC,CAAC,CAAC,EAAE;MAC9B,OAAOE,QAAQ,CAACF,CAAC,CAAC;IACnB;IACAA,CAAC,EAAE;EACJ;AACD","ignoreList":[]}
|
|
@@ -21,10 +21,11 @@ const REGION_CODE_FOR_NON_GEO_ENTITY = '001';
|
|
|
21
21
|
* @param {string} from_country - the region where the call is being placed.
|
|
22
22
|
* @param {boolean} with_formatting - whether the number should be returned with
|
|
23
23
|
* formatting symbols, such as spaces and dashes.
|
|
24
|
+
* @param {object} metadataJson
|
|
24
25
|
* @return {string}
|
|
25
26
|
*/
|
|
26
|
-
export default function (number, from_country, with_formatting,
|
|
27
|
-
metadata = new Metadata(
|
|
27
|
+
export default function (number, from_country, with_formatting, metadataJson) {
|
|
28
|
+
const metadata = new Metadata(metadataJson);
|
|
28
29
|
|
|
29
30
|
// Validate `from_country`.
|
|
30
31
|
if (!metadata.hasCountry(from_country)) {
|
|
@@ -59,7 +60,7 @@ export default function (number, from_country, with_formatting, metadata) {
|
|
|
59
60
|
|
|
60
61
|
// Select country for `checkNumberLength()`.
|
|
61
62
|
metadata.selectNumberingPlan(country);
|
|
62
|
-
if (can_be_internationally_dialled(number) && checkNumberLength(number.phone,
|
|
63
|
+
if (can_be_internationally_dialled(number) && checkNumberLength(number.phone, undefined, metadata) !== 'TOO_SHORT') {
|
|
63
64
|
formatted_number = format(number, 'INTERNATIONAL', metadata.metadata);
|
|
64
65
|
} else {
|
|
65
66
|
formatted_number = format(number, 'NATIONAL', metadata.metadata);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatPhoneNumberForMobileDialing.js","names":["Metadata","format","getNumberType","checkNumberLength","getCountryCallingCode","REGION_CODE_FOR_NON_GEO_ENTITY","number","from_country","with_formatting","metadata","hasCountry","Error","phone","country","number_type","undefined","is_valid_number","formatted_number","is_fixed_line_or_mobile","carrierCode","formatNationalNumberWithPreferredCarrierCode","selectNumberingPlan","can_be_internationally_dialled","diallable_chars","DIALLABLE_CHARACTERS","result","i","length","character","getPreferredDomesticCarrierCodeOrDefault","formatNationalNumberWithCarrierCode"],"sources":["../source/formatPhoneNumberForMobileDialing.js"],"sourcesContent":["// This function is copy-pasted from\r\n// https://github.com/googlei18n/libphonenumber/blob/master/javascript/i18n/phonenumbers/phonenumberutil.js\r\n// It hasn't been tested. It's not currently exported.\r\n// Carriers codes aren't part of this library.\r\n// Send a PR if you want to add them.\r\n\r\nimport Metadata from './metadata.js'\r\nimport format from './format.js'\r\nimport getNumberType from './helpers/getNumberType.js'\r\nimport checkNumberLength from './helpers/checkNumberLength.js'\r\nimport getCountryCallingCode from './getCountryCallingCode.js'\r\n\r\nconst REGION_CODE_FOR_NON_GEO_ENTITY = '001'\r\n\r\n/**\r\n * Returns a number formatted in such a way that it can be dialed from a mobile\r\n * phone in a specific region. If the number cannot be reached from the region\r\n * (e.g. some countries block toll-free numbers from being called outside of the\r\n * country), the method returns an empty string.\r\n *\r\n * @param {object} number - a `parse()`d phone number to be formatted.\r\n * @param {string} from_country - the region where the call is being placed.\r\n * @param {boolean} with_formatting - whether the number should be returned with\r\n * formatting symbols, such as spaces and dashes.\r\n * @return {string}\r\n */\r\nexport default function(number, from_country, with_formatting, metadata) {\r\n\tmetadata = new Metadata(metadata)\r\n\r\n\t// Validate `from_country`.\r\n\tif (!metadata.hasCountry(from_country)) {\r\n\t\tthrow new Error(`Unknown country: ${from_country}`)\r\n\t}\r\n\r\n\t// Not using the extension, as that part cannot normally be dialed\r\n\t// together with the main number.\r\n\tnumber = {\r\n\t\tphone: number.phone,\r\n\t\tcountry: number.country\r\n\t}\r\n\r\n\tconst number_type = getNumberType(number, undefined, metadata.metadata)\r\n\tconst is_valid_number = number_type === number\r\n\r\n\tlet formatted_number\r\n\r\n\tif (country === from_country) {\r\n\t\tconst is_fixed_line_or_mobile =\r\n\t\t\tnumber_type === 'FIXED_LINE' ||\r\n\t\t\tnumber_type === 'MOBILE' ||\r\n\t\t\tnumber_type === 'FIXED_LINE_OR_MOBILE'\r\n\r\n\t\t// Carrier codes may be needed in some countries. We handle this here.\r\n\t\tif (country == 'BR' && is_fixed_line_or_mobile) {\r\n\t\t\tformatted_number =\r\n\t\t\t\tcarrierCode ?\r\n\t\t\t\tformatNationalNumberWithPreferredCarrierCode(number) :\r\n\t\t\t\t// Brazilian fixed line and mobile numbers need to be dialed with a\r\n\t\t\t\t// carrier code when called within Brazil. Without that, most of the\r\n\t\t\t\t// carriers won't connect the call. Because of that, we return an\r\n\t\t\t\t// empty string here.\r\n\t\t\t\t''\r\n\t\t} else if (getCountryCallingCode(country, metadata.metadata) === '1') {\r\n\t\t\t// For NANPA countries, we output international format for numbers that\r\n\t\t\t// can be dialed internationally, since that always works, except for\r\n\t\t\t// numbers which might potentially be short numbers, which are always\r\n\t\t\t// dialled in national format.\r\n\r\n\t\t\t// Select country for `checkNumberLength()`.\r\n\t\t\tmetadata.selectNumberingPlan(country)\r\n\r\n\t\t\tif (can_be_internationally_dialled(number) &&\r\n\t\t\t\tcheckNumberLength(number.phone, country, metadata) !== 'TOO_SHORT') {\r\n\t\t\t\tformatted_number = format(number, 'INTERNATIONAL', metadata.metadata)\r\n\t\t\t}\r\n\t\t\telse {\r\n\t\t\t\tformatted_number = format(number, 'NATIONAL', metadata.metadata)\r\n\t\t\t}\r\n\t\t}\r\n\t\telse {\r\n\t\t\t// For non-geographic countries, Mexican and Chilean fixed line and\r\n\t\t\t// mobile numbers, we output international format for numbers that can be\r\n\t\t\t// dialed internationally, as that always works.\r\n\t\t\tif (\r\n\t\t\t\t(\r\n\t\t\t\t\tcountry === REGION_CODE_FOR_NON_GEO_ENTITY\r\n\t\t\t\t\t||\r\n\t\t\t\t\t// MX fixed line and mobile numbers should always be formatted in\r\n\t\t\t\t\t// international format, even when dialed within MX. For national\r\n\t\t\t\t\t// format to work, a carrier code needs to be used, and the correct\r\n\t\t\t\t\t// carrier code depends on if the caller and callee are from the\r\n\t\t\t\t\t// same local area. It is trickier to get that to work correctly than\r\n\t\t\t\t\t// using international format, which is tested to work fine on all\r\n\t\t\t\t\t// carriers.\r\n\t\t\t\t\t//\r\n\t\t\t\t\t// CL fixed line numbers need the national prefix when dialing in the\r\n\t\t\t\t\t// national format, but don't have it when used for display. The\r\n\t\t\t\t\t// reverse is true for mobile numbers. As a result, we output them in\r\n\t\t\t\t\t// the international format to make it work.\r\n\t\t\t\t\t//\r\n\t\t\t\t\t// UZ mobile and fixed-line numbers have to be formatted in\r\n\t\t\t\t\t// international format or prefixed with special codes like 03, 04\r\n\t\t\t\t\t// (for fixed-line) and 05 (for mobile) for dialling successfully\r\n\t\t\t\t\t// from mobile devices. As we do not have complete information on\r\n\t\t\t\t\t// special codes and to be consistent with formatting across all\r\n\t\t\t\t\t// phone types we return the number in international format here.\r\n\t\t\t\t\t//\r\n\t\t\t\t\t((country === 'MX' || country === 'CL' || country == 'UZ') && is_fixed_line_or_mobile)\r\n\t\t\t\t)\r\n\t\t\t\t&&\r\n\t\t\t\tcan_be_internationally_dialled(number)\r\n\t\t\t) {\r\n\t\t\t\tformatted_number = format(number, 'INTERNATIONAL')\r\n\t\t\t}\r\n\t\t\telse {\r\n\t\t\t\tformatted_number = format(number, 'NATIONAL')\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\telse if (is_valid_number && can_be_internationally_dialled(number)) {\r\n\t\t// We assume that short numbers are not diallable from outside their region,\r\n\t\t// so if a number is not a valid regular length phone number, we treat it as\r\n\t\t// if it cannot be internationally dialled.\r\n\t\treturn with_formatting ?\r\n\t\t\tformat(number, 'INTERNATIONAL', metadata.metadata) :\r\n\t\t\tformat(number, 'E.164', metadata.metadata)\r\n\t}\r\n\r\n\tif (!with_formatting) {\r\n\t\treturn diallable_chars(formatted_number)\r\n\t}\r\n\r\n\treturn formatted_number\r\n}\r\n\r\nfunction can_be_internationally_dialled(number) {\r\n\treturn true\r\n}\r\n\r\n/**\r\n * A map that contains characters that are essential when dialling. That means\r\n * any of the characters in this map must not be removed from a number when\r\n * dialling, otherwise the call will not reach the intended destination.\r\n */\r\nconst DIALLABLE_CHARACTERS = {\r\n\t'0': '0',\r\n\t'1': '1',\r\n\t'2': '2',\r\n\t'3': '3',\r\n\t'4': '4',\r\n\t'5': '5',\r\n\t'6': '6',\r\n\t'7': '7',\r\n\t'8': '8',\r\n\t'9': '9',\r\n\t'+': '+',\r\n\t'*': '*',\r\n\t'#': '#'\r\n}\r\n\r\nfunction diallable_chars(formatted_number) {\r\n\tlet result = ''\r\n\r\n\tlet i = 0\r\n\twhile (i < formatted_number.length) {\r\n\t\tconst character = formatted_number[i]\r\n\t\tif (DIALLABLE_CHARACTERS[character]) {\r\n\t\t\tresult += character\r\n\t\t}\r\n\t\ti++\r\n\t}\r\n\r\n\treturn result\r\n}\r\n\r\nfunction getPreferredDomesticCarrierCodeOrDefault() {\r\n\tthrow new Error('carrier codes are not part of this library')\r\n}\r\n\r\nfunction formatNationalNumberWithCarrierCode() {\r\n\tthrow new Error('carrier codes are not part of this library')\r\n}\r\n\r\n/**\r\n * Formats a phone number in national format for dialing using the carrier as\r\n * specified in the preferred_domestic_carrier_code field of the PhoneNumber\r\n * object passed in. If that is missing, use the {@code fallbackCarrierCode}\r\n * passed in instead. If there is no {@code preferred_domestic_carrier_code},\r\n * and the {@code fallbackCarrierCode} contains an empty string, return the\r\n * number in national format without any carrier code.\r\n *\r\n * <p>Use {@link #formatNationalNumberWithCarrierCode} instead if the carrier\r\n * code passed in should take precedence over the number's\r\n * {@code preferred_domestic_carrier_code} when formatting.\r\n *\r\n * @param {i18n.phonenumbers.PhoneNumber} number the phone number to be\r\n * formatted.\r\n * @param {string} fallbackCarrierCode the carrier selection code to be used, if\r\n * none is found in the phone number itself.\r\n * @return {string} the formatted phone number in national format for dialing\r\n * using the number's preferred_domestic_carrier_code, or the\r\n * {@code fallbackCarrierCode} passed in if none is found.\r\n */\r\nfunction formatNationalNumberWithPreferredCarrierCode(number) {\r\n\treturn formatNationalNumberWithCarrierCode(\r\n\t\tnumber,\r\n\t\tcarrierCode\r\n\t);\r\n}\r\n\r\n// Google's tests:\r\n// https://github.com/googlei18n/libphonenumber/blob/597983dc4d56ed7e5337a8e74316dc7a3d02d794/javascript/i18n/phonenumbers/phonenumberutil_test.js\r\n//\r\n// import metadata from '../metadata.min.json' with { type: 'json' }\r\n//\r\n// import formatPhoneNumberForMobileDialing from './formatPhoneNumberForMobileDialing.js'\r\n//\r\n// describe('formatPhoneNumberForMobileDialing', () => {\r\n// \tit('should format for mobile dialing', () => {\r\n// \t\tformatPhoneNumberForMobileDialing({ phone: '8005553535', country: 'RU' }, 'US', true, metadata).should.equal('+7 800 555 3535')\r\n// \t\tformatPhoneNumberForMobileDialing({ phone: '8005553535', country: 'RU' }, 'US', false, metadata).should.equal('+78005553535')\r\n// \t\tformatPhoneNumberForMobileDialing({ phone: '8005553535', country: 'RU' }, 'RU', false, metadata).should.equal('8005553535')\r\n// \t})\r\n// })"],"mappings":"AAAA;AACA;AACA;AACA;AACA;;AAEA,OAAOA,QAAQ,MAAM,eAAe;AACpC,OAAOC,MAAM,MAAM,aAAa;AAChC,OAAO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G,EAAE,GAAG;EACR,GAAG,EAAE,GAAG;EACR,GAAG,EAAE,GAAG;EACR,GAAG,EAAE,GAAG;EACR,GAAG,EAAE,GAAG;EACR,GAAG,EAAE,GAAG;EACR,GAAG,EAAE,GAAG;EACR,GAAG,EAAE,GAAG;EACR,GAAG,EAAE,GAAG;EACR,GAAG,EAAE,GAAG;EACR,GAAG,EAAE,GAAG;EACR,GAAG,EAAE,GAAG;EACR,GAAG,EAAE;AACN,CAAC;AAED,SAASD,eAAeA,CAACN,gBAAgB,EAAE;EAC1C,IAAIQ,MAAM,GAAG,EAAE;EAEf,IAAIC,CAAC,GAAG,CAAC;EACT,OAAOA,CAAC,GAAGT,gBAAgB,CAACU,MAAM,EAAE;IACnC,MAAMC,SAAS,GAAGX,gBAAgB,CAACS,CAAC,CAAC;IACrC,IAAIF,oBAAoB,CAACI,SAAS,CAAC,EAAE;MACpCH,MAAM,IAAIG,SAAS;IACpB;IACAF,CAAC,EAAE;EACJ;EAEA,OAAOD,MAAM;AACd;AAEA,SAASI,wCAAwCA,CAAA,EAAG;EACnD,MAAM,IAAIlB,KAAK,CAAC,4CAA4C,CAAC;AAC9D;AAEA,SAASmB,mCAAmCA,CAAA,EAAG;EAC9C,MAAM,IAAInB,KAAK,CAAC,4CAA4C,CAAC;AAC9D;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASS,4CAA4CA,CAACd,MAAM,EAAE;EAC7D,OAAOwB,mCAAmC,CACzCxB,MAAM,EACNa,WACD,CAAC;AACF;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"formatPhoneNumberForMobileDialing.js","names":["Metadata","format","getNumberType","checkNumberLength","getCountryCallingCode","REGION_CODE_FOR_NON_GEO_ENTITY","number","from_country","with_formatting","metadataJson","metadata","hasCountry","Error","phone","country","number_type","undefined","is_valid_number","formatted_number","is_fixed_line_or_mobile","carrierCode","formatNationalNumberWithPreferredCarrierCode","selectNumberingPlan","can_be_internationally_dialled","diallable_chars","DIALLABLE_CHARACTERS","result","i","length","character","getPreferredDomesticCarrierCodeOrDefault","formatNationalNumberWithCarrierCode"],"sources":["../source/formatPhoneNumberForMobileDialing.js"],"sourcesContent":["// This function is copy-pasted from\r\n// https://github.com/googlei18n/libphonenumber/blob/master/javascript/i18n/phonenumbers/phonenumberutil.js\r\n// It hasn't been tested. It's not currently exported.\r\n// Carriers codes aren't part of this library.\r\n// Send a PR if you want to add them.\r\n\r\nimport Metadata from './metadata.js'\r\nimport format from './format.js'\r\nimport getNumberType from './helpers/getNumberType.js'\r\nimport checkNumberLength from './helpers/checkNumberLength.js'\r\nimport getCountryCallingCode from './getCountryCallingCode.js'\r\n\r\nconst REGION_CODE_FOR_NON_GEO_ENTITY = '001'\r\n\r\n/**\r\n * Returns a number formatted in such a way that it can be dialed from a mobile\r\n * phone in a specific region. If the number cannot be reached from the region\r\n * (e.g. some countries block toll-free numbers from being called outside of the\r\n * country), the method returns an empty string.\r\n *\r\n * @param {object} number - a `parse()`d phone number to be formatted.\r\n * @param {string} from_country - the region where the call is being placed.\r\n * @param {boolean} with_formatting - whether the number should be returned with\r\n * formatting symbols, such as spaces and dashes.\r\n * @param {object} metadataJson\r\n * @return {string}\r\n */\r\nexport default function(number, from_country, with_formatting, metadataJson) {\r\n\tconst metadata = new Metadata(metadataJson)\r\n\r\n\t// Validate `from_country`.\r\n\tif (!metadata.hasCountry(from_country)) {\r\n\t\tthrow new Error(`Unknown country: ${from_country}`)\r\n\t}\r\n\r\n\t// Not using the extension, as that part cannot normally be dialed\r\n\t// together with the main number.\r\n\tnumber = {\r\n\t\tphone: number.phone,\r\n\t\tcountry: number.country\r\n\t}\r\n\r\n\tconst number_type = getNumberType(number, undefined, metadata.metadata)\r\n\tconst is_valid_number = number_type === number\r\n\r\n\tlet formatted_number\r\n\r\n\tif (country === from_country) {\r\n\t\tconst is_fixed_line_or_mobile =\r\n\t\t\tnumber_type === 'FIXED_LINE' ||\r\n\t\t\tnumber_type === 'MOBILE' ||\r\n\t\t\tnumber_type === 'FIXED_LINE_OR_MOBILE'\r\n\r\n\t\t// Carrier codes may be needed in some countries. We handle this here.\r\n\t\tif (country == 'BR' && is_fixed_line_or_mobile) {\r\n\t\t\tformatted_number =\r\n\t\t\t\tcarrierCode ?\r\n\t\t\t\tformatNationalNumberWithPreferredCarrierCode(number) :\r\n\t\t\t\t// Brazilian fixed line and mobile numbers need to be dialed with a\r\n\t\t\t\t// carrier code when called within Brazil. Without that, most of the\r\n\t\t\t\t// carriers won't connect the call. Because of that, we return an\r\n\t\t\t\t// empty string here.\r\n\t\t\t\t''\r\n\t\t} else if (getCountryCallingCode(country, metadata.metadata) === '1') {\r\n\t\t\t// For NANPA countries, we output international format for numbers that\r\n\t\t\t// can be dialed internationally, since that always works, except for\r\n\t\t\t// numbers which might potentially be short numbers, which are always\r\n\t\t\t// dialled in national format.\r\n\r\n\t\t\t// Select country for `checkNumberLength()`.\r\n\t\t\tmetadata.selectNumberingPlan(country)\r\n\r\n\t\t\tif (can_be_internationally_dialled(number) &&\r\n\t\t\t\tcheckNumberLength(number.phone, undefined, metadata) !== 'TOO_SHORT') {\r\n\t\t\t\tformatted_number = format(number, 'INTERNATIONAL', metadata.metadata)\r\n\t\t\t}\r\n\t\t\telse {\r\n\t\t\t\tformatted_number = format(number, 'NATIONAL', metadata.metadata)\r\n\t\t\t}\r\n\t\t}\r\n\t\telse {\r\n\t\t\t// For non-geographic countries, Mexican and Chilean fixed line and\r\n\t\t\t// mobile numbers, we output international format for numbers that can be\r\n\t\t\t// dialed internationally, as that always works.\r\n\t\t\tif (\r\n\t\t\t\t(\r\n\t\t\t\t\tcountry === REGION_CODE_FOR_NON_GEO_ENTITY\r\n\t\t\t\t\t||\r\n\t\t\t\t\t// MX fixed line and mobile numbers should always be formatted in\r\n\t\t\t\t\t// international format, even when dialed within MX. For national\r\n\t\t\t\t\t// format to work, a carrier code needs to be used, and the correct\r\n\t\t\t\t\t// carrier code depends on if the caller and callee are from the\r\n\t\t\t\t\t// same local area. It is trickier to get that to work correctly than\r\n\t\t\t\t\t// using international format, which is tested to work fine on all\r\n\t\t\t\t\t// carriers.\r\n\t\t\t\t\t//\r\n\t\t\t\t\t// CL fixed line numbers need the national prefix when dialing in the\r\n\t\t\t\t\t// national format, but don't have it when used for display. The\r\n\t\t\t\t\t// reverse is true for mobile numbers. As a result, we output them in\r\n\t\t\t\t\t// the international format to make it work.\r\n\t\t\t\t\t//\r\n\t\t\t\t\t// UZ mobile and fixed-line numbers have to be formatted in\r\n\t\t\t\t\t// international format or prefixed with special codes like 03, 04\r\n\t\t\t\t\t// (for fixed-line) and 05 (for mobile) for dialling successfully\r\n\t\t\t\t\t// from mobile devices. As we do not have complete information on\r\n\t\t\t\t\t// special codes and to be consistent with formatting across all\r\n\t\t\t\t\t// phone types we return the number in international format here.\r\n\t\t\t\t\t//\r\n\t\t\t\t\t((country === 'MX' || country === 'CL' || country == 'UZ') && is_fixed_line_or_mobile)\r\n\t\t\t\t)\r\n\t\t\t\t&&\r\n\t\t\t\tcan_be_internationally_dialled(number)\r\n\t\t\t) {\r\n\t\t\t\tformatted_number = format(number, 'INTERNATIONAL')\r\n\t\t\t}\r\n\t\t\telse {\r\n\t\t\t\tformatted_number = format(number, 'NATIONAL')\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\telse if (is_valid_number && can_be_internationally_dialled(number)) {\r\n\t\t// We assume that short numbers are not diallable from outside their region,\r\n\t\t// so if a number is not a valid regular length phone number, we treat it as\r\n\t\t// if it cannot be internationally dialled.\r\n\t\treturn with_formatting ?\r\n\t\t\tformat(number, 'INTERNATIONAL', metadata.metadata) :\r\n\t\t\tformat(number, 'E.164', metadata.metadata)\r\n\t}\r\n\r\n\tif (!with_formatting) {\r\n\t\treturn diallable_chars(formatted_number)\r\n\t}\r\n\r\n\treturn formatted_number\r\n}\r\n\r\nfunction can_be_internationally_dialled(number) {\r\n\treturn true\r\n}\r\n\r\n/**\r\n * A map that contains characters that are essential when dialling. That means\r\n * any of the characters in this map must not be removed from a number when\r\n * dialling, otherwise the call will not reach the intended destination.\r\n */\r\nconst DIALLABLE_CHARACTERS = {\r\n\t'0': '0',\r\n\t'1': '1',\r\n\t'2': '2',\r\n\t'3': '3',\r\n\t'4': '4',\r\n\t'5': '5',\r\n\t'6': '6',\r\n\t'7': '7',\r\n\t'8': '8',\r\n\t'9': '9',\r\n\t'+': '+',\r\n\t'*': '*',\r\n\t'#': '#'\r\n}\r\n\r\nfunction diallable_chars(formatted_number) {\r\n\tlet result = ''\r\n\r\n\tlet i = 0\r\n\twhile (i < formatted_number.length) {\r\n\t\tconst character = formatted_number[i]\r\n\t\tif (DIALLABLE_CHARACTERS[character]) {\r\n\t\t\tresult += character\r\n\t\t}\r\n\t\ti++\r\n\t}\r\n\r\n\treturn result\r\n}\r\n\r\nfunction getPreferredDomesticCarrierCodeOrDefault() {\r\n\tthrow new Error('carrier codes are not part of this library')\r\n}\r\n\r\nfunction formatNationalNumberWithCarrierCode() {\r\n\tthrow new Error('carrier codes are not part of this library')\r\n}\r\n\r\n/**\r\n * Formats a phone number in national format for dialing using the carrier as\r\n * specified in the preferred_domestic_carrier_code field of the PhoneNumber\r\n * object passed in. If that is missing, use the {@code fallbackCarrierCode}\r\n * passed in instead. If there is no {@code preferred_domestic_carrier_code},\r\n * and the {@code fallbackCarrierCode} contains an empty string, return the\r\n * number in national format without any carrier code.\r\n *\r\n * <p>Use {@link #formatNationalNumberWithCarrierCode} instead if the carrier\r\n * code passed in should take precedence over the number's\r\n * {@code preferred_domestic_carrier_code} when formatting.\r\n *\r\n * @param {i18n.phonenumbers.PhoneNumber} number the phone number to be\r\n * formatted.\r\n * @param {string} fallbackCarrierCode the carrier selection code to be used, if\r\n * none is found in the phone number itself.\r\n * @return {string} the formatted phone number in national format for dialing\r\n * using the number's preferred_domestic_carrier_code, or the\r\n * {@code fallbackCarrierCode} passed in if none is found.\r\n */\r\nfunction formatNationalNumberWithPreferredCarrierCode(number) {\r\n\treturn formatNationalNumberWithCarrierCode(\r\n\t\tnumber,\r\n\t\tcarrierCode\r\n\t);\r\n}\r\n\r\n// Google's tests:\r\n// https://github.com/googlei18n/libphonenumber/blob/597983dc4d56ed7e5337a8e74316dc7a3d02d794/javascript/i18n/phonenumbers/phonenumberutil_test.js\r\n//\r\n// import metadata from '../metadata.min.json' with { type: 'json' }\r\n//\r\n// import formatPhoneNumberForMobileDialing from './formatPhoneNumberForMobileDialing.js'\r\n//\r\n// describe('formatPhoneNumberForMobileDialing', () => {\r\n// \tit('should format for mobile dialing', () => {\r\n// \t\tformatPhoneNumberForMobileDialing({ phone: '8005553535', country: 'RU' }, 'US', true, metadata).should.equal('+7 800 555 3535')\r\n// \t\tformatPhoneNumberForMobileDialing({ phone: '8005553535', country: 'RU' }, 'US', false, metadata).should.equal('+78005553535')\r\n// \t\tformatPhoneNumberForMobileDialing({ phone: '8005553535', country: 'RU' }, 'RU', false, metadata).should.equal('8005553535')\r\n// \t})\r\n// })"],"mappings":"AAAA;AACA;AACA;AACA;AACA;;AAEA,OAAOA,QAAQ,MAAM,eAAe;AACpC,OAAOC,MAAM,MAAM,aAAa;AAChC,OAAOC,aAAa,MAAM,4BAA4B;AACtD,OAAOC,iBAAiB,MAAM,gCAAgC;AAC9D,OAAOC,qBAAqB,MAAM,4BAA4B;AAE9D,MAAMC,8BAA8B,GAAG,KAAK;;AAE5C;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,UAASC,MAAM,EAAEC,YAAY,EAAEC,eAAe,EAAEC,YAAY,EAAE;EAC5E,MAAMC,QAAQ,GAAG,IAAIV,QAAQ,CAACS,YAAY,CAAC;;EAE3C;EACA,IAAI,CAACC,QAAQ,CAACC,UAAU,CAACJ,YAAY,CAAC,EAAE;IACvC,MAAM,I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T,OAAOA,CAAC,GAAGT,gBAAgB,CAACU,MAAM,EAAE;IACnC,MAAMC,SAAS,GAAGX,gBAAgB,CAACS,CAAC,CAAC;IACrC,IAAIF,oBAAoB,CAACI,SAAS,CAAC,EAAE;MACpCH,MAAM,IAAIG,SAAS;IACpB;IACAF,CAAC,EAAE;EACJ;EAEA,OAAOD,MAAM;AACd;AAEA,SAASI,wCAAwCA,CAAA,EAAG;EACnD,MAAM,IAAIlB,KAAK,CAAC,4CAA4C,CAAC;AAC9D;AAEA,SAASmB,mCAAmCA,CAAA,EAAG;EAC9C,MAAM,IAAInB,KAAK,CAAC,4CAA4C,CAAC;AAC9D;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASS,4CAA4CA,CAACf,MAAM,EAAE;EAC7D,OAAOyB,mCAAmC,CACzCzB,MAAM,EACNc,WACD,CAAC;AACF;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA","ignoreList":[]}
|
|
@@ -1,9 +1,27 @@
|
|
|
1
1
|
import Metadata from '../metadata.js';
|
|
2
2
|
import mergeArrays from './mergeArrays.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Checks phone number length against the phone numbering plan selected in metadata.
|
|
6
|
+
* Optionally, it could be restrained by a specific country.
|
|
7
|
+
* @param {string} nationalNumber
|
|
8
|
+
* @param {string?} country — Specific country to use rather than the one that is pre-selected in the metadata instance.
|
|
9
|
+
* @param {Metadata} metadata — Metadata instance with a selected numbering plan.
|
|
10
|
+
* @returns {string}
|
|
11
|
+
*/
|
|
3
12
|
export default function checkNumberLength(nationalNumber, country, metadata) {
|
|
4
|
-
return checkNumberLengthForType(nationalNumber,
|
|
13
|
+
return checkNumberLengthForType(nationalNumber, undefined, country, metadata);
|
|
5
14
|
}
|
|
6
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Checks phone number length against the phone numbering plan selected in metadata.
|
|
18
|
+
* Optionally, it could be restrained by a specific phone number type or a specific country.
|
|
19
|
+
* @param {string} nationalNumber
|
|
20
|
+
* @param {string?} type — Specific phone number type.
|
|
21
|
+
* @param {string?} country — Specific country to use instead of the one that is pre-selected in the metadata instance.
|
|
22
|
+
* @param {Metadata} metadata — Metadata instance with a selected numbering plan.
|
|
23
|
+
* @returns {string} See the README on `validatePhoneNumberLength()` function, with an addition of a special case: when phone numbers of type `type` aren't possible in the telephone numbering plan, it will return "INVALID_LENGTH".
|
|
24
|
+
*/
|
|
7
25
|
// Checks whether a number is possible for a certain `country` based on the number length.
|
|
8
26
|
//
|
|
9
27
|
// This function is not supported by metadata generated with ancient versions of
|
|
@@ -30,11 +48,9 @@ export default function checkNumberLength(nationalNumber, country, metadata) {
|
|
|
30
48
|
// to check phone number length for that specific `country` rather than the "main" country
|
|
31
49
|
// for the shared "country calling code".
|
|
32
50
|
//
|
|
33
|
-
export function checkNumberLengthForType(nationalNumber,
|
|
34
|
-
// If the exact `country` is specified, it's no necessarily already selected in `metadata`.
|
|
35
|
-
// Most likely, in cases when there're multiple countries corresponding to the same
|
|
36
|
-
// "country calling code", the "main" country for that "country calling code" will be selected.
|
|
51
|
+
export function checkNumberLengthForType(nationalNumber, type, country, metadata) {
|
|
37
52
|
if (country) {
|
|
53
|
+
// If a specific `country` is specified, re-create a metadata instance with it.
|
|
38
54
|
metadata = new Metadata(metadata.metadata);
|
|
39
55
|
metadata.selectNumberingPlan(country);
|
|
40
56
|
}
|
|
@@ -60,7 +76,7 @@ export function checkNumberLengthForType(nationalNumber, country, type, metadata
|
|
|
60
76
|
if (!metadata.type('FIXED_LINE')) {
|
|
61
77
|
// The rare case has been encountered where no fixedLine data is available
|
|
62
78
|
// (true for some non-geographic entities), so we just check mobile.
|
|
63
|
-
return checkNumberLengthForType(nationalNumber,
|
|
79
|
+
return checkNumberLengthForType(nationalNumber, 'MOBILE', country, metadata);
|
|
64
80
|
}
|
|
65
81
|
const mobile_type = metadata.type('MOBILE');
|
|
66
82
|
if (mobile_type) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"checkNumberLength.js","names":["Metadata","mergeArrays","checkNumberLength","nationalNumber","country","metadata","checkNumberLengthForType","undefined","type","selectNumberingPlan","type_info","possible_lengths","possibleLengths","mobile_type","actual_length","length","minimum_length","indexOf"],"sources":["../../source/helpers/checkNumberLength.js"],"sourcesContent":["import Metadata from '../metadata.js'\r\nimport mergeArrays from './mergeArrays.js'\r\n\r\nexport default function checkNumberLength(nationalNumber, country, metadata) {\r\n\treturn checkNumberLengthForType(nationalNumber,
|
|
1
|
+
{"version":3,"file":"checkNumberLength.js","names":["Metadata","mergeArrays","checkNumberLength","nationalNumber","country","metadata","checkNumberLengthForType","undefined","type","selectNumberingPlan","type_info","possible_lengths","possibleLengths","mobile_type","actual_length","length","minimum_length","indexOf"],"sources":["../../source/helpers/checkNumberLength.js"],"sourcesContent":["import Metadata from '../metadata.js'\r\nimport mergeArrays from './mergeArrays.js'\r\n\r\n/**\r\n * Checks phone number length against the phone numbering plan selected in metadata.\r\n * Optionally, it could be restrained by a specific country.\r\n * @param {string} nationalNumber\r\n * @param {string?} country — Specific country to use rather than the one that is pre-selected in the metadata instance.\r\n * @param {Metadata} metadata — Metadata instance with a selected numbering plan.\r\n * @returns {string}\r\n */\r\nexport default function checkNumberLength(nationalNumber, country, metadata) {\r\n\treturn checkNumberLengthForType(nationalNumber, undefined, country, metadata)\r\n}\r\n\r\n/**\r\n * Checks phone number length against the phone numbering plan selected in metadata.\r\n * Optionally, it could be restrained by a specific phone number type or a specific country.\r\n * @param {string} nationalNumber\r\n * @param {string?} type — Specific phone number type.\r\n * @param {string?} country — Specific country to use instead of the one that is pre-selected in the metadata instance.\r\n * @param {Metadata} metadata — Metadata instance with a selected numbering plan.\r\n * @returns {string} See the README on `validatePhoneNumberLength()` function, with an addition of a special case: when phone numbers of type `type` aren't possible in the telephone numbering plan, it will return \"INVALID_LENGTH\".\r\n */\r\n// Checks whether a number is possible for a certain `country` based on the number length.\r\n//\r\n// This function is not supported by metadata generated with ancient versions of\r\n// `libphonenumber-js` (before version `1.0.18`) which didn't include \"possible lengths\".\r\n//\r\n// There was also a known issue with `checkNumberLength()` function:\r\n// if a number is possible only in a certain `country` among several `countries`\r\n// that share the same \"country calling code\", that function would check\r\n// the possibility of the phone number only in the \"main\" `country` for the \"country calling code\"\r\n// and would not check if it's actually be possible in the speciifc `country`.\r\n//\r\n// For example, \"+1310xxxx\" numbers are valid in Canada.\r\n// However, they are not possible in the US due to being too short.\r\n// Since Canada and the US share the same country calling code — \"+1\" —\r\n// `checkNumberLength()` function used to return not \"IS_POSSIBLE\" for \"+1310xxxx\" numbers.\r\n//\r\n// In such cases, when using \"/max\" metadata, `isValid()` could output `true`\r\n// but at the same time `isPossible()` could output `false`, which was contradictory.\r\n//\r\n// See https://issuetracker.google.com/issues/335892662 for the discusson in Google's issues.\r\n//\r\n// The solution suggested by Google was implemented: an optional `country` argument\r\n// was added to `checkNumberLength()` function. If present, that `country` will be used\r\n// to check phone number length for that specific `country` rather than the \"main\" country\r\n// for the shared \"country calling code\".\r\n//\r\nexport function checkNumberLengthForType(nationalNumber, type, country, metadata) {\r\n\tif (country) {\r\n\t\t// If a specific `country` is specified, re-create a metadata instance with it.\r\n\t\tmetadata = new Metadata(metadata.metadata)\r\n\t\tmetadata.selectNumberingPlan(country)\r\n\t}\r\n\r\n\tconst type_info = metadata.type(type)\r\n\r\n\t// There should always be \"<possiblePengths/>\" set for every type element.\r\n\t// This is declared in the XML schema.\r\n\t// For size efficiency, where a sub-description (e.g. fixed-line)\r\n\t// has the same \"<possiblePengths/>\" as the \"general description\", this is missing,\r\n\t// so we fall back to the \"general description\". Where no numbers of the type\r\n\t// exist at all, there is one possible length (-1) which is guaranteed\r\n\t// not to match the length of any real phone number.\r\n\tlet possible_lengths = type_info && type_info.possibleLengths() || metadata.possibleLengths()\r\n\t// let local_lengths = type_info && type.possibleLengthsLocal() || metadata.possibleLengthsLocal()\r\n\r\n\t// Metadata before version `1.0.18` didn't contain `possible_lengths`.\r\n\tif (!possible_lengths) {\r\n\t\treturn 'IS_POSSIBLE'\r\n\t}\r\n\r\n\tif (type === 'FIXED_LINE_OR_MOBILE') {\r\n\t\t// No such country in metadata.\r\n\t\t/* istanbul ignore next */\r\n\t\tif (!metadata.type('FIXED_LINE')) {\r\n\t\t\t// The rare case has been encountered where no fixedLine data is available\r\n\t\t\t// (true for some non-geographic entities), so we just check mobile.\r\n\t\t\treturn checkNumberLengthForType(nationalNumber, 'MOBILE', country, metadata)\r\n\t\t}\r\n\r\n\t\tconst mobile_type = metadata.type('MOBILE')\r\n\t\tif (mobile_type) {\r\n\t\t\t// Merge the mobile data in if there was any. \"Concat\" creates a new\r\n\t\t\t// array, it doesn't edit possible_lengths in place, so we don't need a copy.\r\n\t\t\t// Note that when adding the possible lengths from mobile, we have\r\n\t\t\t// to again check they aren't empty since if they are this indicates\r\n\t\t\t// they are the same as the general desc and should be obtained from there.\r\n\t\t\tpossible_lengths = mergeArrays(possible_lengths, mobile_type.possibleLengths())\r\n\t\t\t// The current list is sorted; we need to merge in the new list and\r\n\t\t\t// re-sort (duplicates are okay). Sorting isn't so expensive because\r\n\t\t\t// the lists are very small.\r\n\r\n\t\t\t// if (local_lengths) {\r\n\t\t\t// \tlocal_lengths = mergeArrays(local_lengths, mobile_type.possibleLengthsLocal())\r\n\t\t\t// } else {\r\n\t\t\t// \tlocal_lengths = mobile_type.possibleLengthsLocal()\r\n\t\t\t// }\r\n\t\t}\r\n\t}\r\n\t// If the type doesn't exist then return 'INVALID_LENGTH'.\r\n\telse if (type && !type_info) {\r\n\t\treturn 'INVALID_LENGTH'\r\n\t}\r\n\r\n\tconst actual_length = nationalNumber.length\r\n\r\n\t// In `libphonenumber-js` all \"local-only\" formats are dropped for simplicity.\r\n\t// // This is safe because there is never an overlap beween the possible lengths\r\n\t// // and the local-only lengths; this is checked at build time.\r\n\t// if (local_lengths && local_lengths.indexOf(nationalNumber.length) >= 0)\r\n\t// {\r\n\t// \treturn 'IS_POSSIBLE_LOCAL_ONLY'\r\n\t// }\r\n\r\n\tconst minimum_length = possible_lengths[0]\r\n\r\n\tif (minimum_length === actual_length) {\r\n\t\treturn 'IS_POSSIBLE'\r\n\t}\r\n\r\n\tif (minimum_length > actual_length) {\r\n\t\treturn 'TOO_SHORT'\r\n\t}\r\n\r\n\tif (possible_lengths[possible_lengths.length - 1] < actual_length) {\r\n\t\treturn 'TOO_LONG'\r\n\t}\r\n\r\n\t// We skip the first element since we've already checked it.\r\n\treturn possible_lengths.indexOf(actual_length, 1) >= 0 ? 'IS_POSSIBLE' : 'INVALID_LENGTH'\r\n}"],"mappings":"AAAA,OAAOA,QAAQ,MAAM,gBAAgB;AACrC,OAAOC,WAAW,MAAM,kBAAkB;;AAE1C;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,iBAAiBA,CAACC,cAAc,EAAEC,OAAO,EAAEC,QAAQ,EAAE;EAC5E,OAAOC,wBAAwB,CAACH,cAAc,EAAEI,SAAS,EAAEH,OAAO,EAAEC,QAAQ,CAAC;AAC9E;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,wBAAwBA,CAACH,cAAc,EAAEK,IAAI,EAAEJ,OAAO,EAAEC,QAAQ,EAAE;EACjF,IAAID,OAAO,EAAE;IACZ;IACAC,QAAQ,GAAG,IAAIL,QAAQ,CAACK,QAAQ,CAACA,QAAQ,CAAC;IAC1CA,QAAQ,CAACI,mBAAmB,CAACL,OAAO,CAAC;EACtC;EAEA,MAAMM,SAAS,GAAGL,QAAQ,CAACG,IAAI,CAACA,IAAI,CAAC;;EAErC;EACA;EACA;EACA;EACA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]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"checkNumberLength.test.js","names":["describe","it","expect","Metadata","metadata","type","oldMetadata","checkNumberLength","checkNumberLengthForType","checkNumberLengthWithCountry","to","equal","undefined","
|
|
1
|
+
{"version":3,"file":"checkNumberLength.test.js","names":["describe","it","expect","Metadata","getCountryCallingCode","metadata","type","oldMetadata","checkNumberLength","checkNumberLengthForType","checkNumberLengthWithCountry","to","equal","undefined","metadataInstance","selectNumberingPlan","oldMetadataInstance","number","country"],"sources":["../../source/helpers/checkNumberLength.test.js"],"sourcesContent":["import { describe, it } from 'mocha'\r\nimport { expect } from 'chai'\r\n\r\nimport Metadata, { getCountryCallingCode } from '../metadata.js'\r\nimport metadata from '../../metadata.max.json' with { type: 'json' }\r\nimport oldMetadata from '../../test/metadata/1.0.0/metadata.min.json' with { type: 'json' }\r\n\r\nimport checkNumberLength, { checkNumberLengthForType } from './checkNumberLength.js'\r\n\r\ndescribe('checkNumberLength', () => {\r\n\tit('should check phone number length (`country` is pre-selected in metadata instance)', () => {\r\n\t\t// Too short.\r\n\t\texpect(checkNumberLengthWithCountry('800555353', 'RU', 'FIXED_LINE')).to.equal('TOO_SHORT')\r\n\t\t// Normal.\r\n\t\texpect(checkNumberLengthWithCountry('8005553535', 'RU', 'FIXED_LINE')).to.equal('IS_POSSIBLE')\r\n\t\t// Too long.\r\n\t\texpect(checkNumberLengthWithCountry('80055535355', 'RU', 'FIXED_LINE')).to.equal('TOO_LONG')\r\n\r\n\t\t// No such type.\r\n\t\texpect(checkNumberLengthWithCountry('169454850', 'AC', 'VOIP')).to.equal('INVALID_LENGTH')\r\n\t\t// No such possible length.\r\n\t\texpect(checkNumberLengthWithCountry('1694548', 'AD', undefined)).to.equal('INVALID_LENGTH')\r\n\r\n\t\t// FIXED_LINE_OR_MOBILE\r\n\t\texpect(checkNumberLengthWithCountry('1694548', 'AD', 'FIXED_LINE_OR_MOBILE')).to.equal('INVALID_LENGTH')\r\n\t\t// No mobile phones.\r\n\t\texpect(checkNumberLengthWithCountry('8123', 'TA', 'FIXED_LINE_OR_MOBILE')).to.equal('IS_POSSIBLE')\r\n\t\t// No \"possible lengths\" for \"mobile\".\r\n\t\texpect(checkNumberLengthWithCountry('81234567', 'SZ', 'FIXED_LINE_OR_MOBILE')).to.equal('IS_POSSIBLE')\r\n\t})\r\n\r\n\tit('should check phone number length (`country` override)', () => {\r\n\t\tconst metadataInstance = new Metadata(metadata)\r\n\t\tmetadataInstance.selectNumberingPlan('US')\r\n\r\n\t\t// The country code \"US\" will be overridden with \"CA\".\r\n\t\t//\r\n\t\t// How to find a suitable override for tests:\r\n\t\t//\r\n\t\t// for (const callingCode of Object.keys(metadata.country_calling_codes)) {\r\n\t\t// \tconst countries = metadata.country_calling_codes[callingCode]\r\n\t\t// \tif (countries.length > 1) {\r\n\t\t// \t\tconst mainCountry = countries.shift()\r\n\t\t// \t\tfor (const country of countries) {\r\n\t\t// \t\t\tif (JSON.stringify(metadata.countries[country].possible_lengths) !== JSON.stringify(metadata.countries[mainCountry].possible_lengths)) {\r\n\t\t// \t\t\t\tconsole.log(country, 'has different possible lengths from the default country', mainCountry)\r\n\t\t// \t\t\t}\r\n\t\t// \t\t}\r\n\t\t// \t}\r\n\t\t// }\r\n\r\n\t\t// \"UAN\" phone number type doesn't exist in \"US\".\r\n\t\t// Hence, it returns \"INVALID_LENGTH\".\r\n\t\texpect(checkNumberLengthForType('2133734253', 'UAN', undefined, metadataInstance)).to.equal('INVALID_LENGTH')\r\n\t\texpect(checkNumberLengthForType('2133734253', 'UAN', 'CA', metadataInstance)).to.equal('TOO_LONG')\r\n\t\t// \"UAN\" phone number type doesn't exist in \"US\".\r\n\t\t// Hence, it returns \"INVALID_LENGTH\".\r\n\t\texpect(checkNumberLengthForType('2133734', 'UAN', undefined, metadataInstance)).to.equal('INVALID_LENGTH')\r\n\t\texpect(checkNumberLengthForType('2133734', 'UAN', 'CA', metadataInstance)).to.equal('IS_POSSIBLE')\r\n\t})\r\n\r\n\tit('should work for old metadata (`country` specified)', function() {\r\n\t\tconst oldMetadataInstance = new Metadata(oldMetadata)\r\n\t\toldMetadataInstance.selectNumberingPlan('KZ')\r\n\t\texpect(checkNumberLengthForType('8005553535', 'FIXED_LINE', 'RU', oldMetadataInstance)).to.equal('IS_POSSIBLE')\r\n\t})\r\n\r\n\tit('should work for old metadata (no `country` specified)', function() {\r\n\t\tconst oldMetadataInstance = new Metadata(oldMetadata)\r\n\t\toldMetadataInstance.selectNumberingPlan('RU')\r\n\t\texpect(checkNumberLengthForType('8005553535', 'FIXED_LINE', undefined, oldMetadataInstance)).to.equal('IS_POSSIBLE')\r\n\t})\r\n\r\n\tit('should handle the cases when multiple countries share the same country calling code and a phone number is possible in non-\"main\" country and is not possible in the \"main\" country', () => {\r\n\t\tconst metadataInstance = new Metadata(metadata)\r\n\t\tmetadataInstance.selectNumberingPlan('1')\r\n\t\texpect(checkNumberLength('3100000', undefined, metadataInstance)).to.equal('TOO_SHORT');\r\n\t\texpect(checkNumberLength('3100000', 'US', metadataInstance)).to.equal('TOO_SHORT');\r\n\t\texpect(checkNumberLength('3100000', 'CA', metadataInstance)).to.equal('IS_POSSIBLE');\r\n\t})\r\n})\r\n\r\nfunction checkNumberLengthWithCountry(number, country, type) {\r\n\tconst metadataInstance = new Metadata(metadata)\r\n\tmetadataInstance.selectNumberingPlan(country)\r\n\treturn checkNumberLengthForType(number, type, undefined, metadataInstance)\r\n}"],"mappings":"AAAA,SAASA,QAAQ,EAAEC,EAAE,QAAQ,OAAO;AACpC,SAASC,MAAM,QAAQ,MAAM;AAE7B,OAAOC,QAAQ,IAAIC,qBAAqB,QAAQ,gBAAgB;AAChE,OAAOC,QAAQ,MAAM,yBAAyB,QAAQC,IAAI,EAAE,MAAM;AAClE,OAAOC,WAAW,MAAM,6CAA6C,QAAQD,IAAI,EAAE,MAAM;AAEzF,OAAOE,iBAAiB,IAAIC,wBAAwB,QAAQ,wBAAwB;AAEpFT,QAAQ,CAAC,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C,KAAK,CAAC,aAAa,CAAC;EACrH,CAAC,CAAC;EAEFX,EAAE,CAAC,oLAAoL,EAAE,MAAM;IAC9L,MAAMa,gBAAgB,GAAG,IAAIX,QAAQ,CAACE,QAAQ,CAAC;IAC/CS,gBAAgB,CAACC,mBAAmB,CAAC,GAAG,CAAC;IACzCb,MAAM,CAACM,iBAAiB,CAAC,SAAS,EAAEK,SAAS,EAAEC,gBAAgB,CAAC,CAAC,CAACH,EAAE,CAACC,KAAK,CAAC,WAAW,CAAC;IACvFV,MAAM,CAACM,iBAAiB,CAAC,SAAS,EAAE,IAAI,EAAEM,gBAAgB,CAAC,CAAC,CAACH,EAAE,CAACC,KAAK,CAAC,WAAW,CAAC;IAClFV,MAAM,CAACM,iBAAiB,CAAC,SAAS,EAAE,IAAI,EAAEM,gBAAgB,CAAC,CAAC,CAACH,EAAE,CAACC,KAAK,CAAC,aAAa,CAAC;EACrF,CAAC,CAAC;AACH,CAAC,CAAC;AAEF,SAASF,4BAA4BA,CAACO,MAAM,EAAEC,OAAO,EAAEZ,IAAI,EAAE;EAC5D,MAAMQ,gBAAgB,GAAG,IAAIX,QAAQ,CAACE,QAAQ,CAAC;EAC/CS,gBAAgB,CAACC,mBAAmB,CAACG,OAAO,CAAC;EAC7C,OAAOT,wBAAwB,CAACQ,MAAM,EAAEX,IAAI,EAAEO,SAAS,EAAEC,gBAAgB,CAAC;AAC3E","ignoreList":[]}
|
|
@@ -4,25 +4,30 @@ import Metadata from '../metadata.js';
|
|
|
4
4
|
import { MAX_LENGTH_COUNTRY_CODE } from '../constants.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* (
|
|
7
|
+
* Attempts to extract a calling code from a phone number.
|
|
8
|
+
* * If the phone number is found to be "international":
|
|
9
|
+
* * It will attempt to identify the calling code part of it and whether that part is complete and valid.
|
|
10
|
+
* * If the calling code part is complete and valid, it will return two properties:
|
|
11
|
+
* that calling code part (without `+`) and the rest of the digits.
|
|
12
|
+
* * Otherwise, i.e. if the calling code part is incomplete or invalid,
|
|
13
|
+
* it will return an empty object.
|
|
14
|
+
* * Otherwise, i.e. if the phone number is national, there's no callind code to extract
|
|
15
|
+
* so it will just return the originally-passed `number` string as the only property.
|
|
12
16
|
* @param {string} number — Phone number digits (possibly with a `+`).
|
|
13
17
|
* @param {string} [country] — Country.
|
|
14
18
|
* @param {string} [defaultCountry] — Default country.
|
|
15
19
|
* @param {string} [defaultCallingCode] — Default calling code (some phone numbering plans are non-geographic).
|
|
16
|
-
* @param {object}
|
|
17
|
-
* @return {object} `{ countryCallingCodeSource: string?, countryCallingCode: string?, number: string }`
|
|
20
|
+
* @param {object} metadataJson
|
|
21
|
+
* @return {object} `{ countryCallingCodeSource: string?, countryCallingCode: string?, number: string }`, where `countryCallingCodeSource` tells how the returned calling code was extracted (if it was extracted), `countryCallingCode` is the calling code that was extracted from the input `number` string, and `number` is the originally passed `number` without the extracted calling code (and without a `+`). If the calling code is present but incomplete or invalid, it will return an empty object.
|
|
18
22
|
* @example
|
|
19
23
|
* // Returns `{ countryCallingCode: "1", number: "2133734253" }`.
|
|
24
|
+
* extractCountryCallingCode('2133734253', 'US', null, null, metadata)
|
|
20
25
|
* extractCountryCallingCode('2133734253', null, 'US', null, metadata)
|
|
21
26
|
* extractCountryCallingCode('2133734253', null, null, '1', metadata)
|
|
22
27
|
* extractCountryCallingCode('+12133734253', null, null, null, metadata)
|
|
23
28
|
* extractCountryCallingCode('+12133734253', null, 'RU', null, metadata)
|
|
24
29
|
*/
|
|
25
|
-
export default function extractCountryCallingCode(number, country, defaultCountry, defaultCallingCode,
|
|
30
|
+
export default function extractCountryCallingCode(number, country, defaultCountry, defaultCallingCode, metadataJson) {
|
|
26
31
|
if (!number) {
|
|
27
32
|
return {};
|
|
28
33
|
}
|
|
@@ -38,7 +43,7 @@ export default function extractCountryCallingCode(number, country, defaultCountr
|
|
|
38
43
|
if (number[0] !== '+') {
|
|
39
44
|
// Convert an "out-of-country" dialing phone number
|
|
40
45
|
// to a proper international phone number.
|
|
41
|
-
const numberWithoutIDD = stripIddPrefix(number, country || defaultCountry, defaultCallingCode,
|
|
46
|
+
const numberWithoutIDD = stripIddPrefix(number, country || defaultCountry, defaultCallingCode, metadataJson);
|
|
42
47
|
// If an IDD prefix was stripped then
|
|
43
48
|
// convert the number to international one
|
|
44
49
|
// for subsequent parsing.
|
|
@@ -54,7 +59,7 @@ export default function extractCountryCallingCode(number, country, defaultCountr
|
|
|
54
59
|
const {
|
|
55
60
|
countryCallingCode,
|
|
56
61
|
number: shorterNumber
|
|
57
|
-
} = extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(number, country, defaultCountry, defaultCallingCode,
|
|
62
|
+
} = extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(number, country, defaultCountry, defaultCallingCode, metadataJson);
|
|
58
63
|
if (countryCallingCode) {
|
|
59
64
|
return {
|
|
60
65
|
countryCallingCodeSource: 'FROM_NUMBER_WITHOUT_PLUS_SIGN',
|
|
@@ -77,7 +82,7 @@ export default function extractCountryCallingCode(number, country, defaultCountr
|
|
|
77
82
|
if (number[1] === '0') {
|
|
78
83
|
return {};
|
|
79
84
|
}
|
|
80
|
-
metadata = new Metadata(
|
|
85
|
+
const metadata = new Metadata(metadataJson);
|
|
81
86
|
|
|
82
87
|
// The thing with country phone codes
|
|
83
88
|
// is that they are orthogonal to each other
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extractCountryCallingCode.js","names":["stripIddPrefix","extractCountryCallingCodeFromInternationalNumberWithoutPlusSign","Metadata","MAX_LENGTH_COUNTRY_CODE","extractCountryCallingCode","number","country","defaultCountry","defaultCallingCode","
|
|
1
|
+
{"version":3,"file":"extractCountryCallingCode.js","names":["stripIddPrefix","extractCountryCallingCodeFromInternationalNumberWithoutPlusSign","Metadata","MAX_LENGTH_COUNTRY_CODE","extractCountryCallingCode","number","country","defaultCountry","defaultCallingCode","metadataJson","isNumberWithIddPrefix","numberWithoutIDD","countryCallingCode","shorterNumber","countryCallingCodeSource","metadata","i","length","slice","hasCallingCode","selectNumberingPlan"],"sources":["../../source/helpers/extractCountryCallingCode.js"],"sourcesContent":["import stripIddPrefix from './stripIddPrefix.js'\r\nimport extractCountryCallingCodeFromInternationalNumberWithoutPlusSign from './extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js'\r\nimport Metadata from '../metadata.js'\r\nimport { MAX_LENGTH_COUNTRY_CODE } from '../constants.js'\r\n\r\n/**\r\n * Attempts to extract a calling code from a phone number.\r\n * * If the phone number is found to be \"international\":\r\n * * It will attempt to identify the calling code part of it and whether that part is complete and valid.\r\n * * If the calling code part is complete and valid, it will return two properties:\r\n * that calling code part (without `+`) and the rest of the digits.\r\n * * Otherwise, i.e. if the calling code part is incomplete or invalid,\r\n * it will return an empty object.\r\n * * Otherwise, i.e. if the phone number is national, there's no callind code to extract\r\n * so it will just return the originally-passed `number` string as the only property.\r\n * @param {string} number — Phone number digits (possibly with a `+`).\r\n * @param {string} [country] — Country.\r\n * @param {string} [defaultCountry] — Default country.\r\n * @param {string} [defaultCallingCode] — Default calling code (some phone numbering plans are non-geographic).\r\n * @param {object} metadataJson\r\n * @return {object} `{ countryCallingCodeSource: string?, countryCallingCode: string?, number: string }`, where `countryCallingCodeSource` tells how the returned calling code was extracted (if it was extracted), `countryCallingCode` is the calling code that was extracted from the input `number` string, and `number` is the originally passed `number` without the extracted calling code (and without a `+`). If the calling code is present but incomplete or invalid, it will return an empty object.\r\n * @example\r\n * // Returns `{ countryCallingCode: \"1\", number: \"2133734253\" }`.\r\n * extractCountryCallingCode('2133734253', 'US', null, null, metadata)\r\n * extractCountryCallingCode('2133734253', null, 'US', null, metadata)\r\n * extractCountryCallingCode('2133734253', null, null, '1', metadata)\r\n * extractCountryCallingCode('+12133734253', null, null, null, metadata)\r\n * extractCountryCallingCode('+12133734253', null, 'RU', null, metadata)\r\n */\r\nexport default function extractCountryCallingCode(\r\n\tnumber,\r\n\tcountry,\r\n\tdefaultCountry,\r\n\tdefaultCallingCode,\r\n\tmetadataJson\r\n) {\r\n\tif (!number) {\r\n\t\treturn {}\r\n\t}\r\n\r\n\tlet isNumberWithIddPrefix\r\n\r\n\t// If this is not an international phone number,\r\n\t// then either extract an \"IDD\" prefix, or extract a\r\n\t// country calling code from a number by autocorrecting it\r\n\t// by prepending a leading `+` in cases when it starts\r\n\t// with the country calling code.\r\n\t// https://wikitravel.org/en/International_dialling_prefix\r\n\t// https://github.com/catamphetamine/libphonenumber-js/issues/376\r\n\tif (number[0] !== '+') {\r\n\t\t// Convert an \"out-of-country\" dialing phone number\r\n\t\t// to a proper international phone number.\r\n\t\tconst numberWithoutIDD = stripIddPrefix(number, country || defaultCountry, defaultCallingCode, metadataJson)\r\n\t\t// If an IDD prefix was stripped then\r\n\t\t// convert the number to international one\r\n\t\t// for subsequent parsing.\r\n\t\tif (numberWithoutIDD && numberWithoutIDD !== number) {\r\n\t\t\tisNumberWithIddPrefix = true\r\n\t\t\tnumber = '+' + numberWithoutIDD\r\n\t\t} else {\r\n\t\t\t// Check to see if the number starts with the country calling code\r\n\t\t\t// for the default country. If so, we remove the country calling code,\r\n\t\t\t// and do some checks on the validity of the number before and after.\r\n\t\t\t// https://github.com/catamphetamine/libphonenumber-js/issues/376\r\n\t\t\tif (country || defaultCountry || defaultCallingCode) {\r\n\t\t\t\tconst {\r\n\t\t\t\t\tcountryCallingCode,\r\n\t\t\t\t\tnumber: shorterNumber\r\n\t\t\t\t} = extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(\r\n\t\t\t\t\tnumber,\r\n\t\t\t\t\tcountry,\r\n\t\t\t\t\tdefaultCountry,\r\n\t\t\t\t\tdefaultCallingCode,\r\n\t\t\t\t\tmetadataJson\r\n\t\t\t\t)\r\n\t\t\t\tif (countryCallingCode) {\r\n\t\t\t\t\treturn {\r\n\t\t\t\t\t\tcountryCallingCodeSource: 'FROM_NUMBER_WITHOUT_PLUS_SIGN',\r\n\t\t\t\t\t\tcountryCallingCode,\r\n\t\t\t\t\t\tnumber: shorterNumber\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\treturn {\r\n\t\t\t\t// No need to set it to `UNSPECIFIED`. It can be just `undefined`.\r\n\t\t\t\t// countryCallingCodeSource: 'UNSPECIFIED',\r\n\t\t\t\tnumber\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\t// `number` can only be international at this point.\r\n\r\n\t// Fast abortion: country codes do not begin with a '0'\r\n\tif (number[1] === '0') {\r\n\t\treturn {}\r\n\t}\r\n\r\n\tconst metadata = new Metadata(metadataJson)\r\n\r\n\t// The thing with country phone codes\r\n\t// is that they are orthogonal to each other\r\n\t// i.e. there's no such country phone code A\r\n\t// for which country phone code B exists\r\n\t// where B starts with A.\r\n\t// Therefore, while scanning digits,\r\n\t// if a valid country code is found,\r\n\t// that means that it is the country code.\r\n\t//\r\n\tlet i = 2\r\n\twhile (i - 1 <= MAX_LENGTH_COUNTRY_CODE && i <= number.length) {\r\n\t\tconst countryCallingCode = number.slice(1, i)\r\n\t\tif (metadata.hasCallingCode(countryCallingCode)) {\r\n\t\t\tmetadata.selectNumberingPlan(countryCallingCode)\r\n\t\t\treturn {\r\n\t\t\t\tcountryCallingCodeSource: isNumberWithIddPrefix ? 'FROM_NUMBER_WITH_IDD' : 'FROM_NUMBER_WITH_PLUS_SIGN',\r\n\t\t\t\tcountryCallingCode,\r\n\t\t\t\tnumber: number.slice(i)\r\n\t\t\t}\r\n\t\t}\r\n\t\ti++\r\n\t}\r\n\r\n\treturn {}\r\n}\r\n\r\n// The possible values for the returned `countryCallingCodeSource` are:\r\n//\r\n// Copy-pasted from:\r\n// https://github.com/google/libphonenumber/blob/master/resources/phonenumber.proto\r\n//\r\n// // The source from which the country_code is derived. This is not set in the\r\n// // general parsing method, but in the method that parses and keeps raw_input.\r\n// // New fields could be added upon request.\r\n// enum CountryCodeSource {\r\n// // Default value returned if this is not set, because the phone number was\r\n// // created using parse, not parseAndKeepRawInput. hasCountryCodeSource will\r\n// // return false if this is the case.\r\n// UNSPECIFIED = 0;\r\n//\r\n// // The country_code is derived based on a phone number with a leading \"+\",\r\n// // e.g. the French number \"+33 1 42 68 53 00\".\r\n// FROM_NUMBER_WITH_PLUS_SIGN = 1;\r\n//\r\n// // The country_code is derived based on a phone number with a leading IDD,\r\n// // e.g. the French number \"011 33 1 42 68 53 00\", as it is dialled from US.\r\n// FROM_NUMBER_WITH_IDD = 5;\r\n//\r\n// // The country_code is derived based on a phone number without a leading\r\n// // \"+\", e.g. the French number \"33 1 42 68 53 00\" when defaultCountry is\r\n// // supplied as France.\r\n// FROM_NUMBER_WITHOUT_PLUS_SIGN = 10;\r\n//\r\n// // The country_code is derived NOT based on the phone number itself, but\r\n// // from the defaultCountry parameter provided in the parsing function by the\r\n// // clients. This happens mostly for numbers written in the national format\r\n// // (without country code). For example, this would be set when parsing the\r\n// // French number \"01 42 68 53 00\", when defaultCountry is supplied as\r\n// // France.\r\n// FROM_DEFAULT_COUNTRY = 20;\r\n// }"],"mappings":"AAAA,OAAOA,cAAc,MAAM,qBAAqB;AAChD,OAAOC,+DAA+D,MAAM,sEAAsE;AAClJ,OAAOC,QAAQ,MAAM,gBAAgB;AACrC,SAASC,uBAAuB,QAAQ,iBAAiB;;AAEzD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,yBAAyBA,CAChDC,MAAM,EACNC,OAAO,EACPC,cAAc,EACdC,kBAAkB,EAClBC,YAAY,EACX;EACD,IAAI,CAACJ,MAAM,EAAE;IACZ,OAAO,CAAC,CAAC;EACV;EAEA,IAAIK,qBAAqB;;EAEzB;EACA;EACA;EACA;EACA;EACA;EACA;EACA,IAAIL,MAAM,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE;IACtB;IAC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,4BAA4B;QACvGE,kBAAkB;QAClBP,MAAM,EAAEA,MAAM,CAACa,KAAK,CAACF,CAAC;MACvB,CAAC;IACF;IACAA,CAAC,EAAE;EACJ;EAEA,OAAO,CAAC,CAAC;AACV;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA","ignoreList":[]}
|
package/es6-modern/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js
CHANGED
|
@@ -7,25 +7,36 @@ import getCountryCallingCode from '../getCountryCallingCode.js';
|
|
|
7
7
|
/**
|
|
8
8
|
* Sometimes some people incorrectly input international phone numbers
|
|
9
9
|
* without the leading `+`. This function corrects such input.
|
|
10
|
-
* @param {string} number — Phone number digits.
|
|
10
|
+
* @param {string} number — Phone number digits (only digits, no `+`).
|
|
11
11
|
* @param {string} [country] — Exact country of the phone number.
|
|
12
12
|
* @param {string} [defaultCountry]
|
|
13
13
|
* @param {string} [defaultCallingCode]
|
|
14
|
-
* @param {object}
|
|
15
|
-
* @return {object} `{ countryCallingCode: string?, number: string }
|
|
14
|
+
* @param {object} metadataJson
|
|
15
|
+
* @return {object} `{ countryCallingCode: string?, number: string }`, where `countryCallingCode` is the calling code that was extracted from the input `number` string, and `number` is the originally passed `number` without the extracted calling code.
|
|
16
16
|
*/
|
|
17
|
-
export default function extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(number, country, defaultCountry, defaultCallingCode,
|
|
18
|
-
|
|
17
|
+
export default function extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(number, country, defaultCountry, defaultCallingCode, metadataJson) {
|
|
18
|
+
// Validate arguments.
|
|
19
|
+
// The `number` is known to be in a non-international form
|
|
20
|
+
// because there's no leading "+" character.
|
|
21
|
+
// Therefore, there must be either `country` or `defaultCountry` or `defaultCallingCode`.
|
|
22
|
+
// Otherwise, there'd be no source for the calling code to search for in the `number`.
|
|
23
|
+
if (!(country || defaultCountry || defaultCallingCode)) {
|
|
24
|
+
// There's no source for the calling code to search for in the `number`.
|
|
25
|
+
return {
|
|
26
|
+
number
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
const countryCallingCode = country || defaultCountry ? getCountryCallingCode(country || defaultCountry, metadataJson) : defaultCallingCode;
|
|
19
30
|
if (number.indexOf(countryCallingCode) === 0) {
|
|
20
|
-
metadata = new Metadata(
|
|
21
|
-
metadata.selectNumberingPlan(country || defaultCountry
|
|
31
|
+
const metadata = new Metadata(metadataJson);
|
|
32
|
+
metadata.selectNumberingPlan(country || defaultCountry || defaultCallingCode);
|
|
22
33
|
const possibleShorterNumber = number.slice(countryCallingCode.length);
|
|
23
34
|
const {
|
|
24
35
|
nationalNumber: possibleShorterNationalNumber
|
|
25
|
-
} = extractNationalNumber(possibleShorterNumber,
|
|
36
|
+
} = extractNationalNumber(possibleShorterNumber, undefined, metadata);
|
|
26
37
|
const {
|
|
27
38
|
nationalNumber
|
|
28
|
-
} = extractNationalNumber(number,
|
|
39
|
+
} = extractNationalNumber(number, undefined, metadata);
|
|
29
40
|
|
|
30
41
|
// If the number was not valid before but is valid now,
|
|
31
42
|
// or if it was too long before, we consider the number
|
|
@@ -35,7 +46,7 @@ export default function extractCountryCallingCodeFromInternationalNumberWithoutP
|
|
|
35
46
|
// so if a number starts with `49`, it could be both a valid
|
|
36
47
|
// national German number or an international number without
|
|
37
48
|
// a leading `+`.
|
|
38
|
-
if (!matchesEntirely(nationalNumber, metadata.nationalNumberPattern()) && matchesEntirely(possibleShorterNationalNumber, metadata.nationalNumberPattern()) || checkNumberLength(nationalNumber,
|
|
49
|
+
if (!matchesEntirely(nationalNumber, metadata.nationalNumberPattern()) && matchesEntirely(possibleShorterNationalNumber, metadata.nationalNumberPattern()) || checkNumberLength(nationalNumber, undefined, metadata) === 'TOO_LONG') {
|
|
39
50
|
return {
|
|
40
51
|
countryCallingCode,
|
|
41
52
|
number: possibleShorterNumber
|
package/es6-modern/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js","names":["Metadata","matchesEntirely","extractNationalNumber","checkNumberLength","getCountryCallingCode","extractCountryCallingCodeFromInternationalNumberWithoutPlusSign","number","country","defaultCountry","defaultCallingCode","
|
|
1
|
+
{"version":3,"file":"extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js","names":["Metadata","matchesEntirely","extractNationalNumber","checkNumberLength","getCountryCallingCode","extractCountryCallingCodeFromInternationalNumberWithoutPlusSign","number","country","defaultCountry","defaultCallingCode","metadataJson","countryCallingCode","indexOf","metadata","selectNumberingPlan","possibleShorterNumber","slice","length","nationalNumber","possibleShorterNationalNumber","undefined","nationalNumberPattern"],"sources":["../../source/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js"],"sourcesContent":["import Metadata from '../metadata.js'\r\nimport matchesEntirely from './matchesEntirely.js'\r\nimport extractNationalNumber from './extractNationalNumber.js'\r\nimport checkNumberLength from './checkNumberLength.js'\r\nimport getCountryCallingCode from '../getCountryCallingCode.js'\r\n\r\n/**\r\n * Sometimes some people incorrectly input international phone numbers\r\n * without the leading `+`. This function corrects such input.\r\n * @param {string} number — Phone number digits (only digits, no `+`).\r\n * @param {string} [country] — Exact country of the phone number.\r\n * @param {string} [defaultCountry]\r\n * @param {string} [defaultCallingCode]\r\n * @param {object} metadataJson\r\n * @return {object} `{ countryCallingCode: string?, number: string }`, where `countryCallingCode` is the calling code that was extracted from the input `number` string, and `number` is the originally passed `number` without the extracted calling code.\r\n */\r\nexport default function extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(\r\n\tnumber,\r\n\tcountry,\r\n\tdefaultCountry,\r\n\tdefaultCallingCode,\r\n\tmetadataJson\r\n) {\r\n\t// Validate arguments.\r\n\t// The `number` is known to be in a non-international form\r\n\t// because there's no leading \"+\" character.\r\n\t// Therefore, there must be either `country` or `defaultCountry` or `defaultCallingCode`.\r\n\t// Otherwise, there'd be no source for the calling code to search for in the `number`.\r\n\tif (!(country || defaultCountry || defaultCallingCode)) {\r\n\t\t// There's no source for the calling code to search for in the `number`.\r\n\t\treturn { number }\r\n\t}\r\n\r\n\tconst countryCallingCode = country || defaultCountry\r\n\t\t? getCountryCallingCode(country || defaultCountry, metadataJson)\r\n\t\t: defaultCallingCode\r\n\r\n\tif (number.indexOf(countryCallingCode) === 0) {\r\n\t\tconst metadata = new Metadata(metadataJson)\r\n\t\tmetadata.selectNumberingPlan(country || defaultCountry || defaultCallingCode)\r\n\r\n\t\tconst possibleShorterNumber = number.slice(countryCallingCode.length)\r\n\r\n\t\tconst {\r\n\t\t\tnationalNumber: possibleShorterNationalNumber,\r\n\t\t} = extractNationalNumber(\r\n\t\t\tpossibleShorterNumber,\r\n\t\t\tundefined,\r\n\t\t\tmetadata\r\n\t\t)\r\n\r\n\t\tconst {\r\n\t\t\tnationalNumber\r\n\t\t} = extractNationalNumber(\r\n\t\t\tnumber,\r\n\t\t\tundefined,\r\n\t\t\tmetadata\r\n\t\t)\r\n\r\n\t\t// If the number was not valid before but is valid now,\r\n\t\t// or if it was too long before, we consider the number\r\n\t\t// with the country calling code stripped to be a better result\r\n\t\t// and keep that instead.\r\n\t\t// For example, in Germany (+49), `49` is a valid area code,\r\n\t\t// so if a number starts with `49`, it could be both a valid\r\n\t\t// national German number or an international number without\r\n\t\t// a leading `+`.\r\n\t\tif (\r\n\t\t\t(\r\n\t\t\t\t!matchesEntirely(nationalNumber, metadata.nationalNumberPattern()) &&\r\n\t\t\t\tmatchesEntirely(possibleShorterNationalNumber, metadata.nationalNumberPattern())\r\n\t\t\t)\r\n\t\t\t||\r\n\t\t\tcheckNumberLength(nationalNumber, undefined, metadata) === 'TOO_LONG'\r\n\t\t) {\r\n\t\t\treturn {\r\n\t\t\t\tcountryCall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iB,cAAc,EAAEL,QAAQ,CAACQ,qBAAqB,CAAC,CAAC,CAAC,IAClEpB,eAAe,CAACkB,6BAA6B,EAAEN,QAAQ,CAACQ,qBAAqB,CAAC,CAAC,CAAC,IAGjFlB,iBAAiB,CAACe,cAAc,EAAEE,SAAS,EAAEP,QAAQ,CAAC,KAAK,UAAU,EACpE;MACD,OAAO;QACNF,kBAAkB;QAClBL,MAAM,EAAES;MACT,CAAC;IACF;EACD;EAEA,OAAO;IAAET;EAAO,CAAC;AAClB","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.test.js","names":["describe","it","expect","extractCountryCallingCodeFromInternationalNumberWithoutPlusSign","metadata","type","undefined","to","deep","equal","countryCallingCode","number"],"sources":["../../source/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.test.js"],"sourcesContent":["import { describe, it } from 'mocha'\r\nimport { expect } from 'chai'\r\n\r\nimport extractCountryCallingCodeFromInternationalNumberWithoutPlusSign from './extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js'\r\nimport metadata from '../../metadata.min.json' with { type: 'json' }\r\n\r\ndescribe('helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign', () => {\r\n\tit('should detect international numbers without plus sign', () => {\r\n\t\texpect(extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(\r\n\t\t\t'78005553535',\r\n\t\t\t'RU',\r\n\t\t\tundefined,\r\n\t\t\tundefined,\r\n\t\t\tmetadata\r\n\t\t)).to.deep.equal({\r\n\t\t\tcountryCallingCode: '7',\r\n\t\t\tnumber: '8005553535'\r\n\t\t})\r\n\r\n\t\texpect(extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(\r\n\t\t\t'78005553535',\r\n\t\t\tundefined,\r\n\t\t\t'RU',\r\n\t\t\tundefined,\r\n\t\t\tmetadata\r\n\t\t)).to.deep.equal({\r\n\t\t\tcountryCallingCode: '7',\r\n\t\t\tnumber: '8005553535'\r\n\t\t})\r\n\r\n\t\texpect(extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(\r\n\t\t\t'78005553535',\r\n\t\t\tundefined,\r\n\t\t\tundefined,\r\n\t\t\t'7',\r\n\t\t\tmetadata\r\n\t\t)).to.deep.equal({\r\n\t\t\tcountryCallingCode: '7',\r\n\t\t\tnumber: '8005553535'\r\n\t\t})\r\n\r\n\t\t// No source of country calling code was passed.\r\n\t\texpect(extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(\r\n\t\t\t'78005553535',\r\n\t\t\tundefined,\r\n\t\t\tundefined,\r\n\t\t\tundefined,\r\n\t\t\tmetadata\r\n\t\t)).to.deep.equal({\r\n\t\t\tnumber: '78005553535'\r\n\t\t})\r\n\t})\r\n})"],"mappings":"AAAA,SAASA,QAAQ,EAAEC,EAAE,QAAQ,OAAO;AACpC,SAASC,MAAM,QAAQ,MAAM;AAE7B,OAAOC,+DAA+D,MAAM,sEAAsE;AAClJ,OAAOC,QAAQ,MAAM,yBAAyB,QAAQC,IAAI,EAAE,MAAM;AAElEL,QAAQ,CAAC,yEAAyE,EAAE,MAAM;EACzFC,EAAE,CAAC,uDAAuD,EAAE,MAAM;IACjEC,MAAM,CAACC,+DAA+D,CACrE,aAAa,EACb,IAAI,EACJG,SAAS,EACTA,SAAS,EACTF,QACD,CAAC,CAAC,CAACG,EAAE,CAACC,IAAI,CAACC,KAAK,CAAC;MAChBC,kBAAkB,EAAE,GAAG;MACvBC,MAAM,EAAE;IACT,CAAC,CAAC;IAEFT,MAAM,CAACC,+DAA+D,CACrE,aAAa,EACbG,SAAS,EACT,IAAI,EACJA,SAAS,EACTF,QACD,CAAC,CAAC,CAACG,EAAE,CAACC,IAAI,CAACC,KAAK,CAAC;MAChBC,kBAAkB,EAAE,GAAG;MACvBC,MAAM,EAAE;IACT,CAAC,CAAC;IAEFT,MAAM,CAACC,+DAA+D,CACrE,aAAa,EACbG,SAAS,EACTA,SAAS,EACT,GAAG,EACHF,QACD,CAAC,CAAC,CAACG,EAAE,CAACC,IAAI,CAACC,KAAK,CAAC;MAChBC,kBAAkB,EAAE,GAAG;MACvBC,MAAM,EAAE;IACT,CAAC,CAAC;;IAEF;IACAT,MAAM,CAACC,+DAA+D,CACrE,aAAa,EACbG,SAAS,EACTA,SAAS,EACTA,SAAS,EACTF,QACD,CAAC,CAAC,CAACG,EAAE,CAACC,IAAI,CAACC,KAAK,CAAC;MAChBE,MAAM,EAAE;IACT,CAAC,CAAC;EACH,CAAC,CAAC;AACH,CAAC,CAAC","ignoreList":[]}
|