isPossible.js 3.2 KB

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