isPossible.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. 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; }
  2. 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; }
  3. 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; }
  4. import metadata from '../metadata.min.json' assert { type: 'json' };
  5. import _isPossibleNumber from './isPossible.js';
  6. import parsePhoneNumber from './parsePhoneNumber.js';
  7. function isPossibleNumber() {
  8. var v2;
  9. for (var _len = arguments.length, parameters = new Array(_len), _key = 0; _key < _len; _key++) {
  10. parameters[_key] = arguments[_key];
  11. }
  12. if (parameters.length < 1) {
  13. // `input` parameter.
  14. parameters.push(undefined);
  15. } else {
  16. // Convert string `input` to a `PhoneNumber` instance.
  17. if (typeof parameters[0] === 'string') {
  18. v2 = true;
  19. parameters[0] = parsePhoneNumber(parameters[0], _objectSpread(_objectSpread({}, parameters[1]), {}, {
  20. extract: false
  21. }), metadata);
  22. }
  23. }
  24. if (parameters.length < 2) {
  25. // `options` parameter.
  26. parameters.push(undefined);
  27. } // Set `v2` flag.
  28. parameters[1] = _objectSpread({
  29. v2: v2
  30. }, parameters[1]); // Add `metadata` parameter.
  31. parameters.push(metadata); // Call the function.
  32. return _isPossibleNumber.apply(this, parameters);
  33. }
  34. describe('isPossible', function () {
  35. it('should work', function () {
  36. isPossibleNumber('+79992223344').should.equal(true);
  37. isPossibleNumber({
  38. phone: '1112223344',
  39. country: 'RU'
  40. }).should.equal(true);
  41. isPossibleNumber({
  42. phone: '111222334',
  43. country: 'RU'
  44. }).should.equal(false);
  45. isPossibleNumber({
  46. phone: '11122233445',
  47. country: 'RU'
  48. }).should.equal(false);
  49. isPossibleNumber({
  50. phone: '1112223344',
  51. countryCallingCode: 7
  52. }).should.equal(true);
  53. });
  54. it('should work v2', function () {
  55. isPossibleNumber({
  56. nationalNumber: '111222334',
  57. countryCallingCode: 7
  58. }, {
  59. v2: true
  60. }).should.equal(false);
  61. isPossibleNumber({
  62. nationalNumber: '1112223344',
  63. countryCallingCode: 7
  64. }, {
  65. v2: true
  66. }).should.equal(true);
  67. isPossibleNumber({
  68. nationalNumber: '11122233445',
  69. countryCallingCode: 7
  70. }, {
  71. v2: true
  72. }).should.equal(false);
  73. });
  74. it('should work in edge cases', function () {
  75. // Invalid `PhoneNumber` argument.
  76. expect(function () {
  77. return isPossibleNumber({}, {
  78. v2: true
  79. });
  80. }).to["throw"]('Invalid phone number object passed'); // Empty input is passed.
  81. // This is just to support `isValidNumber({})`
  82. // for cases when `parseNumber()` returns `{}`.
  83. isPossibleNumber({}).should.equal(false);
  84. expect(function () {
  85. return isPossibleNumber({
  86. phone: '1112223344'
  87. });
  88. }).to["throw"]('Invalid phone number object passed'); // Incorrect country.
  89. expect(function () {
  90. return isPossibleNumber({
  91. phone: '1112223344',
  92. country: 'XX'
  93. });
  94. }).to["throw"]('Unknown country');
  95. });
  96. });
  97. //# sourceMappingURL=isPossible.test.js.map