PhoneNumber.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import Metadata from './metadata.js'
  2. import isPossibleNumber from './isPossible.js'
  3. import isValidNumber from './isValid.js'
  4. // import checkNumberLength from './helpers/checkNumberLength.js'
  5. import getNumberType from './helpers/getNumberType.js'
  6. import getPossibleCountriesForNumber from './helpers/getPossibleCountriesForNumber.js'
  7. import formatNumber from './format.js'
  8. const USE_NON_GEOGRAPHIC_COUNTRY_CODE = false
  9. export default class PhoneNumber {
  10. /**
  11. * @param {string} countryOrCountryCallingCode
  12. * @param {string} nationalNumber
  13. * @param {object} metadata — Metadata JSON
  14. * @return {PhoneNumber}
  15. */
  16. constructor(countryOrCountryCallingCode, nationalNumber, metadata) {
  17. if (!countryOrCountryCallingCode) {
  18. throw new TypeError('`country` or `countryCallingCode` not passed')
  19. }
  20. if (!nationalNumber) {
  21. throw new TypeError('`nationalNumber` not passed')
  22. }
  23. if (!metadata) {
  24. throw new TypeError('`metadata` not passed')
  25. }
  26. const { country, countryCallingCode } = getCountryAndCountryCallingCode(
  27. countryOrCountryCallingCode,
  28. metadata
  29. )
  30. this.country = country
  31. this.countryCallingCode = countryCallingCode
  32. this.nationalNumber = nationalNumber
  33. this.number = '+' + this.countryCallingCode + this.nationalNumber
  34. // Exclude `metadata` property output from `PhoneNumber.toString()`
  35. // so that it doesn't clutter the console output of Node.js.
  36. // Previously, when Node.js did `console.log(new PhoneNumber(...))`,
  37. // it would output the whole internal structure of the `metadata` object.
  38. this.getMetadata = () => metadata
  39. }
  40. setExt(ext) {
  41. this.ext = ext
  42. }
  43. getPossibleCountries() {
  44. if (this.country) {
  45. return [this.country]
  46. }
  47. return getPossibleCountriesForNumber(
  48. this.countryCallingCode,
  49. this.nationalNumber,
  50. this.getMetadata()
  51. )
  52. }
  53. isPossible() {
  54. return isPossibleNumber(this, { v2: true }, this.getMetadata())
  55. }
  56. isValid() {
  57. return isValidNumber(this, { v2: true }, this.getMetadata())
  58. }
  59. isNonGeographic() {
  60. const metadata = new Metadata(this.getMetadata())
  61. return metadata.isNonGeographicCallingCode(this.countryCallingCode)
  62. }
  63. isEqual(phoneNumber) {
  64. return this.number === phoneNumber.number && this.ext === phoneNumber.ext
  65. }
  66. // This function was originally meant to be an equivalent for `validatePhoneNumberLength()`,
  67. // but later it was found out that it doesn't include the possible `TOO_SHORT` result
  68. // returned from `parsePhoneNumberWithError()` in the original `validatePhoneNumberLength()`,
  69. // so eventually I simply commented out this method from the `PhoneNumber` class
  70. // and just left the `validatePhoneNumberLength()` function, even though that one would require
  71. // and additional step to also validate the actual country / calling code of the phone number.
  72. // validateLength() {
  73. // const metadata = new Metadata(this.getMetadata())
  74. // metadata.selectNumberingPlan(this.countryCallingCode)
  75. // const result = checkNumberLength(this.nationalNumber, metadata)
  76. // if (result !== 'IS_POSSIBLE') {
  77. // return result
  78. // }
  79. // }
  80. getType() {
  81. return getNumberType(this, { v2: true }, this.getMetadata())
  82. }
  83. format(format, options) {
  84. return formatNumber(
  85. this,
  86. format,
  87. options ? { ...options, v2: true } : { v2: true },
  88. this.getMetadata()
  89. )
  90. }
  91. formatNational(options) {
  92. return this.format('NATIONAL', options)
  93. }
  94. formatInternational(options) {
  95. return this.format('INTERNATIONAL', options)
  96. }
  97. getURI(options) {
  98. return this.format('RFC3966', options)
  99. }
  100. }
  101. const isCountryCode = (value) => /^[A-Z]{2}$/.test(value)
  102. function getCountryAndCountryCallingCode(countryOrCountryCallingCode, metadataJson) {
  103. let country
  104. let countryCallingCode
  105. const metadata = new Metadata(metadataJson)
  106. // If country code is passed then derive `countryCallingCode` from it.
  107. // Also store the country code as `.country`.
  108. if (isCountryCode(countryOrCountryCallingCode)) {
  109. country = countryOrCountryCallingCode
  110. metadata.selectNumberingPlan(country)
  111. countryCallingCode = metadata.countryCallingCode()
  112. } else {
  113. countryCallingCode = countryOrCountryCallingCode
  114. /* istanbul ignore if */
  115. if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
  116. if (metadata.isNonGeographicCallingCode(countryCallingCode)) {
  117. country = '001'
  118. }
  119. }
  120. }
  121. return {
  122. country,
  123. countryCallingCode
  124. }
  125. }