parsePhoneNumberWithError.test.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import _parsePhoneNumber from './parsePhoneNumberWithError.js';
  2. import metadata from '../metadata.min.json' assert { type: 'json' };
  3. import metadataFull from '../metadata.max.json' assert { type: 'json' };
  4. function parsePhoneNumber() {
  5. for (var _len = arguments.length, parameters = new Array(_len), _key = 0; _key < _len; _key++) {
  6. parameters[_key] = arguments[_key];
  7. }
  8. parameters.push(metadata);
  9. return _parsePhoneNumber.apply(this, parameters);
  10. }
  11. function parsePhoneNumberFull() {
  12. for (var _len2 = arguments.length, parameters = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  13. parameters[_key2] = arguments[_key2];
  14. }
  15. parameters.push(metadataFull);
  16. return _parsePhoneNumber.apply(this, parameters);
  17. }
  18. describe('parsePhoneNumberWithError', function () {
  19. it('should parse phone numbers', function () {
  20. var phoneNumber = parsePhoneNumber('The phone number is: 8 (800) 555 35 35. Some other text.', 'RU');
  21. phoneNumber.country.should.equal('RU');
  22. phoneNumber.countryCallingCode.should.equal('7');
  23. phoneNumber.nationalNumber.should.equal('8005553535');
  24. phoneNumber.number.should.equal('+78005553535');
  25. phoneNumber.isPossible().should.equal(true);
  26. phoneNumber.isValid().should.equal(true); // phoneNumber.isValidForRegion('RU').should.equal(true)
  27. // Russian phone type regexps aren't included in default metadata.
  28. parsePhoneNumberFull('Phone: 8 (800) 555 35 35.', 'RU').getType().should.equal('TOLL_FREE');
  29. });
  30. it('shouldn\'t set country when it\'s non-derivable', function () {
  31. var phoneNumber = parsePhoneNumber('+7 111 555 35 35');
  32. expect(phoneNumber.country).to.be.undefined;
  33. phoneNumber.countryCallingCode.should.equal('7');
  34. phoneNumber.nationalNumber.should.equal('1115553535');
  35. });
  36. it('should parse carrier code', function () {
  37. var phoneNumber = parsePhoneNumber('0 15 21 5555-5555', 'BR');
  38. phoneNumber.carrierCode.should.equal('15');
  39. });
  40. it('should parse phone extension', function () {
  41. var phoneNumber = parsePhoneNumber('Phone: 8 (800) 555 35 35 ext. 1234.', 'RU');
  42. phoneNumber.ext.should.equal('1234');
  43. });
  44. it('should validate numbers for countries with no type regular expressions', function () {
  45. parsePhoneNumber('+380391234567').isValid().should.equal(true);
  46. parsePhoneNumber('+380191234567').isValid().should.equal(false);
  47. });
  48. it('should format numbers', function () {
  49. var phoneNumber = parsePhoneNumber('Phone: 8 (800) 555 35 35.', 'RU');
  50. phoneNumber.format('NATIONAL').should.equal('8 (800) 555-35-35');
  51. phoneNumber.formatNational().should.equal('8 (800) 555-35-35');
  52. phoneNumber.format('INTERNATIONAL').should.equal('+7 800 555 35 35');
  53. phoneNumber.formatInternational().should.equal('+7 800 555 35 35');
  54. });
  55. it('should get tel: URI', function () {
  56. var phoneNumber = parsePhoneNumber('Phone: 8 (800) 555 35 35 ext. 1234.', 'RU');
  57. phoneNumber.getURI().should.equal('tel:+78005553535;ext=1234');
  58. });
  59. it('should work in edge cases', function () {
  60. expect(function () {
  61. return parsePhoneNumber('+78005553535', -1, {});
  62. }).to["throw"]('Invalid second argument');
  63. });
  64. it('should throw parse errors', function () {
  65. expect(function () {
  66. return parsePhoneNumber('8005553535', 'XX');
  67. }).to["throw"]('INVALID_COUNTRY');
  68. expect(function () {
  69. return parsePhoneNumber('+', 'RU');
  70. }).to["throw"]('NOT_A_NUMBER');
  71. expect(function () {
  72. return parsePhoneNumber('a', 'RU');
  73. }).to["throw"]('NOT_A_NUMBER');
  74. expect(function () {
  75. return parsePhoneNumber('1', 'RU');
  76. }).to["throw"]('TOO_SHORT');
  77. expect(function () {
  78. return parsePhoneNumber('+4');
  79. }).to["throw"]('TOO_SHORT');
  80. expect(function () {
  81. return parsePhoneNumber('+44');
  82. }).to["throw"]('TOO_SHORT');
  83. expect(function () {
  84. return parsePhoneNumber('+443');
  85. }).to["throw"]('TOO_SHORT');
  86. expect(function () {
  87. return parsePhoneNumber('+370');
  88. }).to["throw"]('TOO_SHORT');
  89. expect(function () {
  90. return parsePhoneNumber('88888888888888888888', 'RU');
  91. }).to["throw"]('TOO_LONG');
  92. expect(function () {
  93. return parsePhoneNumber('8 (800) 555 35 35');
  94. }).to["throw"]('INVALID_COUNTRY');
  95. expect(function () {
  96. return parsePhoneNumber('+9991112233');
  97. }).to["throw"]('INVALID_COUNTRY');
  98. expect(function () {
  99. return parsePhoneNumber('+9991112233', 'US');
  100. }).to["throw"]('INVALID_COUNTRY');
  101. expect(function () {
  102. return parsePhoneNumber('8005553535 ', 'RU');
  103. }).to["throw"]('TOO_LONG');
  104. });
  105. it('should parse incorrect international phone numbers', function () {
  106. // Parsing national prefixes and carrier codes
  107. // is only required for local phone numbers
  108. // but some people don't understand that
  109. // and sometimes write international phone numbers
  110. // with national prefixes (or maybe even carrier codes).
  111. // http://ucken.blogspot.ru/2016/03/trunk-prefixes-in-skype4b.html
  112. // Google's original library forgives such mistakes
  113. // and so does this library, because it has been requested:
  114. // https://github.com/catamphetamine/libphonenumber-js/issues/127
  115. var phoneNumber; // For complete numbers it should strip national prefix.
  116. phoneNumber = parsePhoneNumber('+1 1877 215 5230');
  117. phoneNumber.nationalNumber.should.equal('8772155230');
  118. phoneNumber.country.should.equal('US'); // For complete numbers it should strip national prefix.
  119. phoneNumber = parsePhoneNumber('+7 8800 555 3535');
  120. phoneNumber.nationalNumber.should.equal('8005553535');
  121. phoneNumber.country.should.equal('RU'); // For incomplete numbers it shouldn't strip national prefix.
  122. phoneNumber = parsePhoneNumber('+7 8800 555 353');
  123. phoneNumber.nationalNumber.should.equal('8800555353');
  124. phoneNumber.country.should.equal('RU');
  125. });
  126. });
  127. //# sourceMappingURL=parsePhoneNumberWithError.test.js.map