findPhoneNumbersInText.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import findPhoneNumbersInText from './findPhoneNumbersInText.js';
  2. import metadata from '../metadata.min.json' assert { type: 'json' };
  3. import metadataMax from '../metadata.max.json' assert { type: 'json' };
  4. function findPhoneNumbersInTextWithResults(input, options, metadata) {
  5. var results = findPhoneNumbersInText(input, options, metadata);
  6. return results.map(function (result) {
  7. var startsAt = result.startsAt,
  8. endsAt = result.endsAt,
  9. number = result.number;
  10. var data = {
  11. phone: number.nationalNumber,
  12. startsAt: startsAt,
  13. endsAt: endsAt
  14. };
  15. if (number.country) {
  16. data.country = number.country;
  17. }
  18. if (number.ext) {
  19. data.ext = number.ext;
  20. }
  21. return data;
  22. });
  23. }
  24. describe('findPhoneNumbersInText', function () {
  25. it('should find phone numbers in text (with default country)', function () {
  26. findPhoneNumbersInText('+7 (800) 555-35-35', 'US', metadata)[0].number.number.should.equal('+78005553535');
  27. });
  28. it('should find phone numbers in text (with default country in options)', function () {
  29. findPhoneNumbersInText('+7 (800) 555-35-35', {
  30. defaultCountry: 'US'
  31. }, metadata)[0].number.number.should.equal('+78005553535');
  32. });
  33. it('should find phone numbers in text (with default country and options)', function () {
  34. findPhoneNumbersInText('+7 (800) 555-35-35', 'US', {}, metadata)[0].number.number.should.equal('+78005553535');
  35. });
  36. it('should find phone numbers in text (without default country, with options)', function () {
  37. findPhoneNumbersInText('+7 (800) 555-35-35', undefined, {}, metadata)[0].number.number.should.equal('+78005553535');
  38. });
  39. it('should find phone numbers in text (with default country, without options)', function () {
  40. findPhoneNumbersInText('+7 (800) 555-35-35', 'US', undefined, metadata)[0].number.number.should.equal('+78005553535');
  41. });
  42. it('should find phone numbers in text (with empty default country)', function () {
  43. findPhoneNumbersInText('+7 (800) 555-35-35', undefined, metadata)[0].number.number.should.equal('+78005553535');
  44. });
  45. it('should find phone numbers in text', function () {
  46. var NUMBERS = ['+78005553535', '+12133734253'];
  47. var results = findPhoneNumbersInText('The number is +7 (800) 555-35-35 and not (213) 373-4253 as written in the document.', metadata);
  48. var i = 0;
  49. while (i < results.length) {
  50. results[i].number.number.should.equal(NUMBERS[i]);
  51. i++;
  52. }
  53. });
  54. it('should find phone numbers in text (default country calling code)', function () {
  55. var NUMBERS = ['+870773111632'];
  56. var results = findPhoneNumbersInText('The number is 773 111 632', {
  57. defaultCallingCode: '870'
  58. }, metadata);
  59. var i = 0;
  60. while (i < results.length) {
  61. results[i].number.number.should.equal(NUMBERS[i]);
  62. i++;
  63. }
  64. });
  65. it('should find numbers', function () {
  66. findPhoneNumbersInTextWithResults('2133734253', {
  67. defaultCountry: 'US'
  68. }, metadata).should.deep.equal([{
  69. phone: '2133734253',
  70. country: 'US',
  71. startsAt: 0,
  72. endsAt: 10
  73. }]);
  74. findPhoneNumbersInTextWithResults('(213) 373-4253', {
  75. defaultCountry: 'US'
  76. }, metadata).should.deep.equal([{
  77. phone: '2133734253',
  78. country: 'US',
  79. startsAt: 0,
  80. endsAt: 14
  81. }]);
  82. findPhoneNumbersInTextWithResults('The number is +7 (800) 555-35-35 and not (213) 373-4253 as written in the document.', {
  83. defaultCountry: 'US'
  84. }, metadata).should.deep.equal([{
  85. phone: '8005553535',
  86. country: 'RU',
  87. startsAt: 14,
  88. endsAt: 32
  89. }, {
  90. phone: '2133734253',
  91. country: 'US',
  92. startsAt: 41,
  93. endsAt: 55
  94. }]); // Opening parenthesis issue.
  95. // https://github.com/catamphetamine/libphonenumber-js/issues/252
  96. findPhoneNumbersInTextWithResults('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.', {
  97. defaultCountry: 'US'
  98. }, metadata).should.deep.equal([{
  99. phone: '8005553535',
  100. country: 'RU',
  101. startsAt: 14,
  102. endsAt: 32
  103. }, {
  104. phone: '2133734253',
  105. country: 'US',
  106. startsAt: 41,
  107. endsAt: 55
  108. }]); // No default country.
  109. findPhoneNumbersInTextWithResults('The number is +7 (800) 555-35-35 as written in the document.', undefined, metadata).should.deep.equal([{
  110. phone: '8005553535',
  111. country: 'RU',
  112. startsAt: 14,
  113. endsAt: 32
  114. }]); // Passing `options` and default country.
  115. findPhoneNumbersInTextWithResults('The number is +7 (800) 555-35-35 as written in the document.', {
  116. defaultCountry: 'US',
  117. leniency: 'VALID'
  118. }, metadata).should.deep.equal([{
  119. phone: '8005553535',
  120. country: 'RU',
  121. startsAt: 14,
  122. endsAt: 32
  123. }]); // Passing `options`.
  124. findPhoneNumbersInTextWithResults('The number is +7 (800) 555-35-35 as written in the document.', {
  125. leniency: 'VALID'
  126. }, metadata).should.deep.equal([{
  127. phone: '8005553535',
  128. country: 'RU',
  129. startsAt: 14,
  130. endsAt: 32
  131. }]); // Not a phone number and a phone number.
  132. findPhoneNumbersInTextWithResults('Digits 12 are not a number, but +7 (800) 555-35-35 is.', {
  133. leniency: 'VALID'
  134. }, metadata).should.deep.equal([{
  135. phone: '8005553535',
  136. country: 'RU',
  137. startsAt: 32,
  138. endsAt: 50
  139. }]); // Phone number extension.
  140. findPhoneNumbersInTextWithResults('Date 02/17/2018 is not a number, but +7 (800) 555-35-35 ext. 123 is.', {
  141. leniency: 'VALID'
  142. }, metadata).should.deep.equal([{
  143. phone: '8005553535',
  144. country: 'RU',
  145. ext: '123',
  146. startsAt: 37,
  147. endsAt: 64
  148. }]);
  149. });
  150. it('should find numbers (v2)', function () {
  151. var phoneNumbers = findPhoneNumbersInText('The number is +7 (800) 555-35-35 ext. 1234 and not (213) 373-4253 as written in the document.', {
  152. defaultCountry: 'US',
  153. v2: true
  154. }, metadata);
  155. phoneNumbers.length.should.equal(2);
  156. phoneNumbers[0].startsAt.should.equal(14);
  157. phoneNumbers[0].endsAt.should.equal(42);
  158. phoneNumbers[0].number.number.should.equal('+78005553535');
  159. phoneNumbers[0].number.nationalNumber.should.equal('8005553535');
  160. phoneNumbers[0].number.country.should.equal('RU');
  161. phoneNumbers[0].number.countryCallingCode.should.equal('7');
  162. phoneNumbers[0].number.ext.should.equal('1234');
  163. phoneNumbers[1].startsAt.should.equal(51);
  164. phoneNumbers[1].endsAt.should.equal(65);
  165. phoneNumbers[1].number.number.should.equal('+12133734253');
  166. phoneNumbers[1].number.nationalNumber.should.equal('2133734253');
  167. phoneNumbers[1].number.country.should.equal('US');
  168. phoneNumbers[1].number.countryCallingCode.should.equal('1');
  169. });
  170. it('shouldn\'t find non-valid numbers', function () {
  171. // Not a valid phone number for US.
  172. findPhoneNumbersInTextWithResults('1111111111', {
  173. defaultCountry: 'US'
  174. }, metadata).should.deep.equal([]);
  175. });
  176. it('should find non-European digits', function () {
  177. // E.g. in Iraq they don't write `+442323234` but rather `+٤٤٢٣٢٣٢٣٤`.
  178. findPhoneNumbersInTextWithResults('العَرَبِيَّة‎ +٤٤٣٣٣٣٣٣٣٣٣٣عَرَبِيّ‎', undefined, metadata).should.deep.equal([{
  179. country: 'GB',
  180. phone: '3333333333',
  181. startsAt: 14,
  182. endsAt: 27
  183. }]);
  184. });
  185. it('should work in edge cases', function () {
  186. var thrower; // No input
  187. findPhoneNumbersInTextWithResults('', undefined, metadata).should.deep.equal([]); // // No country metadata for this `require` country code
  188. // thrower = () => findPhoneNumbersInTextWithResults('123', { defaultCountry: 'ZZ' }, metadata)
  189. // thrower.should.throw('Unknown country')
  190. // Numerical `value`
  191. thrower = function thrower() {
  192. return findPhoneNumbersInTextWithResults(2141111111, {
  193. defaultCountry: 'US'
  194. });
  195. };
  196. thrower.should["throw"]('A text for parsing must be a string.'); // // No metadata
  197. // thrower = () => findPhoneNumbersInTextWithResults('')
  198. // thrower.should.throw('`metadata` argument not passed')
  199. // No metadata, no default country, no phone numbers.
  200. findPhoneNumbersInTextWithResults('').should.deep.equal([]);
  201. });
  202. it('should find international numbers when passed a non-existent default country', function () {
  203. var numbers = findPhoneNumbersInText('Phone: +7 (800) 555 35 35. National: 8 (800) 555-55-55', {
  204. defaultCountry: 'XX',
  205. v2: true
  206. }, metadata);
  207. numbers.length.should.equal(1);
  208. numbers[0].number.nationalNumber.should.equal('8005553535');
  209. });
  210. it('shouldn\'t find phone numbers which are not phone numbers', function () {
  211. // A timestamp.
  212. findPhoneNumbersInTextWithResults('2012-01-02 08:00', {
  213. defaultCountry: 'US'
  214. }, metadata).should.deep.equal([]); // A valid number (not a complete timestamp).
  215. findPhoneNumbersInTextWithResults('2012-01-02 08', {
  216. defaultCountry: 'US'
  217. }, metadata).should.deep.equal([{
  218. country: 'US',
  219. phone: '2012010208',
  220. startsAt: 0,
  221. endsAt: 13
  222. }]); // Invalid parens.
  223. findPhoneNumbersInTextWithResults('213(3734253', {
  224. defaultCountry: 'US'
  225. }, metadata).should.deep.equal([]); // Letters after phone number.
  226. findPhoneNumbersInTextWithResults('2133734253a', {
  227. defaultCountry: 'US'
  228. }, metadata).should.deep.equal([]); // Valid phone (same as the one found in the UUID below).
  229. findPhoneNumbersInTextWithResults('The phone number is 231354125.', {
  230. defaultCountry: 'FR'
  231. }, metadata).should.deep.equal([{
  232. country: 'FR',
  233. phone: '231354125',
  234. startsAt: 20,
  235. endsAt: 29
  236. }]); // Not a phone number (part of a UUID).
  237. // Should parse in `{ extended: true }` mode.
  238. var possibleNumbers = findPhoneNumbersInTextWithResults('The UUID is CA801c26f98cd16e231354125ad046e40b.', {
  239. defaultCountry: 'FR',
  240. extended: true
  241. }, metadata);
  242. possibleNumbers.length.should.equal(1);
  243. possibleNumbers[0].country.should.equal('FR');
  244. possibleNumbers[0].phone.should.equal('231354125'); // Not a phone number (part of a UUID).
  245. // Shouldn't parse by default.
  246. findPhoneNumbersInTextWithResults('The UUID is CA801c26f98cd16e231354125ad046e40b.', {
  247. defaultCountry: 'FR'
  248. }, metadata).should.deep.equal([]);
  249. }); // https://gitlab.com/catamphetamine/libphonenumber-js/-/merge_requests/4
  250. it('should return correct `startsAt` and `endsAt` when matching "inner" candidates in a could-be-a-candidate substring', function () {
  251. findPhoneNumbersInTextWithResults('39945926 77200596 16533084', {
  252. defaultCountry: 'ID'
  253. }, metadataMax).should.deep.equal([{
  254. country: 'ID',
  255. phone: '77200596',
  256. startsAt: 9,
  257. endsAt: 17
  258. }]);
  259. });
  260. });
  261. //# sourceMappingURL=findPhoneNumbersInText.test.js.map