PhoneNumberMatcher.test.js 3.0 KB

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