findPhoneNumbersInText.test.js 10 KB

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