parsePhoneNumber.test.js 3.0 KB

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