PhoneNumberMatcher.test.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import PhoneNumberMatcher from './PhoneNumberMatcher.js';
  2. import metadata from '../metadata.min.json' assert { type: 'json' };
  3. function test(text, defaultCountry, expectedNumbers) {
  4. if (typeof expectedNumbers === 'string') {
  5. expectedNumbers = [{
  6. nationalNumber: expectedNumbers
  7. }];
  8. }
  9. var matcher = new PhoneNumberMatcher(text, {
  10. defaultCountry: defaultCountry,
  11. v2: true
  12. }, metadata);
  13. while (matcher.hasNext()) {
  14. var number = matcher.next();
  15. var phoneNumber = expectedNumbers.shift();
  16. if (phoneNumber.startsAt !== undefined) {
  17. number.startsAt.should.equal(phoneNumber.startsAt);
  18. }
  19. if (phoneNumber.endsAt !== undefined) {
  20. number.endsAt.should.equal(phoneNumber.endsAt);
  21. }
  22. number.number.country.should.equal(phoneNumber.country || defaultCountry);
  23. number.number.nationalNumber.should.equal(phoneNumber.nationalNumber);
  24. }
  25. expectedNumbers.length.should.equal(0);
  26. }
  27. describe('PhoneNumberMatcher', function () {
  28. it('should find phone numbers', function () {
  29. test('The number is +7 (800) 555-35-35 and not (213) 373-4253 as written in the document.', 'US', [{
  30. country: 'RU',
  31. nationalNumber: '8005553535',
  32. startsAt: 14,
  33. endsAt: 32
  34. }, {
  35. country: 'US',
  36. nationalNumber: '2133734253',
  37. startsAt: 41,
  38. endsAt: 55
  39. }]);
  40. });
  41. it('should find phone numbers from Mexico', function () {
  42. // Test parsing fixed-line numbers of Mexico.
  43. test('+52 (449)978-0001', 'MX', '4499780001');
  44. test('01 (449)978-0001', 'MX', '4499780001');
  45. test('(449)978-0001', 'MX', '4499780001'); // Test parsing mobile numbers of Mexico.
  46. test('+52 1 33 1234-5678', 'MX', '3312345678');
  47. test('044 (33) 1234-5678', 'MX', '3312345678');
  48. test('045 33 1234-5678', 'MX', '3312345678');
  49. });
  50. it('should find phone numbers from Argentina', function () {
  51. // Test parsing mobile numbers of Argentina.
  52. test('+54 9 343 555 1212', 'AR', '93435551212');
  53. test('0343 15-555-1212', 'AR', '93435551212');
  54. test('+54 9 3715 65 4320', 'AR', '93715654320');
  55. test('03715 15 65 4320', 'AR', '93715654320'); // Test parsing fixed-line numbers of Argentina.
  56. test('+54 11 3797 0000', 'AR', '1137970000');
  57. test('011 3797 0000', 'AR', '1137970000');
  58. test('+54 3715 65 4321', 'AR', '3715654321');
  59. test('03715 65 4321', 'AR', '3715654321');
  60. test('+54 23 1234 0000', 'AR', '2312340000');
  61. test('023 1234 0000', 'AR', '2312340000');
  62. });
  63. it('should only support the supported leniency values', function () {
  64. expect(function () {
  65. return new PhoneNumberMatcher('+54 23 1234 0000', {
  66. leniency: 'STRICT_GROUPING',
  67. v2: true
  68. }, metadata);
  69. }).to["throw"]('Supported values: "POSSIBLE", "VALID".');
  70. });
  71. });
  72. //# sourceMappingURL=PhoneNumberMatcher.test.js.map