isValid.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = isValidNumber;
  6. var _metadata = _interopRequireDefault(require("./metadata.js"));
  7. var _matchesEntirely = _interopRequireDefault(require("./helpers/matchesEntirely.js"));
  8. var _getNumberType = _interopRequireDefault(require("./helpers/getNumberType.js"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  10. /**
  11. * Checks if a given phone number is valid.
  12. *
  13. * isValid(phoneNumberInstance, { ..., v2: true }, metadata)
  14. *
  15. * isPossible({ phone: '8005553535', country: 'RU' }, { ... }, metadata)
  16. * isPossible({ phone: '8005553535', country: 'RU' }, undefined, metadata)
  17. *
  18. * If the `number` is a string, it will be parsed to an object,
  19. * but only if it contains only valid phone number characters (including punctuation).
  20. * If the `number` is an object, it is used as is.
  21. *
  22. * The optional `defaultCountry` argument is the default country.
  23. * I.e. it does not restrict to just that country,
  24. * e.g. in those cases where several countries share
  25. * the same phone numbering rules (NANPA, Britain, etc).
  26. * For example, even though the number `07624 369230`
  27. * belongs to the Isle of Man ("IM" country code)
  28. * calling `isValidNumber('07624369230', 'GB', metadata)`
  29. * still returns `true` because the country is not restricted to `GB`,
  30. * it's just that `GB` is the default one for the phone numbering rules.
  31. * For restricting the country see `isValidNumberForRegion()`
  32. * though restricting a country might not be a good idea.
  33. * https://github.com/googlei18n/libphonenumber/blob/master/FAQ.md#when-should-i-use-isvalidnumberforregion
  34. *
  35. * Examples:
  36. *
  37. * ```js
  38. * isValidNumber('+78005553535', metadata)
  39. * isValidNumber('8005553535', 'RU', metadata)
  40. * isValidNumber('88005553535', 'RU', metadata)
  41. * isValidNumber({ phone: '8005553535', country: 'RU' }, metadata)
  42. * ```
  43. */
  44. function isValidNumber(input, options, metadata) {
  45. // If assigning the `{}` default value is moved to the arguments above,
  46. // code coverage would decrease for some weird reason.
  47. options = options || {};
  48. metadata = new _metadata["default"](metadata);
  49. /**
  50. * Checks if a phone number is "possible" (basically just checks its length).
  51. *
  52. * @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: '...' }`.
  53. * @param {object} [options]
  54. * @param {object} metadata
  55. * @return {string}
  56. */
  57. metadata.selectNumberingPlan(input.country, input.countryCallingCode); // By default, countries only have type regexps when it's required for
  58. // distinguishing different countries having the same `countryCallingCode`.
  59. if (metadata.hasTypes()) {
  60. return (0, _getNumberType["default"])(input, options, metadata.metadata) !== undefined;
  61. } // If there are no type regexps for this country in metadata then use
  62. // `nationalNumberPattern` as a "better than nothing" replacement.
  63. var nationalNumber = options.v2 ? input.nationalNumber : input.phone;
  64. return (0, _matchesEntirely["default"])(nationalNumber, metadata.nationalNumberPattern());
  65. }
  66. //# sourceMappingURL=isValid.js.map