isPossible.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import Metadata from './metadata.js';
  2. import checkNumberLength from './helpers/checkNumberLength.js';
  3. /**
  4. * Checks if a phone number is "possible" (basically just checks its length).
  5. *
  6. * isPossible(phoneNumberInstance, { ..., v2: true }, metadata)
  7. *
  8. * isPossible({ phone: '8005553535', country: 'RU' }, { ... }, metadata)
  9. * isPossible({ phone: '8005553535', country: 'RU' }, undefined, metadata)
  10. *
  11. * @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: '...' }`.
  12. * @param {object} [options]
  13. * @param {object} metadata
  14. * @return {string}
  15. */
  16. export default function isPossiblePhoneNumber(input, options, metadata) {
  17. /* istanbul ignore if */
  18. if (options === undefined) {
  19. options = {};
  20. }
  21. metadata = new Metadata(metadata);
  22. if (options.v2) {
  23. if (!input.countryCallingCode) {
  24. throw new Error('Invalid phone number object passed');
  25. }
  26. metadata.selectNumberingPlan(input.countryCallingCode);
  27. } else {
  28. if (!input.phone) {
  29. return false;
  30. }
  31. if (input.country) {
  32. if (!metadata.hasCountry(input.country)) {
  33. throw new Error("Unknown country: ".concat(input.country));
  34. }
  35. metadata.country(input.country);
  36. } else {
  37. if (!input.countryCallingCode) {
  38. throw new Error('Invalid phone number object passed');
  39. }
  40. metadata.selectNumberingPlan(input.countryCallingCode);
  41. }
  42. } // Old metadata (< 1.0.18) had no "possible length" data.
  43. if (metadata.possibleLengths()) {
  44. return isPossibleNumber(input.phone || input.nationalNumber, metadata);
  45. } else {
  46. // There was a bug between `1.7.35` and `1.7.37` where "possible_lengths"
  47. // were missing for "non-geographical" numbering plans.
  48. // Just assume the number is possible in such cases:
  49. // it's unlikely that anyone generated their custom metadata
  50. // in that short period of time (one day).
  51. // This code can be removed in some future major version update.
  52. if (input.countryCallingCode && metadata.isNonGeographicCallingCode(input.countryCallingCode)) {
  53. // "Non-geographic entities" did't have `possibleLengths`
  54. // due to a bug in metadata generation process.
  55. return true;
  56. } else {
  57. throw new Error('Missing "possibleLengths" in metadata. Perhaps the metadata has been generated before v1.0.18.');
  58. }
  59. }
  60. }
  61. export function isPossibleNumber(nationalNumber, metadata) {
  62. //, isInternational) {
  63. switch (checkNumberLength(nationalNumber, metadata)) {
  64. case 'IS_POSSIBLE':
  65. return true;
  66. // This library ignores "local-only" phone numbers (for simplicity).
  67. // See the readme for more info on what are "local-only" phone numbers.
  68. // case 'IS_POSSIBLE_LOCAL_ONLY':
  69. // return !isInternational
  70. default:
  71. return false;
  72. }
  73. }
  74. //# sourceMappingURL=isPossible.js.map