parseIncompletePhoneNumber.test.js 2.4 KB

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