findPhoneNumbers.test.js 8.6 KB

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