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/parse.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse.js","names":["VALID_DIGITS","PLUS_CHARS","MIN_LENGTH_FOR_NSN","MAX_LENGTH_FOR_NSN","ParseError","Metadata","isViablePhoneNumber","isViablePhoneNumberStart","extractExtension","parseIncompletePhoneNumber","getCountryCallingCode","isPossibleNumber","PhoneNumber","matchesEntirely","extractCountryCallingCode","extractNationalNumber","getCountryByCallingCode","extractFormattedPhoneNumberFromPossibleRfc3966NumberUri","MAX_INPUT_STRING_LENGTH","PHONE_NUMBER_START_PATTERN","RegExp","AFTER_PHONE_NUMBER_END_PATTERN","USE_NON_GEOGRAPHIC_COUNTRY_CODE","parse","text","options","metadata","defaultCountry","hasCountry","v2","Error","number","formattedPhoneNumber","ext","error","parseInput","extract","country","nationalNumber","countryCallingCode","countryCallingCodeSource","carrierCode","parsePhoneNumber","defaultCallingCode","hasSelectedNumberingPlan","length","phoneNumber","__countryCallingCodeSource","valid","extended","nationalNumberPattern","result","possible","possibleLengths","phone","extractFormattedPhoneNumber","throwOnError","startsAt","search","slice","replace","withExtensionStripped","undefined","selectNumberingPlan","isNonGeographicCallingCode","exactCountry"],"sources":["../source/parse.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 {\r\n\tVALID_DIGITS,\r\n\tPLUS_CHARS,\r\n\tMIN_LENGTH_FOR_NSN,\r\n\tMAX_LENGTH_FOR_NSN\r\n} from './constants.js'\r\n\r\nimport ParseError from './ParseError.js'\r\nimport Metadata from './metadata.js'\r\nimport isViablePhoneNumber, { isViablePhoneNumberStart } from './helpers/isViablePhoneNumber.js'\r\nimport extractExtension from './helpers/extension/extractExtension.js'\r\nimport parseIncompletePhoneNumber from './parseIncompletePhoneNumber.js'\r\nimport getCountryCallingCode from './getCountryCallingCode.js'\r\nimport { isPossibleNumber } from './isPossible.js'\r\n// import { parseRFC3966 } from './helpers/RFC3966.js'\r\nimport PhoneNumber from './PhoneNumber.js'\r\nimport matchesEntirely from './helpers/matchesEntirely.js'\r\nimport extractCountryCallingCode from './helpers/extractCountryCallingCode.js'\r\nimport extractNationalNumber from './helpers/extractNationalNumber.js'\r\n// import stripIddPrefix from './helpers/stripIddPrefix.js'\r\nimport getCountryByCallingCode from './helpers/getCountryByCallingCode.js'\r\nimport extractFormattedPhoneNumberFromPossibleRfc3966NumberUri from './helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js'\r\n\r\n// We don't allow input strings for parsing to be longer than 250 chars.\r\n// This prevents malicious input from consuming CPU.\r\nconst MAX_INPUT_STRING_LENGTH = 250\r\n\r\n// This consists of the plus symbol, digits, and arabic-indic digits.\r\nconst PHONE_NUMBER_START_PATTERN = new RegExp('[' + PLUS_CHARS + VALID_DIGITS + ']')\r\n\r\n// Regular expression of trailing characters that we want to remove.\r\n// A trailing `#` is sometimes used when writing phone numbers with extensions in US.\r\n// Example: \"+1 (645) 123 1234-910#\" number has extension \"910\".\r\nconst AFTER_PHONE_NUMBER_END_PATTERN = new RegExp('[^' + VALID_DIGITS + '#' + ']+$')\r\n\r\nconst USE_NON_GEOGRAPHIC_COUNTRY_CODE = false\r\n\r\n// Examples:\r\n//\r\n// ```js\r\n// parse('8 (800) 555-35-35', 'RU')\r\n// parse('8 (800) 555-35-35', 'RU', metadata)\r\n// parse('8 (800) 555-35-35', { country: { default: 'RU' } })\r\n// parse('8 (800) 555-35-35', { country: { default: 'RU' } }, metadata)\r\n// parse('+7 800 555 35 35')\r\n// parse('+7 800 555 35 35', metadata)\r\n// ```\r\n//\r\n\r\n/**\r\n * Parses a phone number.\r\n *\r\n * parse('123456789', { defaultCountry: 'RU', v2: true }, metadata)\r\n * parse('123456789', { defaultCountry: 'RU' }, metadata)\r\n * parse('123456789', undefined, metadata)\r\n *\r\n * @param {string} input\r\n * @param {object} [options]\r\n * @param {object} metadata\r\n * @return {object|PhoneNumber?} If `options.v2: true` flag is passed, it returns a `PhoneNumber?` instance. Otherwise, returns an object of shape `{ phone: '...', country: '...' }` (or just `{}` if no phone number was parsed).\r\n */\r\nexport default function parse(text, options, metadata) {\r\n\t// If assigning the `{}` default value is moved to the arguments above,\r\n\t// code coverage would decrease for some weird reason.\r\n\toptions = options || {}\r\n\r\n\tmetadata = new Metadata(metadata)\r\n\r\n\t// Validate `defaultCountry`.\r\n\tif (options.defaultCountry && !metadata.hasCountry(options.defaultCountry)) {\r\n\t\tif (options.v2) {\r\n\t\t\tthrow new ParseError('INVALID_COUNTRY')\r\n\t\t}\r\n\t\tthrow new Error(`Unknown country: ${options.defaultCountry}`)\r\n\t}\r\n\r\n\t// Parse the phone number.\r\n\tconst { number: formattedPhoneNumber, ext, error } = parseInput(text, options.v2, options.extract)\r\n\r\n\t// If the phone number is not viable then return nothing.\r\n\tif (!formattedPhoneNumber) {\r\n\t\tif (options.v2) {\r\n\t\t\tif (error === 'TOO_SHORT') {\r\n\t\t\t\tthrow new ParseError('TOO_SHORT')\r\n\t\t\t}\r\n\t\t\tthrow new ParseError('NOT_A_NUMBER')\r\n\t\t}\r\n\t\treturn {}\r\n\t}\r\n\r\n\tconst {\r\n\t\tcountry,\r\n\t\tnationalNumber,\r\n\t\tcountryCallingCode,\r\n\t\tcountryCallingCodeSource,\r\n\t\tcarrierCode\r\n\t} = parsePhoneNumber(\r\n\t\tformattedPhoneNumber,\r\n\t\toptions.defaultCountry,\r\n\t\toptions.defaultCallingCode,\r\n\t\tmetadata\r\n\t)\r\n\r\n\tif (!metadata.hasSelectedNumberingPlan()) {\r\n\t\tif (options.v2) {\r\n\t\t\tthrow new ParseError('INVALID_COUNTRY')\r\n\t\t}\r\n\t\treturn {}\r\n\t}\r\n\r\n\t// Validate national (significant) number length.\r\n\tif (!nationalNumber || nationalNumber.length < MIN_LENGTH_FOR_NSN) {\r\n\t\t// Won't throw here because the regexp already demands length > 1.\r\n\t\t/* istanbul ignore if */\r\n\t\tif (options.v2) {\r\n\t\t\tthrow new ParseError('TOO_SHORT')\r\n\t\t}\r\n\t\t// Google's demo just throws an error in this case.\r\n\t\treturn {}\r\n\t}\r\n\r\n\t// Validate national (significant) number length.\r\n\t//\r\n\t// A sidenote:\r\n\t//\r\n\t// They say that sometimes national (significant) numbers\r\n\t// can be longer than `MAX_LENGTH_FOR_NSN` (e.g. in Germany).\r\n\t// https://github.com/googlei18n/libphonenumber/blob/7e1748645552da39c4e1ba731e47969d97bdb539/resources/phonenumber.proto#L36\r\n\t// Such numbers will just be discarded.\r\n\t//\r\n\tif (nationalNumber.length > MAX_LENGTH_FOR_NSN) {\r\n\t\tif (options.v2) {\r\n\t\t\tthrow new ParseError('TOO_LONG')\r\n\t\t}\r\n\t\t// Google's demo just throws an error in this case.\r\n\t\treturn {}\r\n\t}\r\n\r\n\tif (options.v2) {\r\n\t\tconst phoneNumber = new PhoneNumber(\r\n\t\t\tcountryCallingCode,\r\n\t\t\tnationalNumber,\r\n\t\t\tmetadata.metadata\r\n\t\t)\r\n\t\tif (country) {\r\n\t\t\tphoneNumber.country = country\r\n\t\t}\r\n\t\tif (carrierCode) {\r\n\t\t\tphoneNumber.carrierCode = carrierCode\r\n\t\t}\r\n\t\tif (ext) {\r\n\t\t\tphoneNumber.ext = ext\r\n\t\t}\r\n\t\tphoneNumber.__countryCallingCodeSource = countryCallingCodeSource\r\n\t\treturn phoneNumber\r\n\t}\r\n\r\n\t// Check if national phone number pattern matches the number.\r\n\t// National number pattern is different for each country,\r\n\t// even for those ones which are part of the \"NANPA\" group.\r\n\tconst valid = (options.extended ? metadata.hasSelectedNumberingPlan() : country) ?\r\n\t\tmatchesEntirely(nationalNumber, metadata.nationalNumberPattern()) :\r\n\t\tfalse\r\n\r\n\tif (!options.extended) {\r\n\t\treturn valid ? result(country, nationalNumber, ext) : {}\r\n\t}\r\n\r\n\t// isInternational: countryCallingCode !== undefined\r\n\r\n\treturn {\r\n\t\tcountry,\r\n\t\tcountryCallingCode,\r\n\t\tcarrierCode,\r\n\t\tvalid,\r\n\t\tpossible: valid ? true : (\r\n\t\t\toptions.extended === true &&\r\n\t\t\tmetadata.possibleLengths() &&\r\n\t\t\tisPossibleNumber(nationalNumber, country, metadata) ? true : false\r\n\t\t),\r\n\t\tphone: nationalNumber,\r\n\t\text\r\n\t}\r\n}\r\n\r\n/**\r\n * Extracts a formatted phone number from text.\r\n * Doesn't guarantee that the extracted phone number\r\n * is a valid phone number (for example, doesn't validate its length).\r\n * @param {string} text\r\n * @param {boolean} [extract] — If `false`, then will parse the entire `text` as a phone number.\r\n * @param {boolean} [throwOnError] — By default, it won't throw if the text is too long.\r\n * @return {string}\r\n * @example\r\n * // Returns \"(213) 373-4253\".\r\n * extractFormattedPhoneNumber(\"Call (213) 373-4253 for assistance.\")\r\n */\r\nfunction extractFormattedPhoneNumber(text, extract, throwOnError) {\r\n\tif (!text) {\r\n\t\treturn\r\n\t}\r\n\tif (text.length > MAX_INPUT_STRING_LENGTH) {\r\n\t\tif (throwOnError) {\r\n\t\t\tthrow new ParseError('TOO_LONG')\r\n\t\t}\r\n\t\treturn\r\n\t}\r\n\tif (extract === false) {\r\n\t\treturn text\r\n\t}\r\n\t// Attempt to extract a possible number from the string passed in\r\n\tconst startsAt = text.search(PHONE_NUMBER_START_PATTERN)\r\n\tif (startsAt < 0) {\r\n\t\treturn\r\n\t}\r\n\treturn text\r\n\t\t// Trim everything to the left of the phone number\r\n\t\t.slice(startsAt)\r\n\t\t// Remove trailing non-numerical characters\r\n\t\t.replace(AFTER_PHONE_NUMBER_END_PATTERN, '')\r\n}\r\n\r\n/**\r\n * @param {string} text - Input.\r\n * @param {boolean} v2 - Legacy API functions don't pass `v2: true` flag.\r\n * @param {boolean} [extract] - Whether to extract a phone number from `text`, or attempt to parse the entire text as a phone number.\r\n * @return {object} `{ ?number, ?ext }`.\r\n */\r\nfunction parseInput(text, v2, extract) {\r\n\t// // Parse RFC 3966 phone number URI.\r\n\t// if (text && text.indexOf('tel:') === 0) {\r\n\t// \treturn parseRFC3966(text)\r\n\t// }\r\n\t// let number = extractFormattedPhoneNumber(text, extract, v2)\r\n\tlet number = extractFormattedPhoneNumberFromPossibleRfc3966NumberUri(text, {\r\n\t\textractFormattedPhoneNumber: (text) => extractFormattedPhoneNumber(text, extract, v2)\r\n\t})\r\n\t// If the phone number is not viable, then abort.\r\n\tif (!number) {\r\n\t\treturn {}\r\n\t}\r\n\tif (!isViablePhoneNumber(number)) {\r\n\t\tif (isViablePhoneNumberStart(number)) {\r\n\t\t\treturn { error: 'TOO_SHORT' }\r\n\t\t}\r\n\t\treturn {}\r\n\t}\r\n\t// Attempt to parse extension first, since it doesn't require region-specific\r\n\t// data and we want to have the non-normalised number here.\r\n\tconst withExtensionStripped = extractExtension(number)\r\n\tif (withExtensionStripped.ext) {\r\n\t\treturn withExtensionStripped\r\n\t}\r\n\treturn { number }\r\n}\r\n\r\n/**\r\n * Creates `parse()` result object.\r\n */\r\nfunction result(country, nationalNumber, ext) {\r\n\tconst result = {\r\n\t\tcountry,\r\n\t\tphone: nationalNumber\r\n\t}\r\n\tif (ext) {\r\n\t\tresult.ext = ext\r\n\t}\r\n\treturn result\r\n}\r\n\r\n/**\r\n * Parses a viable phone number.\r\n * @param {string} formattedPhoneNumber — Example: \"(213) 373-4253\".\r\n * @param {string} [defaultCountry]\r\n * @param {string} [defaultCallingCode]\r\n * @param {Metadata} metadata\r\n * @return {object} Returns `{ country: string?, countryCallingCode: string?, nationalNumber: string? }`.\r\n */\r\nfunction parsePhoneNumber(\r\n\tformattedPhoneNumber,\r\n\tdefaultCountry,\r\n\tdefaultCallingCode,\r\n\tmetadata\r\n) {\r\n\t// Extract calling code from phone number.\r\n\tlet { countryCallingCodeSource, countryCallingCode, number } = extractCountryCallingCode(\r\n\t\tparseIncompletePhoneNumber(formattedPhoneNumber),\r\n\t\tundefined,\r\n\t\tdefaultCountry,\r\n\t\tdefaultCallingCode,\r\n\t\tmetadata.metadata\r\n\t)\r\n\r\n\t// The exact country of the phone number\r\n\tlet country\r\n\r\n\t// If `formattedPhoneNumber` is passed in \"international\" format,\r\n\t// choose a country by `countryCallingCode`.\r\n\tif (countryCallingCode) {\r\n\t\tmetadata.selectNumberingPlan(countryCallingCode)\r\n\t}\r\n\t// Else, if `formattedPhoneNumber` is passed in \"national\" format,\r\n\t// then `number` is defined and `countryCallingCode` is `undefined`.\r\n\telse if (number && (defaultCountry || defaultCallingCode)) {\r\n\t\tmetadata.selectNumberingPlan(defaultCountry, defaultCallingCode)\r\n\t\tif (defaultCountry) {\r\n\t\t\tcountry = defaultCountry\r\n\t\t} else {\r\n\t\t\t/* istanbul ignore if */\r\n\t\t\tif (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {\r\n\t\t\t\tif (metadata.isNonGeographicCallingCode(defaultCallingCode)) {\r\n\t\t\t\t\tcountry = '001'\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\tcountryCallingCode = defaultCallingCode || getCountryCallingCode(defaultCountry, metadata.metadata)\r\n\t}\r\n\telse return {}\r\n\r\n\tif (!number) {\r\n\t\treturn {\r\n\t\t\tcountryCallingCodeSource,\r\n\t\t\tcountryCallingCode\r\n\t\t}\r\n\t}\r\n\r\n\tconst {\r\n\t\tnationalNumber,\r\n\t\tcarrierCode\r\n\t} = extractNationalNumber(\r\n\t\tparseIncompletePhoneNumber(number),\r\n\t\tcountry,\r\n\t\tmetadata\r\n\t)\r\n\r\n\t// Sometimes there are several countries\r\n\t// corresponding to the same country phone code\r\n\t// (e.g. NANPA countries all having `1` country phone code).\r\n\t// Therefore, to reliably determine the exact country,\r\n\t// national (significant) number should have been parsed first.\r\n\t//\r\n\t// When `metadata.json` is generated, all \"ambiguous\" country phone codes\r\n\t// get their countries populated with the full set of\r\n\t// \"phone number type\" regular expressions.\r\n\t//\r\n\tconst exactCountry = getCountryByCallingCode(countryCallingCode, {\r\n\t\tnationalNumber,\r\n\t\tmetadata\r\n\t})\r\n\tif (exactCountry) {\r\n\t\tcountry = exactCountry\r\n\t\t/* istanbul ignore if */\r\n\t\tif (exactCountry === '001') {\r\n\t\t\t// Can't happen with `USE_NON_GEOGRAPHIC_COUNTRY_CODE` being `false`.\r\n\t\t\t// If `USE_NON_GEOGRAPHIC_COUNTRY_CODE` is set to `true` for some reason,\r\n\t\t\t// then remove the \"istanbul ignore if\".\r\n\t\t} else {\r\n\t\t\tmetadata.selectNumberingPlan(country)\r\n\t\t}\r\n\t}\r\n\r\n\treturn {\r\n\t\tcountry,\r\n\t\tcountryCallingCode,\r\n\t\tc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N,OAAO,GAAG,KAAK;QAChB;MACD;IACD;IACAE,kBAAkB,GAAGI,kBAAkB,IAAIjC,qBAAqB,CAACiB,cAAc,EAAED,QAAQ,CAACA,QAAQ,CAAC;EACpG,CAAC,MACI,OAAO,CAAC,CAAC;EAEd,IAAI,CAACK,MAAM,EAAE;IACZ,OAAO;MACNS,wBAAwB;MACxBD;IACD,CAAC;EACF;EAEA,MAAM;IACLD,cAAc;IACdG;EACD,CAAC,GAAG1B,qBAAqB,CACxBN,0BAA0B,CAACsB,MAAM,CAAC,EAClCM,OAAO,EACPX,QACD,CAAC;;EAED;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,MAAMuC,YAAY,GAAGjD,uBAAuB,CAACuB,kBAAkB,EAAE;IAChED,cAAc;IACdZ;EACD,CAAC,CAAC;EACF,IAAIuC,YAAY,EAAE;IACjB5B,OAAO,GAAG4B,YAAY;IACtB;IACA,IAAIA,YAAY,KAAK,KAAK,EAAE;MAC3B;MACA;MACA;IAAA,CACA,MAAM;MACNvC,QAAQ,CAACqC,mBAAmB,CAAC1B,OAAO,CAAC;IACtC;EACD;EAEA,OAAO;IACNA,OAAO;IACPE,kBAAkB;IAClBC,wBAAwB;IACxBF,cAAc;IACdG;EACD,CAAC;AACF","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"parse.js","names":["VALID_DIGITS","PLUS_CHARS","MIN_LENGTH_FOR_NSN","MAX_LENGTH_FOR_NSN","ParseError","Metadata","isViablePhoneNumber","isViablePhoneNumberStart","extractExtension","parseIncompletePhoneNumber","getCountryCallingCode","isPossibleNumber","PhoneNumber","matchesEntirely","extractCountryCallingCode","extractNationalNumber","getCountryByCallingCode","extractFormattedPhoneNumberFromPossibleRfc3966NumberUri","MAX_INPUT_STRING_LENGTH","PHONE_NUMBER_START_PATTERN","RegExp","AFTER_PHONE_NUMBER_END_PATTERN","USE_NON_GEOGRAPHIC_COUNTRY_CODE","parse","text","options","metadataJson","metadata","defaultCountry","hasCountry","v2","Error","number","formattedPhoneNumber","ext","error","parseInput","extract","country","nationalNumber","countryCallingCode","countryCallingCodeSource","carrierCode","parsePhoneNumber","defaultCallingCode","hasSelectedNumberingPlan","length","phoneNumber","__countryCallingCodeSource","valid","extended","nationalNumberPattern","result","possible","possibleLengths","phone","extractFormattedPhoneNumber","throwOnError","startsAt","search","slice","replace","withExtensionStripped","undefined","selectNumberingPlan","numberingPlan","callingCode","isNonGeographicCallingCode","exactCountry"],"sources":["../source/parse.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 {\r\n\tVALID_DIGITS,\r\n\tPLUS_CHARS,\r\n\tMIN_LENGTH_FOR_NSN,\r\n\tMAX_LENGTH_FOR_NSN\r\n} from './constants.js'\r\n\r\nimport ParseError from './ParseError.js'\r\nimport Metadata from './metadata.js'\r\nimport isViablePhoneNumber, { isViablePhoneNumberStart } from './helpers/isViablePhoneNumber.js'\r\nimport extractExtension from './helpers/extension/extractExtension.js'\r\nimport parseIncompletePhoneNumber from './parseIncompletePhoneNumber.js'\r\nimport getCountryCallingCode from './getCountryCallingCode.js'\r\nimport { isPossibleNumber } from './isPossible.js'\r\n// import { parseRFC3966 } from './helpers/RFC3966.js'\r\nimport PhoneNumber from './PhoneNumber.js'\r\nimport matchesEntirely from './helpers/matchesEntirely.js'\r\nimport extractCountryCallingCode from './helpers/extractCountryCallingCode.js'\r\nimport extractNationalNumber from './helpers/extractNationalNumber.js'\r\n// import stripIddPrefix from './helpers/stripIddPrefix.js'\r\nimport getCountryByCallingCode from './helpers/getCountryByCallingCode.js'\r\nimport extractFormattedPhoneNumberFromPossibleRfc3966NumberUri from './helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js'\r\n\r\n// We don't allow input strings for parsing to be longer than 250 chars.\r\n// This prevents malicious input from consuming CPU.\r\nconst MAX_INPUT_STRING_LENGTH = 250\r\n\r\n// This consists of the plus symbol, digits, and arabic-indic digits.\r\nconst PHONE_NUMBER_START_PATTERN = new RegExp('[' + PLUS_CHARS + VALID_DIGITS + ']')\r\n\r\n// Regular expression of trailing characters that we want to remove.\r\n// A trailing `#` is sometimes used when writing phone numbers with extensions in US.\r\n// Example: \"+1 (645) 123 1234-910#\" number has extension \"910\".\r\nconst AFTER_PHONE_NUMBER_END_PATTERN = new RegExp('[^' + VALID_DIGITS + '#' + ']+$')\r\n\r\nconst USE_NON_GEOGRAPHIC_COUNTRY_CODE = false\r\n\r\n// Examples:\r\n//\r\n// ```js\r\n// parse('8 (800) 555-35-35', 'RU')\r\n// parse('8 (800) 555-35-35', 'RU', metadata)\r\n// parse('8 (800) 555-35-35', { country: { default: 'RU' } })\r\n// parse('8 (800) 555-35-35', { country: { default: 'RU' } }, metadata)\r\n// parse('+7 800 555 35 35')\r\n// parse('+7 800 555 35 35', metadata)\r\n// ```\r\n//\r\n\r\n/**\r\n * Parses a phone number.\r\n *\r\n * parse('123456789', { defaultCountry: 'RU', v2: true }, metadata)\r\n * parse('123456789', { defaultCountry: 'RU' }, metadata)\r\n * parse('123456789', undefined, metadata)\r\n *\r\n * @param {string} input\r\n * @param {object} [options]\r\n * @param {object} metadataJson\r\n * @return {object|PhoneNumber?} If `options.v2: true` flag is passed, it returns a `PhoneNumber?` instance. Otherwise, returns an object of shape `{ phone: '...', country: '...' }` (or just `{}` if no phone number was parsed).\r\n */\r\nexport default function parse(text, options, metadataJson) {\r\n\t// If assigning the `{}` default value is moved to the arguments above,\r\n\t// code coverage would decrease for some weird reason.\r\n\toptions = options || {}\r\n\r\n\tconst metadata = new Metadata(metadataJson)\r\n\r\n\t// Validate `defaultCountry`.\r\n\tif (options.defaultCountry && !metadata.hasCountry(options.defaultCountry)) {\r\n\t\tif (options.v2) {\r\n\t\t\tthrow new ParseError('INVALID_COUNTRY')\r\n\t\t}\r\n\t\tthrow new Error(`Unknown country: ${options.defaultCountry}`)\r\n\t}\r\n\r\n\t// Parse the phone number.\r\n\tconst { number: formattedPhoneNumber, ext, error } = parseInput(text, options.v2, options.extract)\r\n\r\n\t// If the phone number is not viable then return nothing.\r\n\tif (!formattedPhoneNumber) {\r\n\t\tif (options.v2) {\r\n\t\t\tif (error === 'TOO_SHORT') {\r\n\t\t\t\tthrow new ParseError('TOO_SHORT')\r\n\t\t\t}\r\n\t\t\tthrow new ParseError('NOT_A_NUMBER')\r\n\t\t}\r\n\t\treturn {}\r\n\t}\r\n\r\n\tconst {\r\n\t\tcountry,\r\n\t\tnationalNumber,\r\n\t\tcountryCallingCode,\r\n\t\tcountryCallingCodeSource,\r\n\t\tcarrierCode\r\n\t} = parsePhoneNumber(\r\n\t\tformattedPhoneNumber,\r\n\t\toptions.defaultCountry,\r\n\t\toptions.defaultCallingCode,\r\n\t\t// If `country` is returned, its numbering plan will also be selected in `metadata`.\r\n\t\t// Otherwise, if `countryCallingCode` is returned, its numbering plan will also be selected in `metadata`.\r\n\t\t// Otherwise, if neither `country` nor `countryCallingCode` are returned, no numbering plan will be selected in `metadata`.\r\n\t\tmetadata\r\n\t)\r\n\r\n\tif (!metadata.hasSelectedNumberingPlan()) {\r\n\t\tif (options.v2) {\r\n\t\t\tthrow new ParseError('INVALID_COUNTRY')\r\n\t\t}\r\n\t\treturn {}\r\n\t}\r\n\r\n\t// Validate national (significant) number length.\r\n\tif (!nationalNumber || nationalNumber.length < MIN_LENGTH_FOR_NSN) {\r\n\t\t// Won't throw here because the regexp already demands length > 1.\r\n\t\t/* istanbul ignore if */\r\n\t\tif (options.v2) {\r\n\t\t\tthrow new ParseError('TOO_SHORT')\r\n\t\t}\r\n\t\t// Google's demo just throws an error in this case.\r\n\t\treturn {}\r\n\t}\r\n\r\n\t// Validate national (significant) number length.\r\n\t//\r\n\t// A sidenote:\r\n\t//\r\n\t// They say that sometimes national (significant) numbers\r\n\t// can be longer than `MAX_LENGTH_FOR_NSN` (e.g. in Germany).\r\n\t// https://github.com/googlei18n/libphonenumber/blob/7e1748645552da39c4e1ba731e47969d97bdb539/resources/phonenumber.proto#L36\r\n\t// Such numbers will just be discarded.\r\n\t//\r\n\tif (nationalNumber.length > MAX_LENGTH_FOR_NSN) {\r\n\t\tif (options.v2) {\r\n\t\t\tthrow new ParseError('TOO_LONG')\r\n\t\t}\r\n\t\t// Google's demo just throws an error in this case.\r\n\t\treturn {}\r\n\t}\r\n\r\n\tif (options.v2) {\r\n\t\tconst phoneNumber = new PhoneNumber(\r\n\t\t\tcountryCallingCode,\r\n\t\t\tnationalNumber,\r\n\t\t\tmetadata.metadata\r\n\t\t)\r\n\t\tif (country) {\r\n\t\t\tphoneNumber.country = country\r\n\t\t}\r\n\t\tif (carrierCode) {\r\n\t\t\tphoneNumber.carrierCode = carrierCode\r\n\t\t}\r\n\t\tif (ext) {\r\n\t\t\tphoneNumber.ext = ext\r\n\t\t}\r\n\t\tphoneNumber.__countryCallingCodeSource = countryCallingCodeSource\r\n\t\treturn phoneNumber\r\n\t}\r\n\r\n\t// Check if national phone number pattern matches the number.\r\n\t// National number pattern is different for each country,\r\n\t// even for those ones which are part of the \"NANPA\" group.\r\n\tconst valid = (options.extended ? metadata.hasSelectedNumberingPlan() : country) ?\r\n\t\tmatchesEntirely(nationalNumber, metadata.nationalNumberPattern()) :\r\n\t\tfalse\r\n\r\n\tif (!options.extended) {\r\n\t\treturn valid ? result(country, nationalNumber, ext) : {}\r\n\t}\r\n\r\n\t// isInternational: countryCallingCode !== undefined\r\n\r\n\treturn {\r\n\t\tcountry,\r\n\t\tcountryCallingCode,\r\n\t\tcarrierCode,\r\n\t\tvalid,\r\n\t\tpossible: valid ? true : (\r\n\t\t\toptions.extended === true &&\r\n\t\t\tmetadata.possibleLengths() &&\r\n\t\t\tisPossibleNumber(nationalNumber, metadata) ? true : false\r\n\t\t),\r\n\t\tphone: nationalNumber,\r\n\t\text\r\n\t}\r\n}\r\n\r\n/**\r\n * Extracts a formatted phone number from text.\r\n * Doesn't guarantee that the extracted phone number\r\n * is a valid phone number (for example, doesn't validate its length).\r\n * @param {string} text\r\n * @param {boolean} [extract] — If `false`, then will parse the entire `text` as a phone number.\r\n * @param {boolean} [throwOnError] — By default, it won't throw if the text is too long.\r\n * @return {string}\r\n * @example\r\n * // Returns \"(213) 373-4253\".\r\n * extractFormattedPhoneNumber(\"Call (213) 373-4253 for assistance.\")\r\n */\r\nfunction extractFormattedPhoneNumber(text, extract, throwOnError) {\r\n\tif (!text) {\r\n\t\treturn\r\n\t}\r\n\tif (text.length > MAX_INPUT_STRING_LENGTH) {\r\n\t\tif (throwOnError) {\r\n\t\t\tthrow new ParseError('TOO_LONG')\r\n\t\t}\r\n\t\treturn\r\n\t}\r\n\tif (extract === false) {\r\n\t\treturn text\r\n\t}\r\n\t// Attempt to extract a possible number from the string passed in\r\n\tconst startsAt = text.search(PHONE_NUMBER_START_PATTERN)\r\n\tif (startsAt < 0) {\r\n\t\treturn\r\n\t}\r\n\treturn text\r\n\t\t// Trim everything to the left of the phone number\r\n\t\t.slice(startsAt)\r\n\t\t// Remove trailing non-numerical characters\r\n\t\t.replace(AFTER_PHONE_NUMBER_END_PATTERN, '')\r\n}\r\n\r\n/**\r\n * @param {string} text - Input.\r\n * @param {boolean} v2 - Legacy API functions don't pass `v2: true` flag.\r\n * @param {boolean} [extract] - Whether to extract a phone number from `text`, or attempt to parse the entire text as a phone number.\r\n * @return {object} `{ ?number, ?ext }`.\r\n */\r\nfunction parseInput(text, v2, extract) {\r\n\t// // Parse RFC 3966 phone number URI.\r\n\t// if (text && text.indexOf('tel:') === 0) {\r\n\t// \treturn parseRFC3966(text)\r\n\t// }\r\n\t// let number = extractFormattedPhoneNumber(text, extract, v2)\r\n\tlet number = extractFormattedPhoneNumberFromPossibleRfc3966NumberUri(text, {\r\n\t\textractFormattedPhoneNumber: (text) => extractFormattedPhoneNumber(text, extract, v2)\r\n\t})\r\n\t// If the phone number is not viable, then abort.\r\n\tif (!number) {\r\n\t\treturn {}\r\n\t}\r\n\tif (!isViablePhoneNumber(number)) {\r\n\t\tif (isViablePhoneNumberStart(number)) {\r\n\t\t\treturn { error: 'TOO_SHORT' }\r\n\t\t}\r\n\t\treturn {}\r\n\t}\r\n\t// Attempt to parse extension first, since it doesn't require region-specific\r\n\t// data and we want to have the non-normalised number here.\r\n\tconst withExtensionStripped = extractExtension(number)\r\n\tif (withExtensionStripped.ext) {\r\n\t\treturn withExtensionStripped\r\n\t}\r\n\treturn { number }\r\n}\r\n\r\n/**\r\n * Creates `parse()` result object.\r\n */\r\nfunction result(country, nationalNumber, ext) {\r\n\tconst result = {\r\n\t\tcountry,\r\n\t\tphone: nationalNumber\r\n\t}\r\n\tif (ext) {\r\n\t\tresult.ext = ext\r\n\t}\r\n\treturn result\r\n}\r\n\r\n/**\r\n * Parses a viable phone number.\r\n * @param {string} formattedPhoneNumber — Example: \"(213) 373-4253\".\r\n * @param {string} [defaultCountry]\r\n * @param {string} [defaultCallingCode]\r\n * @param {Metadata} metadata — Metadata instance with no pre-selected numbering plan. If `country` is returned, its numbering plan will also be selected in `metadata`. Otherwise, if `countryCallingCode` is returned, its numbering plan will also be selected in `metadata`. Otherwise, if neither `country` nor `countryCallingCode` are returned, no numbering plan will be selected in `metadata`.\r\n * @return {object} Returns `{ country: string?, countryCallingCode: string?, nationalNumber: string? }`.\r\n */\r\nfunction parsePhoneNumber(\r\n\tformattedPhoneNumber,\r\n\tdefaultCountry,\r\n\tdefaultCallingCode,\r\n\tmetadata\r\n) {\r\n\t// Attempt to extract a calling code from a phone number.\r\n\t// * If the phone number is found to be \"international\":\r\n\t// * It will attempt to identify the calling code part of it and whether that part is complete and valid.\r\n\t// * If the calling code part is complete and valid, it will return two properties:\r\n\t// that calling code part (without `+`) and the rest of the digits.\r\n\t// * Otherwise, i.e. if the calling code part is incomplete or invalid,\r\n\t// it will return an empty object.\r\n\t// * Otherwise, i.e. if the phone number is national, there's no callind code to extract\r\n\t// so it will just return the originally-passed `number` string as the only property.\r\n\tlet {\r\n\t\t// `countryCallingCodeSource` tells how the returned calling code was extracted (if it was extracted).\r\n\t\tcountryCallingCodeSource,\r\n\t\t// `countryCallingCode` is the calling code that was extracted from the input phone number string.\r\n\t\tcountryCallingCode,\r\n\t\t// `number` is the originally passed `number` without the extracted calling code (and without a `+`).\r\n\t\t// If the calling code is present but incomplete or invalid, both `countryCallingCode` and `number`\r\n\t\t// will be returned as `undefined`.\r\n\t\tnumber\r\n\t} = extractCountryCallingCode(\r\n\t\tparseIncompletePhoneNumber(formattedPhoneNumber),\r\n\t\tundefined,\r\n\t\t// `defaultCountry` and `defaultCallingCode` are only used to detect\r\n\t\t// if it's an \"international\" phone number or not. They won't be used\r\n\t\t// to derive the resulting `countryCallingCode` from them, or anything like that.\r\n\t\tdefaultCountry,\r\n\t\tdefaultCallingCode,\r\n\t\tmetadata.metadata\r\n\t)\r\n\r\n\t// The exact country of the phone number\r\n\tlet country\r\n\r\n\t// If `formattedPhoneNumber` is passed in \"international\" format,\r\n\t// choose a country by `countryCallingCode`.\r\n\tif (countryCallingCode) {\r\n\t\tmetadata.selectNumberingPlan(countryCallingCode)\r\n\t}\r\n\t// Else, if `formattedPhoneNumber` is passed in \"national\" format,\r\n\t// then `number` is defined and `countryCallingCode` is `undefined`.\r\n\telse if (number && (defaultCountry || defaultCallingCode)) {\r\n\t\tif (defaultCountry) {\r\n\t\t\tcountry = defaultCountry\r\n\t\t\tmetadata.selectNumberingPlan(defaultCountry)\r\n\t\t\tcountryCallingCode = metadata.numberingPlan.callingCode()\r\n\t\t} else {\r\n\t\t\tmetadata.selectNumberingPlan(defaultCallingCode)\r\n\t\t\tcountryCallingCode = defaultCallingCode\r\n\t\t\t/* istanbul ignore if */\r\n\t\t\tif (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {\r\n\t\t\t\tif (metadata.isNonGeographicCallingCode(countryCallingCode)) {\r\n\t\t\t\t\tcountry = '001'\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\telse return {}\r\n\r\n\t// At this point, `country` could be `undefined` but `countryCallingCode` is always defined.\r\n\t//\r\n\t// Also, if `country` is defined, its numbering plan is selected in `metadata`.\r\n\t// Otherwise, the numering plan for the `countryCallingCode` is selected in `metadata`.\r\n\r\n\tif (!number) {\r\n\t\treturn {\r\n\t\t\tcountryCallingCodeSource,\r\n\t\t\tcountryCallingCode\r\n\t\t}\r\n\t}\r\n\r\n\tconst {\r\n\t\tnationalNumber,\r\n\t\tcarrierCode\r\n\t} = extractNationalNumber(\r\n\t\tparseIncompletePhoneNumber(number),\r\n\t\tundefined,\r\n\t\tmetadata\r\n\t)\r\n\r\n\t// Sometimes there are several countries\r\n\t// corresponding to the same country phone code\r\n\t// (e.g. NANPA countries all having `1` country phone code).\r\n\t// Therefore, to reliably determine the exact country,\r\n\t// national (significant) number should have been parsed first.\r\n\t//\r\n\t// When `metadata.json` is generated, all \"ambiguous\" country phone codes\r\n\t// get their countries populated with the full set of\r\n\t// \"phone number type\" regular expressions.\r\n\t//\r\n\tconst exactCountry = getCountryByCallingCode(countryCallingCode, {\r\n\t\tnationalNumber,\r\n\t\tmetadata\r\n\t})\r\n\tif (exactCountry) {\r\n\t\tcountry = exactCountry\r\n\t\t/* istanbul ignore if */\r\n\t\tif (exactCountry === '001') {\r\n\t\t\t// Can't happen with `USE_NON_GEOGRAPHIC_COUNTRY_CODE` being `false`.\r\n\t\t\t// If `USE_NON_GEOGRAPHIC_COUNTRY_CODE` is set to `true` for some reason,\r\n\t\t\t// then remove the \"istanbul ignore if\".\r\n\t\t} else {\r\n\t\t\tmetadata.selectNumberingPlan(country)\r\n\t\t}\r\n\t}\r\n\r\n\treturn {\r\n\t\tcountry,\r\n\t\tcountryCallingCode,\r\n\t\tcountryCallingCodeSource,\r\n\t\tnationalNumber,\r\n\t\tcarrierCode\r\n\t}\r\n}"],"mappings":"AAAA;AACA;AACA;AACA;;AAEA,SACCA,YAAY,EACZC,UAAU,EACVC,kBAAkB,EAClBC,kBAA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AE;QACpC,IAAIK,QAAQ,CAACwC,0BAA0B,CAAC3B,kBAAkB,CAAC,EAAE;UAC5DF,OAAO,GAAG,KAAK;QAChB;MACD;IACD;EACD,CAAC,MACI,OAAO,CAAC,CAAC;;EAEd;EACA;EACA;EACA;;EAEA,IAAI,CAACN,MAAM,EAAE;IACZ,OAAO;MACNS,wBAAwB;MACxBD;IACD,CAAC;EACF;EAEA,MAAM;IACLD,cAAc;IACdG;EACD,CAAC,GAAG3B,qBAAqB,CACxBN,0BAA0B,CAACuB,MAAM,CAAC,EAClC+B,SAAS,EACTpC,QACD,CAAC;;EAED;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,MAAMyC,YAAY,GAAGpD,uBAAuB,CAACwB,kBAAkB,EAAE;IAChED,cAAc;IACdZ;EACD,CAAC,CAAC;EACF,IAAIyC,YAAY,EAAE;IACjB9B,OAAO,GAAG8B,YAAY;IACtB;IACA,IAAIA,YAAY,KAAK,KAAK,EAAE;MAC3B;MACA;MACA;IAAA,CACA,MAAM;MACNzC,QAAQ,CAACqC,mBAAmB,CAAC1B,OAAO,CAAC;IACtC;EACD;EAEA,OAAO;IACNA,OAAO;IACPE,kBAAkB;IAClBC,wBAAwB;IACxBF,cAAc;IACdG;EACD,CAAC;AACF","ignoreList":[]}
|
|
@@ -17,9 +17,9 @@ export default function validatePhoneNumberLength() {
|
|
|
17
17
|
// Parse phone number.
|
|
18
18
|
try {
|
|
19
19
|
const phoneNumber = parsePhoneNumberWithError(text, options, metadata);
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const result = checkNumberLength(phoneNumber.nationalNumber,
|
|
20
|
+
const metadataInstance = new Metadata(metadata);
|
|
21
|
+
metadataInstance.selectNumberingPlan(phoneNumber.country || phoneNumber.countryCallingCode);
|
|
22
|
+
const result = checkNumberLength(phoneNumber.nationalNumber, undefined, metadataInstance);
|
|
23
23
|
if (result !== 'IS_POSSIBLE') {
|
|
24
24
|
return result;
|
|
25
25
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validatePhoneNumberLength.js","names":["normalizeArguments","parsePhoneNumberWithError","ParseError","Metadata","checkNumberLength","validatePhoneNumberLength","text","options","metadata","arguments","_extends","extract","phoneNumber","selectNumberingPlan","countryCallingCode","result","nationalNumber","
|
|
1
|
+
{"version":3,"file":"validatePhoneNumberLength.js","names":["normalizeArguments","parsePhoneNumberWithError","ParseError","Metadata","checkNumberLength","validatePhoneNumberLength","text","options","metadata","arguments","_extends","extract","phoneNumber","metadataInstance","selectNumberingPlan","country","countryCallingCode","result","nationalNumber","undefined","error","message"],"sources":["../source/validatePhoneNumberLength.js"],"sourcesContent":["import normalizeArguments from './normalizeArguments.js'\r\nimport parsePhoneNumberWithError from './parsePhoneNumberWithError_.js'\r\nimport ParseError from './ParseError.js'\r\nimport Metadata from './metadata.js'\r\nimport checkNumberLength from './helpers/checkNumberLength.js'\r\n\r\nexport default function validatePhoneNumberLength() {\r\n\tlet { text, options, metadata } = normalizeArguments(arguments)\r\n\toptions = {\r\n\t\t...options,\r\n\t\textract: false\r\n\t}\r\n\r\n\t// Parse phone number.\r\n\ttry {\r\n\t\tconst phoneNumber = parsePhoneNumberWithError(text, options, metadata)\r\n\t\tconst metadataInstance = new Metadata(metadata)\r\n\t\tmetadataInstance.selectNumberingPlan(phoneNumber.country || phoneNumber.countryCallingCode)\r\n\t\tconst result = checkNumberLength(phoneNumber.nationalNumber, undefined, metadataInstance)\r\n\t\tif (result !== 'IS_POSSIBLE') {\r\n\t\t\treturn result\r\n\t\t}\r\n\t} catch (error) {\r\n\t\t/* istanbul ignore else */\r\n\t\tif (error instanceof ParseError) {\r\n\t\t\treturn error.message\r\n\t\t} else {\r\n\t\t\tthrow error\r\n\t\t}\r\n\t}\r\n}"],"mappings":";AAAA,OAAOA,kBAAkB,MAAM,yBAAyB;AACxD,OAAOC,yBAAyB,MAAM,iCAAiC;AACvE,OAAOC,UAAU,MAAM,iBAAiB;AACxC,OAAOC,QAAQ,MAAM,eAAe;AACpC,OAAOC,iBAAiB,MAAM,gCAAgC;AAE9D,eAAe,SAASC,yBAAyBA,CAAA,EAAG;EACnD,IAAI;IAAEC,IAAI;IAAEC,OAAO;IAAEC;EAAS,CAAC,GAAGR,kBAAkB,CAACS,SAAS,CAAC;EAC/DF,OAAO,GAAAG,QAAA,KACHH,OAAO;IACVI,OAAO,EAAE;EAAK,EACd;;EAED;EACA,IAAI;IACH,MAAMC,WAAW,GAAGX,yBAAyB,CAACK,IAAI,EAAEC,OAAO,EAAEC,QAAQ,CAAC;IACtE,MAAMK,gBAAgB,GAAG,IAAIV,QAAQ,CAACK,QAAQ,CAAC;IAC/CK,gBAAgB,CAACC,mBAAmB,CAACF,WAAW,CAACG,OAAO,IAAIH,WAAW,CAACI,kBAAkB,CAAC;IAC3F,MAAMC,MAAM,GAAGb,iBAAiB,CAACQ,WAAW,CAACM,cAAc,EAAEC,SAAS,EAAEN,gBAAgB,CAAC;IACzF,IAAII,MAAM,KAAK,aAAa,EAAE;MAC7B,OAAOA,MAAM;IACd;EACD,CAAC,CAAC,OAAOG,KAAK,EAAE;IACf;IACA,IAAIA,KAAK,YAAYlB,UAAU,EAAE;MAChC,OAAOkB,KAAK,CAACC,OAAO;IACrB,CAAC,MAAM;MACN,MAAMD,KAAK;IACZ;EACD;AACD","ignoreList":[]}
|
package/index.d.cts
CHANGED
package/index.d.ts
CHANGED
package/max/index.d.cts
CHANGED
package/max/index.d.ts
CHANGED
package/min/index.d.cts
CHANGED
package/min/index.d.ts
CHANGED
package/mobile/index.d.cts
CHANGED
package/mobile/index.d.ts
CHANGED
package/package.json
CHANGED
package/source/AsYouType.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import Metadata from './metadata.js'
|
|
2
2
|
import PhoneNumber from './PhoneNumber.js'
|
|
3
|
+
import checkNumberLength from './helpers/checkNumberLength.js'
|
|
3
4
|
import AsYouTypeState from './AsYouTypeState.js'
|
|
4
5
|
import AsYouTypeFormatter, { DIGIT_PLACEHOLDER } from './AsYouTypeFormatter.js'
|
|
5
6
|
import AsYouTypeParser, { extractFormattedDigitsAndPlus } from './AsYouTypeParser.js'
|
|
@@ -15,6 +16,9 @@ export default class AsYouType {
|
|
|
15
16
|
* @param {Object} metadata
|
|
16
17
|
*/
|
|
17
18
|
constructor(optionsOrDefaultCountry, metadata) {
|
|
19
|
+
// Metadata instance is created once and stays constant.
|
|
20
|
+
// This is required because other utility classes such as `AsYouTypeFormatter`
|
|
21
|
+
// will use this metadata instance "reference" and will assume that it stays constant.
|
|
18
22
|
this.metadata = new Metadata(metadata)
|
|
19
23
|
const [defaultCountry, defaultCallingCode] = this.getCountryAndCallingCode(optionsOrDefaultCountry)
|
|
20
24
|
// `this.defaultCountry` and `this.defaultCallingCode` aren't required to be in sync.
|
|
@@ -103,7 +107,7 @@ export default class AsYouType {
|
|
|
103
107
|
this.country = country
|
|
104
108
|
},
|
|
105
109
|
onCallingCodeChange: (callingCode, country) => {
|
|
106
|
-
this.metadata.selectNumberingPlan(country
|
|
110
|
+
this.metadata.selectNumberingPlan(country || callingCode)
|
|
107
111
|
this.formatter.reset(this.metadata.numberingPlan, this.state)
|
|
108
112
|
this.parser.reset(this.metadata.numberingPlan)
|
|
109
113
|
}
|
|
@@ -276,13 +280,35 @@ export default class AsYouType {
|
|
|
276
280
|
// entered so far based on the country phone code
|
|
277
281
|
// and the national phone number.
|
|
278
282
|
determineTheCountry() {
|
|
279
|
-
this.
|
|
283
|
+
// Get a more precise country code for this national number.
|
|
284
|
+
// It is possible that the precise country couldn't be determined
|
|
285
|
+
// in cases when there're multiple countries sharing the same calling code.
|
|
286
|
+
// In that case, `country` will be `undefined`.
|
|
287
|
+
const country = getCountryByCallingCode(
|
|
280
288
|
this.isInternational() ? this.state.callingCode : this.defaultCallingCode,
|
|
281
289
|
{
|
|
282
290
|
nationalNumber: this.state.nationalSignificantNumber,
|
|
283
291
|
metadata: this.metadata
|
|
284
292
|
}
|
|
285
|
-
)
|
|
293
|
+
)
|
|
294
|
+
// If the derived country is indeed more precise,
|
|
295
|
+
// or if no country could be derived for the updated phone number.
|
|
296
|
+
if (country !== this.state.country) {
|
|
297
|
+
// Set (or reset) the more precise country.
|
|
298
|
+
this.state.setCountry(country)
|
|
299
|
+
// If a more precise country could be derived.
|
|
300
|
+
if (country) {
|
|
301
|
+
// Select a more precise telephone numbering plan.
|
|
302
|
+
// For example, consider that `AsYouType` instance was created with `defaultCountry: "US"` argument.
|
|
303
|
+
// In that case, the metadata instance will be using "US" metadata.
|
|
304
|
+
// But then the user could input a Canadian phone number, in which case
|
|
305
|
+
// the metadata instance should be switched to Canadian telephone numbering plan.
|
|
306
|
+
// It is required to do that because later `canFormatCompleteNumber()` function will be called,
|
|
307
|
+
// and that function relies on the precise country being pre-selected in the metadata instance.
|
|
308
|
+
// Or, for example, `checkNumberLength()` function also relies on same thing.
|
|
309
|
+
this.metadata.selectNumberingPlan(country)
|
|
310
|
+
}
|
|
311
|
+
}
|
|
286
312
|
}
|
|
287
313
|
|
|
288
314
|
/**
|
|
@@ -343,18 +369,19 @@ export default class AsYouType {
|
|
|
343
369
|
callingCode
|
|
344
370
|
} = this.state
|
|
345
371
|
|
|
346
|
-
// `this._getCountry()` is basically same as `this.state.country`
|
|
347
|
-
// with the only change that it return `undefined` in case of a
|
|
348
|
-
// "non-geographic" numbering plan instead of `"001"` "internal use" value.
|
|
349
|
-
let country = this._getCountry()
|
|
350
|
-
|
|
351
372
|
if (!nationalSignificantNumber) {
|
|
352
373
|
return
|
|
353
374
|
}
|
|
354
375
|
|
|
355
|
-
// `
|
|
356
|
-
//
|
|
357
|
-
//
|
|
376
|
+
// `this._getCountry()` is basically same as `this.state.country`
|
|
377
|
+
// with the only change that it returns `undefined` in case of a
|
|
378
|
+
// "non-geographic" numbering plan instead of `"001"` "internal use" value.
|
|
379
|
+
let country = this._getCountry()
|
|
380
|
+
|
|
381
|
+
// Sidenote:
|
|
382
|
+
// `state.callingCode` and `state.country` are somewhat independent from one another
|
|
383
|
+
// and there could be situations when one is defined but the other is not.
|
|
384
|
+
// See the comment in `AsYouTypeState.js` for more info on when that is possible.
|
|
358
385
|
|
|
359
386
|
if (!country && !callingCode) {
|
|
360
387
|
return
|
|
@@ -370,26 +397,36 @@ export default class AsYouType {
|
|
|
370
397
|
//
|
|
371
398
|
// Because `AsYouType.getNumber()` method is supposed to be a 1:1
|
|
372
399
|
// equivalent for `parsePhoneNumber(AsYouType.getNumberValue())`,
|
|
373
|
-
//
|
|
400
|
+
// it means that it should also behave accordingly in such cases of `country` ambiguity.
|
|
374
401
|
// That's how users of this library would expect it to behave anyway.
|
|
375
402
|
//
|
|
403
|
+
// So here it checks if `state.country` is same as the `defaultCountry`,
|
|
404
|
+
// and if it is, it attempts to get rid of any potential ambiguity.
|
|
405
|
+
//
|
|
376
406
|
if (country) {
|
|
377
407
|
if (country === this.defaultCountry) {
|
|
378
|
-
//
|
|
379
|
-
//
|
|
380
|
-
//
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
408
|
+
// // Create a `Metadata` instance just to get the calling code of the `country`.
|
|
409
|
+
// const metadata = new Metadata(this.metadata.metadata)
|
|
410
|
+
// // `state.callingCode` and `state.country` are somewhat independent from one another
|
|
411
|
+
// // and there could be situations when one is defined but the other is not.
|
|
412
|
+
// // See the comment in `AsYouTypeState.js` for more info on when that is possible.
|
|
413
|
+
// // Here, it only passes `state.country` because there's no need to pass `state.callingCode`
|
|
414
|
+
// // because `state.country` is known to be defined.
|
|
415
|
+
// metadata.selectNumberingPlan(country)
|
|
416
|
+
// const countryCallingCode = metadata.numberingPlan.callingCode()
|
|
417
|
+
|
|
418
|
+
// Instead of getting `countryCallingCode` value above,
|
|
419
|
+
// it could simply read it from the existing metadata instance.
|
|
420
|
+
const countryCallingCode = this.metadata.numberingPlan.callingCode()
|
|
421
|
+
|
|
422
|
+
// Attempt to rule out any potential country code ambiguity, if present.
|
|
423
|
+
const exactCountry = getExactCountryForMultiCountryCallingCode(
|
|
424
|
+
countryCallingCode,
|
|
425
|
+
nationalSignificantNumber,
|
|
426
|
+
this.metadata
|
|
427
|
+
)
|
|
428
|
+
if (exactCountry) {
|
|
429
|
+
country = exactCountry
|
|
393
430
|
}
|
|
394
431
|
}
|
|
395
432
|
}
|
|
@@ -432,6 +469,41 @@ export default class AsYouType {
|
|
|
432
469
|
return phoneNumber.isValid()
|
|
433
470
|
}
|
|
434
471
|
|
|
472
|
+
/**
|
|
473
|
+
* Checks if the phone number length is valid.
|
|
474
|
+
* If it is, nothing is returned.
|
|
475
|
+
* Otherwise, a rejection reason is returned.
|
|
476
|
+
* @return {string?}
|
|
477
|
+
*/
|
|
478
|
+
validateLength() {
|
|
479
|
+
const {
|
|
480
|
+
digits,
|
|
481
|
+
nationalSignificantNumber
|
|
482
|
+
} = this.state
|
|
483
|
+
|
|
484
|
+
if (!digits) {
|
|
485
|
+
return 'NOT_A_NUMBER'
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
if (!this.metadata.numberingPlan) {
|
|
489
|
+
return 'INVALID_COUNTRY'
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
if (!nationalSignificantNumber) {
|
|
493
|
+
return 'TOO_SHORT'
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// `state.callingCode` and `state.country` are somewhat independent from one another
|
|
497
|
+
// and there could be situations when one is defined but the other is not.
|
|
498
|
+
// See the comment in `AsYouTypeState.js` for more info on when that is possible.
|
|
499
|
+
// Here, it uses both: if `country` is defined then it will use that,
|
|
500
|
+
// otherwise it will use `callingCode`.
|
|
501
|
+
const result = checkNumberLength(nationalSignificantNumber, undefined, this.metadata)
|
|
502
|
+
if (result !== 'IS_POSSIBLE') {
|
|
503
|
+
return result
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
435
507
|
/**
|
|
436
508
|
* @deprecated
|
|
437
509
|
* This method is used in `react-phone-number-input/source/input-control.js`
|
|
@@ -456,4 +528,13 @@ export default class AsYouType {
|
|
|
456
528
|
getTemplate() {
|
|
457
529
|
return this.formatter.getTemplate(this.state) || this.getNonFormattedTemplate() || ''
|
|
458
530
|
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
// In situations when multiple countries use the same calling code,
|
|
534
|
+
// this function tells what exact country does a given national (significant) number belong to.
|
|
535
|
+
function getExactCountryForMultiCountryCallingCode(callingCode, nationalSignificantNumber, metadata) {
|
|
536
|
+
const ambiguousCountries = metadata.getCountryCodesForCallingCode(callingCode)
|
|
537
|
+
if (ambiguousCountries.length > 1) {
|
|
538
|
+
return getCountryByNationalNumber(nationalSignificantNumber, ambiguousCountries, metadata.metadata)
|
|
539
|
+
}
|
|
459
540
|
}
|
|
@@ -21,8 +21,8 @@ export default function formatCompleteNumber(state, format, {
|
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
export function canFormatCompleteNumber(nationalSignificantNumber,
|
|
25
|
-
return checkNumberLength(nationalSignificantNumber,
|
|
24
|
+
export function canFormatCompleteNumber(nationalSignificantNumber, metadata) {
|
|
25
|
+
return checkNumberLength(nationalSignificantNumber, undefined, metadata) === 'IS_POSSIBLE'
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
function formatNationalNumberWithAndWithoutNationalPrefixFormattingRule(state, format, {
|
|
@@ -154,7 +154,7 @@ export default class AsYouTypeFormatter {
|
|
|
154
154
|
// If so, we should use this instead of any other formatting template
|
|
155
155
|
// whose leadingDigitsPattern also matches the input."
|
|
156
156
|
//
|
|
157
|
-
if (canFormatCompleteNumber(state.nationalSignificantNumber,
|
|
157
|
+
if (canFormatCompleteNumber(state.nationalSignificantNumber, this.metadata)) {
|
|
158
158
|
for (const format of this.matchingFormats) {
|
|
159
159
|
const formattedCompleteNumber = formatCompleteNumber(
|
|
160
160
|
state,
|
package/source/AsYouTypeState.js
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
// This "state" object simply holds the state of the "AsYouType" parser:
|
|
2
2
|
//
|
|
3
|
+
// Sidenote:
|
|
4
|
+
// `state.callingCode` and `state.country` are somewhat independent from one another
|
|
5
|
+
// and there could be situations when one is defined but the other is not.
|
|
6
|
+
// * Situations when `state.country` would be defined but `state.callingCode` would be `undefined`:
|
|
7
|
+
// * When `defaultCountry` is specified and inputting a phone number not in "international" format.
|
|
8
|
+
// * When `defaultCountry` is specified and inputting a phone number in "international" format,
|
|
9
|
+
// but before the `+` character has been input.
|
|
10
|
+
// * Situations when `state.country` would be `undefined` but `state.callingCode` would be defined:
|
|
11
|
+
// * When `defaultCountry` is not specified and inputting a phone number in "international" format,
|
|
12
|
+
// and the "calling code" part is already complete, but multiple countries share this "calling code"
|
|
13
|
+
// and there's not enough national (significant) number digits yet to determine the exact country.
|
|
14
|
+
// * In any other sigutations, `state.country` and `state.callingCode` are either both defined or both `undefined`.
|
|
15
|
+
// * When they're both defined, `state.callingCode` always corresponds to `state.country`.
|
|
16
|
+
// So both are always consistent in this case.
|
|
17
|
+
//
|
|
3
18
|
// * `country?: string` — The exact country of the phone number, if it could be determined.
|
|
4
19
|
// When inputting a phone number in "international" format, it will derive the `country` from "country calling code" and the phone number digits.
|
|
5
20
|
// When inputting a phone number in "national" format, it will derive the `country` from `defaultCountry` that was specified when creating the `AsYouType` formatter.
|
|
@@ -7,7 +22,6 @@
|
|
|
7
22
|
// * `callingCode?: string` — "Country calling code" that has been extracted from the input phone number.
|
|
8
23
|
// When inputting a phone number in "international" format, it will extract the "country calling code" from the digits that follow the "+" character.
|
|
9
24
|
// When inputting a phone number in "national" format, `callingCode` will be `undefined`.
|
|
10
|
-
// Sidenote: `state.callingCode` is therefore independent from `state.country` and there could be situations when `state.country` is defined by `state.callingCode` is not — that would be when inputting a phone number not in "international" format.
|
|
11
25
|
// * `digits: string` — Phone number digits that have been input so far, including the "+" character, if present. In case of inputting non-arabic digits, those will be converted to arabic ones.
|
|
12
26
|
// * `international: boolean` — Whether the phone number is being input in "international" format, i.e. with a "+" character.
|
|
13
27
|
// * `missingPlus: boolean` — Whether it's a phone number in "international" format that is missing the leading "+" character for some reason — apparently, Google thinks that it's a common mistake when inputting a phone number.
|
package/source/PhoneNumber.js
CHANGED
|
@@ -2,6 +2,7 @@ import Metadata, { validateMetadata } from './metadata.js'
|
|
|
2
2
|
import isPossibleNumber from './isPossible.js'
|
|
3
3
|
import isValidNumber from './isValid.js'
|
|
4
4
|
import getNumberType from './helpers/getNumberType.js'
|
|
5
|
+
import getCountryAndCallingCodeFromOneOfThem from './helpers/getCountryAndCallingCodeFromOneOfThem.js'
|
|
5
6
|
import getPossibleCountriesForNumber from './helpers/getPossibleCountriesForNumber.js'
|
|
6
7
|
import extractCountryCallingCode from './helpers/extractCountryCallingCode.js'
|
|
7
8
|
import isObject from './helpers/isObject.js'
|
|
@@ -57,7 +58,7 @@ export default class PhoneNumber {
|
|
|
57
58
|
validateMetadata(metadata)
|
|
58
59
|
|
|
59
60
|
// Initialize properties.
|
|
60
|
-
const { country, countryCallingCode } =
|
|
61
|
+
const { country, callingCode: countryCallingCode } = getCountryAndCallingCodeFromOneOfThem(
|
|
61
62
|
countryOrCountryCallingCode,
|
|
62
63
|
metadata
|
|
63
64
|
)
|
|
@@ -104,16 +105,29 @@ export default class PhoneNumber {
|
|
|
104
105
|
return this.number === phoneNumber.number && this.ext === phoneNumber.ext
|
|
105
106
|
}
|
|
106
107
|
|
|
107
|
-
//
|
|
108
|
-
//
|
|
109
|
-
//
|
|
110
|
-
//
|
|
111
|
-
//
|
|
112
|
-
//
|
|
108
|
+
// `validateLength()` method was originally meant to be an equivalent for `validatePhoneNumberLength()`.
|
|
109
|
+
//
|
|
110
|
+
// Later, it became apparent that it's not really a true equivalent.
|
|
111
|
+
// The reason is that a `PhoneNumber` instance is not created
|
|
112
|
+
// when the phone number string is too short for it to be considered a valid phone number:
|
|
113
|
+
// * When there must be at least 2 national (significant) number digits: `"1"`.
|
|
114
|
+
// * When the country calling code part of an international number is incomplete: `"+12"`.
|
|
115
|
+
//
|
|
116
|
+
// So leaving this `validateLength()` method here would suggest a hidden anti-pattern
|
|
117
|
+
// of using it instead of `validatePhoneNumberLength()` while ignoring
|
|
118
|
+
// the "too short to be even possible" case from phone number length validation.
|
|
119
|
+
// And ignoring that case wouldn't make any sense in a real-world application
|
|
120
|
+
// because it would still be a valid case that should be handled.
|
|
121
|
+
//
|
|
122
|
+
// Because of that, this method was eventually commented out in order to not introduce
|
|
123
|
+
// that kind of an anti-pattern.
|
|
124
|
+
//
|
|
113
125
|
// validateLength() {
|
|
114
|
-
// const
|
|
115
|
-
//
|
|
116
|
-
//
|
|
126
|
+
// const result = checkNumberLength(
|
|
127
|
+
// this.nationalNumber,
|
|
128
|
+
// undefined,
|
|
129
|
+
// this.getMetadata()
|
|
130
|
+
// )
|
|
117
131
|
// if (result !== 'IS_POSSIBLE') {
|
|
118
132
|
// return result
|
|
119
133
|
// }
|
|
@@ -145,33 +159,4 @@ export default class PhoneNumber {
|
|
|
145
159
|
}
|
|
146
160
|
}
|
|
147
161
|
|
|
148
|
-
const isCountryCode = (value) => /^[A-Z]{2}$/.test(value)
|
|
149
|
-
|
|
150
|
-
function getCountryAndCountryCallingCode(countryOrCountryCallingCode, metadataJson) {
|
|
151
|
-
let country
|
|
152
|
-
let countryCallingCode
|
|
153
|
-
|
|
154
|
-
const metadata = new Metadata(metadataJson)
|
|
155
|
-
// If country code is passed then derive `countryCallingCode` from it.
|
|
156
|
-
// Also store the country code as `.country`.
|
|
157
|
-
if (isCountryCode(countryOrCountryCallingCode)) {
|
|
158
|
-
country = countryOrCountryCallingCode
|
|
159
|
-
metadata.selectNumberingPlan(country)
|
|
160
|
-
countryCallingCode = metadata.countryCallingCode()
|
|
161
|
-
} else {
|
|
162
|
-
countryCallingCode = countryOrCountryCallingCode
|
|
163
|
-
/* istanbul ignore if */
|
|
164
|
-
if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
|
|
165
|
-
if (metadata.isNonGeographicCallingCode(countryCallingCode)) {
|
|
166
|
-
country = '001'
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
return {
|
|
172
|
-
country,
|
|
173
|
-
countryCallingCode
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
162
|
const E164_NUMBER_REGEXP = /^\+\d+$/
|
package/source/format.js
CHANGED
|
@@ -25,10 +25,10 @@ const DEFAULT_OPTIONS = {
|
|
|
25
25
|
* @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: '...' }`.
|
|
26
26
|
* @param {string} format
|
|
27
27
|
* @param {object} [options]
|
|
28
|
-
* @param {object}
|
|
28
|
+
* @param {object} metadataJson
|
|
29
29
|
* @return {string}
|
|
30
30
|
*/
|
|
31
|
-
export default function formatNumber(input, format, options,
|
|
31
|
+
export default function formatNumber(input, format, options, metadataJson) {
|
|
32
32
|
// Apply default options.
|
|
33
33
|
if (options) {
|
|
34
34
|
// Using ES6 "rest spread" syntax here didn't work with `babel`/`istanbul`
|
|
@@ -43,7 +43,7 @@ export default function formatNumber(input, format, options, metadata) {
|
|
|
43
43
|
options = DEFAULT_OPTIONS
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
metadata = new Metadata(
|
|
46
|
+
const metadata = new Metadata(metadataJson)
|
|
47
47
|
|
|
48
48
|
// Normally, the `input` object is supposed to be a `PhoneNumber` class instance.
|
|
49
49
|
// Also, according to the `PhoneNumber` class source code, `country` can't be "001".
|
|
@@ -22,10 +22,11 @@ const REGION_CODE_FOR_NON_GEO_ENTITY = '001'
|
|
|
22
22
|
* @param {string} from_country - the region where the call is being placed.
|
|
23
23
|
* @param {boolean} with_formatting - whether the number should be returned with
|
|
24
24
|
* formatting symbols, such as spaces and dashes.
|
|
25
|
+
* @param {object} metadataJson
|
|
25
26
|
* @return {string}
|
|
26
27
|
*/
|
|
27
|
-
export default function(number, from_country, with_formatting,
|
|
28
|
-
metadata = new Metadata(
|
|
28
|
+
export default function(number, from_country, with_formatting, metadataJson) {
|
|
29
|
+
const metadata = new Metadata(metadataJson)
|
|
29
30
|
|
|
30
31
|
// Validate `from_country`.
|
|
31
32
|
if (!metadata.hasCountry(from_country)) {
|
|
@@ -70,7 +71,7 @@ export default function(number, from_country, with_formatting, metadata) {
|
|
|
70
71
|
metadata.selectNumberingPlan(country)
|
|
71
72
|
|
|
72
73
|
if (can_be_internationally_dialled(number) &&
|
|
73
|
-
checkNumberLength(number.phone,
|
|
74
|
+
checkNumberLength(number.phone, undefined, metadata) !== 'TOO_SHORT') {
|
|
74
75
|
formatted_number = format(number, 'INTERNATIONAL', metadata.metadata)
|
|
75
76
|
}
|
|
76
77
|
else {
|