findPhoneNumbers.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. "use strict";
  2. function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
  3. var _findPhoneNumbers = _interopRequireWildcard(require("./findPhoneNumbers.js"));
  4. var _findPhoneNumbersInitialImplementation = require("./findPhoneNumbersInitialImplementation.js");
  5. var _metadataMin = _interopRequireDefault(require("../../metadata.min.json"));
  6. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  7. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  8. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  9. function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (it) return (it = it.call(o)).next.bind(it); if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  10. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  11. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  12. describe('findPhoneNumbers', function () {
  13. it('should find numbers', function () {
  14. (0, _findPhoneNumbers["default"])('2133734253', 'US', _metadataMin["default"]).should.deep.equal([{
  15. phone: '2133734253',
  16. country: 'US',
  17. startsAt: 0,
  18. endsAt: 10
  19. }]);
  20. (0, _findPhoneNumbers["default"])('(213) 373-4253', 'US', _metadataMin["default"]).should.deep.equal([{
  21. phone: '2133734253',
  22. country: 'US',
  23. startsAt: 0,
  24. endsAt: 14
  25. }]);
  26. (0, _findPhoneNumbers["default"])('The number is +7 (800) 555-35-35 and not (213) 373-4253 as written in the document.', 'US', _metadataMin["default"]).should.deep.equal([{
  27. phone: '8005553535',
  28. country: 'RU',
  29. startsAt: 14,
  30. endsAt: 32
  31. }, {
  32. phone: '2133734253',
  33. country: 'US',
  34. startsAt: 41,
  35. endsAt: 55
  36. }]); // Opening parenthesis issue.
  37. // https://github.com/catamphetamine/libphonenumber-js/issues/252
  38. (0, _findPhoneNumbers["default"])('The number is +7 (800) 555-35-35 and not (213) 373-4253 (that\'s not even in the same country!) as written in the document.', 'US', _metadataMin["default"]).should.deep.equal([{
  39. phone: '8005553535',
  40. country: 'RU',
  41. startsAt: 14,
  42. endsAt: 32
  43. }, {
  44. phone: '2133734253',
  45. country: 'US',
  46. startsAt: 41,
  47. endsAt: 55
  48. }]); // No default country.
  49. (0, _findPhoneNumbers["default"])('The number is +7 (800) 555-35-35 as written in the document.', _metadataMin["default"]).should.deep.equal([{
  50. phone: '8005553535',
  51. country: 'RU',
  52. startsAt: 14,
  53. endsAt: 32
  54. }]); // Passing `options` and default country.
  55. (0, _findPhoneNumbers["default"])('The number is +7 (800) 555-35-35 as written in the document.', 'US', {
  56. leniency: 'VALID'
  57. }, _metadataMin["default"]).should.deep.equal([{
  58. phone: '8005553535',
  59. country: 'RU',
  60. startsAt: 14,
  61. endsAt: 32
  62. }]); // Passing `options`.
  63. (0, _findPhoneNumbers["default"])('The number is +7 (800) 555-35-35 as written in the document.', {
  64. leniency: 'VALID'
  65. }, _metadataMin["default"]).should.deep.equal([{
  66. phone: '8005553535',
  67. country: 'RU',
  68. startsAt: 14,
  69. endsAt: 32
  70. }]); // Not a phone number and a phone number.
  71. (0, _findPhoneNumbers["default"])('Digits 12 are not a number, but +7 (800) 555-35-35 is.', {
  72. leniency: 'VALID'
  73. }, _metadataMin["default"]).should.deep.equal([{
  74. phone: '8005553535',
  75. country: 'RU',
  76. startsAt: 32,
  77. endsAt: 50
  78. }]); // Phone number extension.
  79. (0, _findPhoneNumbers["default"])('Date 02/17/2018 is not a number, but +7 (800) 555-35-35 ext. 123 is.', {
  80. leniency: 'VALID'
  81. }, _metadataMin["default"]).should.deep.equal([{
  82. phone: '8005553535',
  83. country: 'RU',
  84. ext: '123',
  85. startsAt: 37,
  86. endsAt: 64
  87. }]);
  88. });
  89. it('shouldn\'t find non-valid numbers', function () {
  90. // Not a valid phone number for US.
  91. (0, _findPhoneNumbers["default"])('1111111111', 'US', _metadataMin["default"]).should.deep.equal([]);
  92. });
  93. it('should find non-European digits', function () {
  94. // E.g. in Iraq they don't write `+442323234` but rather `+٤٤٢٣٢٣٢٣٤`.
  95. (0, _findPhoneNumbers["default"])('العَرَبِيَّة‎ +٤٤٣٣٣٣٣٣٣٣٣٣عَرَبِيّ‎', _metadataMin["default"]).should.deep.equal([{
  96. country: 'GB',
  97. phone: '3333333333',
  98. startsAt: 14,
  99. endsAt: 27
  100. }]);
  101. });
  102. it('should iterate', function () {
  103. var expected_numbers = [{
  104. country: 'RU',
  105. phone: '8005553535',
  106. // number : '+7 (800) 555-35-35',
  107. startsAt: 14,
  108. endsAt: 32
  109. }, {
  110. country: 'US',
  111. phone: '2133734253',
  112. // number : '(213) 373-4253',
  113. startsAt: 41,
  114. endsAt: 55
  115. }];
  116. for (var _iterator = _createForOfIteratorHelperLoose((0, _findPhoneNumbers.searchPhoneNumbers)('The number is +7 (800) 555-35-35 and not (213) 373-4253 as written in the document.', 'US', _metadataMin["default"])), _step; !(_step = _iterator()).done;) {
  117. var number = _step.value;
  118. number.should.deep.equal(expected_numbers.shift());
  119. }
  120. expected_numbers.length.should.equal(0);
  121. });
  122. it('should work in edge cases', function () {
  123. var thrower; // No input
  124. (0, _findPhoneNumbers["default"])('', _metadataMin["default"]).should.deep.equal([]); // No country metadata for this `require` country code
  125. thrower = function thrower() {
  126. return (0, _findPhoneNumbers["default"])('123', 'ZZ', _metadataMin["default"]);
  127. };
  128. thrower.should["throw"]('Unknown country'); // Numerical `value`
  129. thrower = function thrower() {
  130. return (0, _findPhoneNumbers["default"])(2141111111, 'US');
  131. };
  132. thrower.should["throw"]('A text for parsing must be a string.'); // // No metadata
  133. // thrower = () => findNumbers('')
  134. // thrower.should.throw('`metadata` argument not passed')
  135. });
  136. it('shouldn\'t find phone numbers which are not phone numbers', function () {
  137. // A timestamp.
  138. (0, _findPhoneNumbers["default"])('2012-01-02 08:00', 'US', _metadataMin["default"]).should.deep.equal([]); // A valid number (not a complete timestamp).
  139. (0, _findPhoneNumbers["default"])('2012-01-02 08', 'US', _metadataMin["default"]).should.deep.equal([{
  140. country: 'US',
  141. phone: '2012010208',
  142. startsAt: 0,
  143. endsAt: 13
  144. }]); // Invalid parens.
  145. (0, _findPhoneNumbers["default"])('213(3734253', 'US', _metadataMin["default"]).should.deep.equal([]); // Letters after phone number.
  146. (0, _findPhoneNumbers["default"])('2133734253a', 'US', _metadataMin["default"]).should.deep.equal([]); // Valid phone (same as the one found in the UUID below).
  147. (0, _findPhoneNumbers["default"])('The phone number is 231354125.', 'FR', _metadataMin["default"]).should.deep.equal([{
  148. country: 'FR',
  149. phone: '231354125',
  150. startsAt: 20,
  151. endsAt: 29
  152. }]); // Not a phone number (part of a UUID).
  153. // Should parse in `{ extended: true }` mode.
  154. var possibleNumbers = (0, _findPhoneNumbers["default"])('The UUID is CA801c26f98cd16e231354125ad046e40b.', 'FR', {
  155. extended: true
  156. }, _metadataMin["default"]);
  157. possibleNumbers.length.should.equal(3);
  158. possibleNumbers[1].country.should.equal('FR');
  159. possibleNumbers[1].phone.should.equal('231354125'); // Not a phone number (part of a UUID).
  160. // Shouldn't parse by default.
  161. (0, _findPhoneNumbers["default"])('The UUID is CA801c26f98cd16e231354125ad046e40b.', 'FR', _metadataMin["default"]).should.deep.equal([]);
  162. });
  163. });
  164. describe('PhoneNumberSearch', function () {
  165. it('should search for phone numbers', function () {
  166. var finder = new _findPhoneNumbersInitialImplementation.PhoneNumberSearch('The number is +7 (800) 555-35-35 and not (213) 373-4253 as written in the document.', {
  167. defaultCountry: 'US'
  168. }, _metadataMin["default"]);
  169. finder.hasNext().should.equal(true);
  170. finder.next().should.deep.equal({
  171. country: 'RU',
  172. phone: '8005553535',
  173. // number : '+7 (800) 555-35-35',
  174. startsAt: 14,
  175. endsAt: 32
  176. });
  177. finder.hasNext().should.equal(true);
  178. finder.next().should.deep.equal({
  179. country: 'US',
  180. phone: '2133734253',
  181. // number : '(213) 373-4253',
  182. startsAt: 41,
  183. endsAt: 55
  184. });
  185. finder.hasNext().should.equal(false);
  186. });
  187. it('should search for phone numbers (no options)', function () {
  188. var finder = new _findPhoneNumbersInitialImplementation.PhoneNumberSearch('The number is +7 (800) 555-35-35', undefined, _metadataMin["default"]);
  189. finder.hasNext().should.equal(true);
  190. finder.next().should.deep.equal({
  191. country: 'RU',
  192. phone: '8005553535',
  193. // number : '+7 (800) 555-35-35',
  194. startsAt: 14,
  195. endsAt: 32
  196. });
  197. finder.hasNext().should.equal(false);
  198. });
  199. it('should work in edge cases', function () {
  200. // No options
  201. var search = new _findPhoneNumbersInitialImplementation.PhoneNumberSearch('', undefined, _metadataMin["default"]); // No next element
  202. var thrower = function thrower() {
  203. return search.next();
  204. };
  205. thrower.should["throw"]('No next element');
  206. });
  207. });
  208. //# sourceMappingURL=findPhoneNumbers.test.js.map