findNumbers.test.js 6.9 KB

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