parsePhoneNumberWithError.test.js 5.1 KB

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