isViablePhoneNumber.js 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.VALID_PHONE_NUMBER_WITH_EXTENSION = exports.VALID_PHONE_NUMBER = void 0;
  6. exports["default"] = isViablePhoneNumber;
  7. exports.isViablePhoneNumberStart = isViablePhoneNumberStart;
  8. var _constants = require("../constants.js");
  9. var _createExtensionPattern = _interopRequireDefault(require("./extension/createExtensionPattern.js"));
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  11. // Regular expression of viable phone numbers. This is location independent.
  12. // Checks we have at least three leading digits, and only valid punctuation,
  13. // alpha characters and digits in the phone number. Does not include extension
  14. // data. The symbol 'x' is allowed here as valid punctuation since it is often
  15. // used as a placeholder for carrier codes, for example in Brazilian phone
  16. // numbers. We also allow multiple '+' characters at the start.
  17. //
  18. // Corresponds to the following:
  19. // [digits]{minLengthNsn}|
  20. // plus_sign*
  21. // (([punctuation]|[star])*[digits]){3,}([punctuation]|[star]|[digits]|[alpha])*
  22. //
  23. // The first reg-ex is to allow short numbers (two digits long) to be parsed if
  24. // they are entered as "15" etc, but only if there is no punctuation in them.
  25. // The second expression restricts the number of digits to three or more, but
  26. // then allows them to be in international form, and to have alpha-characters
  27. // and punctuation. We split up the two reg-exes here and combine them when
  28. // creating the reg-ex VALID_PHONE_NUMBER_PATTERN itself so we can prefix it
  29. // with ^ and append $ to each branch.
  30. //
  31. // "Note VALID_PUNCTUATION starts with a -,
  32. // so must be the first in the range" (c) Google devs.
  33. // (wtf did they mean by saying that; probably nothing)
  34. //
  35. var MIN_LENGTH_PHONE_NUMBER_PATTERN = '[' + _constants.VALID_DIGITS + ']{' + _constants.MIN_LENGTH_FOR_NSN + '}'; //
  36. // And this is the second reg-exp:
  37. // (see MIN_LENGTH_PHONE_NUMBER_PATTERN for a full description of this reg-exp)
  38. //
  39. var VALID_PHONE_NUMBER = '[' + _constants.PLUS_CHARS + ']{0,1}' + '(?:' + '[' + _constants.VALID_PUNCTUATION + ']*' + '[' + _constants.VALID_DIGITS + ']' + '){3,}' + '[' + _constants.VALID_PUNCTUATION + _constants.VALID_DIGITS + ']*'; // This regular expression isn't present in Google's `libphonenumber`
  40. // and is only used to determine whether the phone number being input
  41. // is too short for it to even consider it a "valid" number.
  42. // This is just a way to differentiate between a really invalid phone
  43. // number like "abcde" and a valid phone number that a user has just
  44. // started inputting, like "+1" or "1": both these cases would be
  45. // considered `NOT_A_NUMBER` by Google's `libphonenumber`, but this
  46. // library can provide a more detailed error message — whether it's
  47. // really "not a number", or is it just a start of a valid phone number.
  48. exports.VALID_PHONE_NUMBER = VALID_PHONE_NUMBER;
  49. var VALID_PHONE_NUMBER_START_REG_EXP = new RegExp('^' + '[' + _constants.PLUS_CHARS + ']{0,1}' + '(?:' + '[' + _constants.VALID_PUNCTUATION + ']*' + '[' + _constants.VALID_DIGITS + ']' + '){1,2}' + '$', 'i');
  50. var VALID_PHONE_NUMBER_WITH_EXTENSION = VALID_PHONE_NUMBER + // Phone number extensions
  51. '(?:' + (0, _createExtensionPattern["default"])() + ')?'; // The combined regular expression for valid phone numbers:
  52. //
  53. exports.VALID_PHONE_NUMBER_WITH_EXTENSION = VALID_PHONE_NUMBER_WITH_EXTENSION;
  54. var VALID_PHONE_NUMBER_PATTERN = new RegExp( // Either a short two-digit-only phone number
  55. '^' + MIN_LENGTH_PHONE_NUMBER_PATTERN + '$' + '|' + // Or a longer fully parsed phone number (min 3 characters)
  56. '^' + VALID_PHONE_NUMBER_WITH_EXTENSION + '$', 'i'); // Checks to see if the string of characters could possibly be a phone number at
  57. // all. At the moment, checks to see that the string begins with at least 2
  58. // digits, ignoring any punctuation commonly found in phone numbers. This method
  59. // does not require the number to be normalized in advance - but does assume
  60. // that leading non-number symbols have been removed, such as by the method
  61. // `extract_possible_number`.
  62. //
  63. function isViablePhoneNumber(number) {
  64. return number.length >= _constants.MIN_LENGTH_FOR_NSN && VALID_PHONE_NUMBER_PATTERN.test(number);
  65. } // This is just a way to differentiate between a really invalid phone
  66. // number like "abcde" and a valid phone number that a user has just
  67. // started inputting, like "+1" or "1": both these cases would be
  68. // considered `NOT_A_NUMBER` by Google's `libphonenumber`, but this
  69. // library can provide a more detailed error message — whether it's
  70. // really "not a number", or is it just a start of a valid phone number.
  71. function isViablePhoneNumberStart(number) {
  72. return VALID_PHONE_NUMBER_START_REG_EXP.test(number);
  73. }
  74. //# sourceMappingURL=isViablePhoneNumber.js.map