findPhoneNumbersInText.test.js 11 KB

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