isValidNumberForRegion.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = isValidNumberForRegion;
  6. var _isViablePhoneNumber = _interopRequireDefault(require("../helpers/isViablePhoneNumber.js"));
  7. var _parse = _interopRequireDefault(require("../parse.js"));
  8. var _isValidNumberForRegion_ = _interopRequireDefault(require("./isValidNumberForRegion_.js"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  10. // This function has been deprecated and is not exported as
  11. // `isValidPhoneNumberForCountry()` or `isValidPhoneNumberForRegion()`.
  12. //
  13. // The rationale is:
  14. //
  15. // * We don't use the "region" word, so "country" would be better.
  16. //
  17. // * It could be substituted with:
  18. //
  19. // ```js
  20. // export default function isValidPhoneNumberForCountry(phoneNumberString, country) {
  21. // const phoneNumber = parsePhoneNumber(phoneNumberString, {
  22. // defaultCountry: country,
  23. // // Demand that the entire input string must be a phone number.
  24. // // Otherwise, it would "extract" a phone number from an input string.
  25. // extract: false
  26. // })
  27. // if (!phoneNumber) {
  28. // return false
  29. // }
  30. // if (phoneNumber.country !== country) {
  31. // return false
  32. // }
  33. // return phoneNumber.isValid()
  34. // }
  35. // ```
  36. //
  37. // * Same function could be used for `isPossiblePhoneNumberForCountry()`
  38. // by replacing `isValid()` with `isPossible()`.
  39. //
  40. // * The reason why this function is not exported is because its result is ambiguous.
  41. // Suppose `false` is returned. It could mean any of:
  42. // * Not a phone number.
  43. // * The phone number is valid but belongs to another country or another calling code.
  44. // * The phone number belongs to the correct country but is not valid digit-wise.
  45. // All those three cases should be handled separately from a "User Experience" standpoint.
  46. // Simply showing "Invalid phone number" error in all of those cases would be lazy UX.
  47. function isValidNumberForRegion(number, country, metadata) {
  48. if (typeof number !== 'string') {
  49. throw new TypeError('number must be a string');
  50. }
  51. if (typeof country !== 'string') {
  52. throw new TypeError('country must be a string');
  53. } // `parse` extracts phone numbers from raw text,
  54. // therefore it will cut off all "garbage" characters,
  55. // while this `validate` function needs to verify
  56. // that the phone number contains no "garbage"
  57. // therefore the explicit `isViablePhoneNumber` check.
  58. var input;
  59. if ((0, _isViablePhoneNumber["default"])(number)) {
  60. input = (0, _parse["default"])(number, {
  61. defaultCountry: country
  62. }, metadata);
  63. } else {
  64. input = {};
  65. }
  66. return (0, _isValidNumberForRegion_["default"])(input, country, undefined, metadata);
  67. }
  68. //# sourceMappingURL=isValidNumberForRegion.js.map