parsePhoneNumber.test.js 3.5 KB

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