isValid.js 2.9 KB

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