parseIncompletePhoneNumber.test.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import parseIncompletePhoneNumber, { parsePhoneNumberCharacter } from './parseIncompletePhoneNumber.js';
  2. describe('parseIncompletePhoneNumber', function () {
  3. it('should parse phone number character', function () {
  4. // Accepts leading `+`.
  5. parsePhoneNumberCharacter('+').should.equal('+'); // Doesn't accept non-leading `+`.
  6. expect(parsePhoneNumberCharacter('+', '+')).to.be.undefined; // Parses digits.
  7. parsePhoneNumberCharacter('1').should.equal('1'); // Parses non-European digits.
  8. parsePhoneNumberCharacter('٤').should.equal('4'); // Dismisses other characters.
  9. expect(parsePhoneNumberCharacter('-')).to.be.undefined;
  10. });
  11. it('should parse incomplete phone number', function () {
  12. parseIncompletePhoneNumber('').should.equal(''); // Doesn't accept non-leading `+`.
  13. parseIncompletePhoneNumber('++').should.equal('+'); // Accepts leading `+`.
  14. parseIncompletePhoneNumber('+7 800 555').should.equal('+7800555'); // Parses digits.
  15. parseIncompletePhoneNumber('8 (800) 555').should.equal('8800555'); // Parses non-European digits.
  16. parseIncompletePhoneNumber('+٤٤٢٣٢٣٢٣٤').should.equal('+442323234');
  17. });
  18. it('should work with a new `context` argument in `parsePhoneNumberCharacter()` function (international number)', function () {
  19. var stopped = false;
  20. var emit = function emit(event) {
  21. switch (event) {
  22. case 'end':
  23. stopped = true;
  24. break;
  25. }
  26. };
  27. parsePhoneNumberCharacter('+', undefined, emit).should.equal('+');
  28. expect(stopped).to.equal(false);
  29. parsePhoneNumberCharacter('1', '+', emit).should.equal('1');
  30. expect(stopped).to.equal(false);
  31. expect(parsePhoneNumberCharacter('+', '+1', emit)).to.equal(undefined);
  32. expect(stopped).to.equal(true);
  33. expect(parsePhoneNumberCharacter('2', '+1', emit)).to.equal('2');
  34. expect(stopped).to.equal(true);
  35. });
  36. it('should work with a new `context` argument in `parsePhoneNumberCharacter()` function (national number)', function () {
  37. var stopped = false;
  38. var emit = function emit(event) {
  39. switch (event) {
  40. case 'end':
  41. stopped = true;
  42. break;
  43. }
  44. };
  45. parsePhoneNumberCharacter('2', undefined, emit).should.equal('2');
  46. expect(stopped).to.equal(false);
  47. expect(parsePhoneNumberCharacter('+', '2', emit)).to.equal(undefined);
  48. expect(stopped).to.equal(true);
  49. expect(parsePhoneNumberCharacter('1', '2', emit)).to.equal('1');
  50. expect(stopped).to.equal(true);
  51. });
  52. });
  53. //# sourceMappingURL=parseIncompletePhoneNumber.test.js.map