isPossible.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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: ${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. }
  43. // Old metadata (< 1.0.18) had no "possible length" data.
  44. if (metadata.possibleLengths()) {
  45. return isPossibleNumber(input.phone || input.nationalNumber, metadata)
  46. } else {
  47. // There was a bug between `1.7.35` and `1.7.37` where "possible_lengths"
  48. // were missing for "non-geographical" numbering plans.
  49. // Just assume the number is possible in such cases:
  50. // it's unlikely that anyone generated their custom metadata
  51. // in that short period of time (one day).
  52. // This code can be removed in some future major version update.
  53. if (input.countryCallingCode && metadata.isNonGeographicCallingCode(input.countryCallingCode)) {
  54. // "Non-geographic entities" did't have `possibleLengths`
  55. // due to a bug in metadata generation process.
  56. return true
  57. } else {
  58. throw new Error('Missing "possibleLengths" in metadata. Perhaps the metadata has been generated before v1.0.18.');
  59. }
  60. }
  61. }
  62. export function isPossibleNumber(nationalNumber, metadata) { //, 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. }