extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = extractCountryCallingCodeFromInternationalNumberWithoutPlusSign;
  6. var _metadata = _interopRequireDefault(require("../metadata.js"));
  7. var _matchesEntirely = _interopRequireDefault(require("./matchesEntirely.js"));
  8. var _extractNationalNumber = _interopRequireDefault(require("./extractNationalNumber.js"));
  9. var _checkNumberLength = _interopRequireDefault(require("./checkNumberLength.js"));
  10. var _getCountryCallingCode = _interopRequireDefault(require("../getCountryCallingCode.js"));
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  12. /**
  13. * Sometimes some people incorrectly input international phone numbers
  14. * without the leading `+`. This function corrects such input.
  15. * @param {string} number — Phone number digits.
  16. * @param {string?} country
  17. * @param {string?} callingCode
  18. * @param {object} metadata
  19. * @return {object} `{ countryCallingCode: string?, number: string }`.
  20. */
  21. function extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(number, country, callingCode, metadata) {
  22. var countryCallingCode = country ? (0, _getCountryCallingCode["default"])(country, metadata) : callingCode;
  23. if (number.indexOf(countryCallingCode) === 0) {
  24. metadata = new _metadata["default"](metadata);
  25. metadata.selectNumberingPlan(country, callingCode);
  26. var possibleShorterNumber = number.slice(countryCallingCode.length);
  27. var _extractNationalNumbe = (0, _extractNationalNumber["default"])(possibleShorterNumber, metadata),
  28. possibleShorterNationalNumber = _extractNationalNumbe.nationalNumber;
  29. var _extractNationalNumbe2 = (0, _extractNationalNumber["default"])(number, metadata),
  30. nationalNumber = _extractNationalNumbe2.nationalNumber; // If the number was not valid before but is valid now,
  31. // or if it was too long before, we consider the number
  32. // with the country calling code stripped to be a better result
  33. // and keep that instead.
  34. // For example, in Germany (+49), `49` is a valid area code,
  35. // so if a number starts with `49`, it could be both a valid
  36. // national German number or an international number without
  37. // a leading `+`.
  38. if (!(0, _matchesEntirely["default"])(nationalNumber, metadata.nationalNumberPattern()) && (0, _matchesEntirely["default"])(possibleShorterNationalNumber, metadata.nationalNumberPattern()) || (0, _checkNumberLength["default"])(nationalNumber, metadata) === 'TOO_LONG') {
  39. return {
  40. countryCallingCode: countryCallingCode,
  41. number: possibleShorterNumber
  42. };
  43. }
  44. }
  45. return {
  46. number: number
  47. };
  48. }
  49. //# sourceMappingURL=extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map