isPossible.test.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. "use strict";
  2. var _metadataMin = _interopRequireDefault(require("../metadata.min.json"));
  3. var _isPossible = _interopRequireDefault(require("./isPossible.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 isPossibleNumber() {
  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 _isPossible["default"].apply(this, parameters);
  35. }
  36. describe('isPossible', function () {
  37. it('should work', function () {
  38. isPossibleNumber('+79992223344').should.equal(true);
  39. isPossibleNumber({
  40. phone: '1112223344',
  41. country: 'RU'
  42. }).should.equal(true);
  43. isPossibleNumber({
  44. phone: '111222334',
  45. country: 'RU'
  46. }).should.equal(false);
  47. isPossibleNumber({
  48. phone: '11122233445',
  49. country: 'RU'
  50. }).should.equal(false);
  51. isPossibleNumber({
  52. phone: '1112223344',
  53. countryCallingCode: 7
  54. }).should.equal(true);
  55. });
  56. it('should work v2', function () {
  57. isPossibleNumber({
  58. nationalNumber: '111222334',
  59. countryCallingCode: 7
  60. }, {
  61. v2: true
  62. }).should.equal(false);
  63. isPossibleNumber({
  64. nationalNumber: '1112223344',
  65. countryCallingCode: 7
  66. }, {
  67. v2: true
  68. }).should.equal(true);
  69. isPossibleNumber({
  70. nationalNumber: '11122233445',
  71. countryCallingCode: 7
  72. }, {
  73. v2: true
  74. }).should.equal(false);
  75. });
  76. it('should work in edge cases', function () {
  77. // Invalid `PhoneNumber` argument.
  78. expect(function () {
  79. return isPossibleNumber({}, {
  80. v2: true
  81. });
  82. }).to["throw"]('Invalid phone number object passed'); // Empty input is passed.
  83. // This is just to support `isValidNumber({})`
  84. // for cases when `parseNumber()` returns `{}`.
  85. isPossibleNumber({}).should.equal(false);
  86. expect(function () {
  87. return isPossibleNumber({
  88. phone: '1112223344'
  89. });
  90. }).to["throw"]('Invalid phone number object passed'); // Incorrect country.
  91. expect(function () {
  92. return isPossibleNumber({
  93. phone: '1112223344',
  94. country: 'XX'
  95. });
  96. }).to["throw"]('Unknown country');
  97. });
  98. });
  99. //# sourceMappingURL=isPossible.test.js.map