isValid.test.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. "use strict";
  2. var _metadataMin = _interopRequireDefault(require("../metadata.min.json"));
  3. var _isValid = _interopRequireDefault(require("./isValid.js"));
  4. var _parsePhoneNumber = _interopRequireDefault(require("./parsePhoneNumber.js"));
  5. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  6. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
  7. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
  8. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  9. function isValidNumber() {
  10. var v2;
  11. for (var _len = arguments.length, parameters = new Array(_len), _key = 0; _key < _len; _key++) {
  12. parameters[_key] = arguments[_key];
  13. }
  14. if (parameters.length < 1) {
  15. // `input` parameter.
  16. parameters.push(undefined);
  17. } else {
  18. // Convert string `input` to a `PhoneNumber` instance.
  19. if (typeof parameters[0] === 'string') {
  20. v2 = true;
  21. parameters[0] = (0, _parsePhoneNumber["default"])(parameters[0], _objectSpread(_objectSpread({}, parameters[1]), {}, {
  22. extract: false
  23. }), _metadataMin["default"]);
  24. }
  25. }
  26. if (parameters.length < 2) {
  27. // `options` parameter.
  28. parameters.push(undefined);
  29. } // Set `v2` flag.
  30. parameters[1] = _objectSpread({
  31. v2: v2
  32. }, parameters[1]); // Add `metadata` parameter.
  33. parameters.push(_metadataMin["default"]); // Call the function.
  34. return _isValid["default"].apply(this, parameters);
  35. }
  36. describe('validate', function () {
  37. it('should validate phone numbers', function () {
  38. isValidNumber('+1-213-373-4253').should.equal(true);
  39. isValidNumber('+1-213-373').should.equal(false);
  40. isValidNumber('+1-213-373-4253', undefined).should.equal(true);
  41. isValidNumber('(213) 373-4253', {
  42. defaultCountry: 'US'
  43. }).should.equal(true);
  44. isValidNumber('(213) 37', {
  45. defaultCountry: 'US'
  46. }).should.equal(false);
  47. isValidNumber({
  48. country: 'US',
  49. phone: '2133734253'
  50. }).should.equal(true); // No "types" info: should return `true`.
  51. isValidNumber('+380972423740').should.equal(true);
  52. isValidNumber('0912345678', {
  53. defaultCountry: 'TW'
  54. }).should.equal(true); // Moible numbers starting 07624* are Isle of Man
  55. // which has its own "country code" "IM"
  56. // which is in the "GB" "country calling code" zone.
  57. // So while this number is for "IM" it's still supposed to
  58. // be valid when passed "GB" as a default country.
  59. isValidNumber('07624369230', {
  60. defaultCountry: 'GB'
  61. }).should.equal(true);
  62. });
  63. it('should refine phone number validation in case extended regular expressions are set for a country', function () {
  64. // Germany general validation must pass
  65. isValidNumber('961111111', {
  66. defaultCountry: 'UZ'
  67. }).should.equal(true);
  68. var phoneNumberTypePatterns = _metadataMin["default"].countries.UZ[11]; // Different regular expressions for precise national number validation.
  69. // `types` index in compressed array is `9` for v1.
  70. // For v2 it's 10.
  71. // For v3 it's 11.
  72. _metadataMin["default"].countries.UZ[11] = [["(?:6(?:1(?:22|3[124]|4[1-4]|5[123578]|64)|2(?:22|3[0-57-9]|41)|5(?:22|3[3-7]|5[024-8])|6\\d{2}|7(?:[23]\\d|7[69])|9(?:22|4[1-8]|6[135]))|7(?:0(?:5[4-9]|6[0146]|7[12456]|9[135-8])|1[12]\\d|2(?:22|3[1345789]|4[123579]|5[14])|3(?:2\\d|3[1578]|4[1-35-7]|5[1-57]|61)|4(?:2\\d|3[1-579]|7[1-79])|5(?:22|5[1-9]|6[1457])|6(?:22|3[12457]|4[13-8])|9(?:22|5[1-9])))\\d{5}"], ["6(?:1(?:2(?:98|2[01])|35[0-4]|50\\d|61[23]|7(?:[01][017]|4\\d|55|9[5-9]))|2(?:11\\d|2(?:[12]1|9[01379])|5(?:[126]\\d|3[0-4])|7\\d{2})|5(?:19[01]|2(?:27|9[26])|30\\d|59\\d|7\\d{2})|6(?:2(?:1[5-9]|2[0367]|38|41|52|60)|3[79]\\d|4(?:56|83)|7(?:[07]\\d|1[017]|3[07]|4[047]|5[057]|67|8[0178]|9[79])|9[0-3]\\d)|7(?:2(?:24|3[237]|4[5-9]|7[15-8])|5(?:7[12]|8[0589])|7(?:0\\d|[39][07])|9(?:0\\d|7[079]))|9(?:2(?:1[1267]|5\\d|3[01]|7[0-4])|5[67]\\d|6(?:2[0-26]|8\\d)|7\\d{2}))\\d{4}|7(?:0\\d{3}|1(?:13[01]|6(?:0[47]|1[67]|66)|71[3-69]|98\\d)|2(?:2(?:2[79]|95)|3(?:2[5-9]|6[0-6])|57\\d|7(?:0\\d|1[17]|2[27]|3[37]|44|5[057]|66|88))|3(?:2(?:1[0-6]|21|3[469]|7[159])|33\\d|5(?:0[0-4]|5[579]|9\\d)|7(?:[0-3579]\\d|4[0467]|6[67]|8[078])|9[4-6]\\d)|4(?:2(?:29|5[0257]|6[0-7]|7[1-57])|5(?:1[0-4]|8\\d|9[5-9])|7(?:0\\d|1[024589]|2[0127]|3[0137]|[46][07]|5[01]|7[5-9]|9[079])|9(?:7[015-9]|[89]\\d))|5(?:112|2(?:0\\d|2[29]|[49]4)|3[1568]\\d|52[6-9]|7(?:0[01578]|1[017]|[23]7|4[047]|[5-7]\\d|8[78]|9[079]))|6(?:2(?:2[1245]|4[2-4])|39\\d|41[179]|5(?:[349]\\d|5[0-2])|7(?:0[017]|[13]\\d|22|44|55|67|88))|9(?:22[128]|3(?:2[0-4]|7\\d)|57[05629]|7(?:2[05-9]|3[37]|4\\d|60|7[2579]|87|9[07])))\\d{4}|9[0-57-9]\\d{7}"]]; // Extended validation must not pass for an invalid phone number
  73. isValidNumber('961111111', {
  74. defaultCountry: 'UZ'
  75. }).should.equal(false); // Extended validation must pass for a valid phone number
  76. isValidNumber('912345678', {
  77. defaultCountry: 'UZ'
  78. }).should.equal(true);
  79. _metadataMin["default"].countries.UZ[11] = phoneNumberTypePatterns;
  80. });
  81. it('should work in edge cases', function () {
  82. // No metadata
  83. var thrower = function thrower() {
  84. return (0, _isValid["default"])('+78005553535');
  85. };
  86. thrower.should["throw"]('`metadata` argument not passed'); // // Non-phone-number characters in a phone number
  87. // isValidNumber('+499821958a').should.equal(false)
  88. // isValidNumber('88005553535x', { defaultCountry: 'RU' }).should.equal(false)
  89. // Doesn't have `types` regexps in default metadata.
  90. isValidNumber({
  91. country: 'UA',
  92. phone: '300000000'
  93. }).should.equal(true);
  94. isValidNumber({
  95. country: 'UA',
  96. phone: '200000000'
  97. }).should.equal(false); // // Numerical `value`
  98. // thrower = () => isValidNumber(88005553535, { defaultCountry: 'RU' })
  99. // thrower.should.throw('A phone number must either be a string or an object of shape { phone, [country] }.')
  100. // Long country phone code
  101. isValidNumber('+3725555555').should.equal(true); // // Invalid country
  102. // thrower = () => isValidNumber({ phone: '8005553535', country: 'RUS' })
  103. // thrower.should.throw('Unknown country')
  104. });
  105. it('should accept phone number extensions', function () {
  106. // International
  107. isValidNumber('+12133734253 ext. 123').should.equal(true); // National
  108. isValidNumber('88005553535 x123', {
  109. defaultCountry: 'RU'
  110. }).should.equal(true);
  111. });
  112. it('should validate non-geographic toll-free phone numbers', function () {
  113. isValidNumber('+80074454123').should.equal(true);
  114. });
  115. });
  116. //# sourceMappingURL=isValid.test.js.map