parsePhoneNumber.test.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import _parsePhoneNumber from './parsePhoneNumber.js';
  2. import metadata from '../metadata.min.json' assert { type: 'json' };
  3. function parsePhoneNumber() {
  4. for (var _len = arguments.length, parameters = new Array(_len), _key = 0; _key < _len; _key++) {
  5. parameters[_key] = arguments[_key];
  6. }
  7. parameters.push(metadata);
  8. return _parsePhoneNumber.apply(this, parameters);
  9. }
  10. var USE_NON_GEOGRAPHIC_COUNTRY_CODE = false;
  11. describe('parsePhoneNumber', function () {
  12. it('should parse phone numbers from string', function () {
  13. parsePhoneNumber('Phone: 8 (800) 555 35 35.', 'RU').nationalNumber.should.equal('8005553535');
  14. expect(parsePhoneNumber('3', 'RU')).to.be.undefined;
  15. });
  16. it('should work in edge cases', function () {
  17. expect(parsePhoneNumber('')).to.be.undefined;
  18. });
  19. it('should parse phone numbers when invalid country code is passed', function () {
  20. parsePhoneNumber('Phone: +7 (800) 555 35 35.', 'XX').nationalNumber.should.equal('8005553535');
  21. expect(parsePhoneNumber('Phone: 8 (800) 555-35-35.', 'XX')).to.be.undefined;
  22. });
  23. it('should parse non-geographic numbering plan phone numbers (extended)', function () {
  24. var phoneNumber = parsePhoneNumber('+870773111632');
  25. phoneNumber.number.should.equal('+870773111632');
  26. if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
  27. phoneNumber.country.should.equal('001');
  28. } else {
  29. expect(phoneNumber.country).to.be.undefined;
  30. }
  31. phoneNumber.countryCallingCode.should.equal('870');
  32. });
  33. it('should parse non-geographic numbering plan phone numbers (default country code) (extended)', function () {
  34. var phoneNumber = parsePhoneNumber('773111632', {
  35. defaultCallingCode: '870'
  36. });
  37. phoneNumber.number.should.equal('+870773111632');
  38. if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
  39. phoneNumber.country.should.equal('001');
  40. } else {
  41. expect(phoneNumber.country).to.be.undefined;
  42. }
  43. phoneNumber.countryCallingCode.should.equal('870');
  44. });
  45. it('should determine the possibility of non-geographic phone numbers', function () {
  46. var phoneNumber = parsePhoneNumber('+870773111632');
  47. phoneNumber.isPossible().should.equal(true);
  48. var phoneNumber2 = parsePhoneNumber('+8707731116321');
  49. phoneNumber2.isPossible().should.equal(false);
  50. });
  51. it('should support `extract: false` flag', function () {
  52. var testCorrectness = function testCorrectness(number, expectedResult) {
  53. var result = expect(parsePhoneNumber(number, {
  54. extract: false,
  55. defaultCountry: 'US'
  56. }));
  57. if (expectedResult) {
  58. result.to.not.be.undefined;
  59. } else {
  60. result.to.be.undefined;
  61. }
  62. };
  63. testCorrectness('Call: (213) 373-4253', false);
  64. testCorrectness('(213) 373-4253x', false);
  65. testCorrectness('(213) 373-4253', true);
  66. testCorrectness('- (213) 373-4253 -', true);
  67. testCorrectness('+1 (213) 373-4253', true);
  68. testCorrectness(' +1 (213) 373-4253', false);
  69. });
  70. it('should not prematurely strip a possible national prefix from Chinese numbers', function () {
  71. // https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/57
  72. var phoneNumber = parsePhoneNumber('+86123456789');
  73. phoneNumber.isPossible().should.equal(true);
  74. phoneNumber.isValid().should.equal(false);
  75. phoneNumber.nationalNumber.should.equal('123456789');
  76. });
  77. });
  78. //# sourceMappingURL=parsePhoneNumber.test.js.map