PhoneNumber.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
  2. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
  3. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  4. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  5. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  6. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  7. import Metadata from './metadata.js';
  8. import isPossibleNumber from './isPossible.js';
  9. import isValidNumber from './isValid.js'; // import checkNumberLength from './helpers/checkNumberLength.js'
  10. import getNumberType from './helpers/getNumberType.js';
  11. import getPossibleCountriesForNumber from './helpers/getPossibleCountriesForNumber.js';
  12. import formatNumber from './format.js';
  13. var USE_NON_GEOGRAPHIC_COUNTRY_CODE = false;
  14. var PhoneNumber = /*#__PURE__*/function () {
  15. /**
  16. * @param {string} countryOrCountryCallingCode
  17. * @param {string} nationalNumber
  18. * @param {object} metadata — Metadata JSON
  19. * @return {PhoneNumber}
  20. */
  21. function PhoneNumber(countryOrCountryCallingCode, nationalNumber, metadata) {
  22. _classCallCheck(this, PhoneNumber);
  23. if (!countryOrCountryCallingCode) {
  24. throw new TypeError('`country` or `countryCallingCode` not passed');
  25. }
  26. if (!nationalNumber) {
  27. throw new TypeError('`nationalNumber` not passed');
  28. }
  29. if (!metadata) {
  30. throw new TypeError('`metadata` not passed');
  31. }
  32. var _getCountryAndCountry = getCountryAndCountryCallingCode(countryOrCountryCallingCode, metadata),
  33. country = _getCountryAndCountry.country,
  34. countryCallingCode = _getCountryAndCountry.countryCallingCode;
  35. this.country = country;
  36. this.countryCallingCode = countryCallingCode;
  37. this.nationalNumber = nationalNumber;
  38. this.number = '+' + this.countryCallingCode + this.nationalNumber; // Exclude `metadata` property output from `PhoneNumber.toString()`
  39. // so that it doesn't clutter the console output of Node.js.
  40. // Previously, when Node.js did `console.log(new PhoneNumber(...))`,
  41. // it would output the whole internal structure of the `metadata` object.
  42. this.getMetadata = function () {
  43. return metadata;
  44. };
  45. }
  46. _createClass(PhoneNumber, [{
  47. key: "setExt",
  48. value: function setExt(ext) {
  49. this.ext = ext;
  50. }
  51. }, {
  52. key: "getPossibleCountries",
  53. value: function getPossibleCountries() {
  54. if (this.country) {
  55. return [this.country];
  56. }
  57. return getPossibleCountriesForNumber(this.countryCallingCode, this.nationalNumber, this.getMetadata());
  58. }
  59. }, {
  60. key: "isPossible",
  61. value: function isPossible() {
  62. return isPossibleNumber(this, {
  63. v2: true
  64. }, this.getMetadata());
  65. }
  66. }, {
  67. key: "isValid",
  68. value: function isValid() {
  69. return isValidNumber(this, {
  70. v2: true
  71. }, this.getMetadata());
  72. }
  73. }, {
  74. key: "isNonGeographic",
  75. value: function isNonGeographic() {
  76. var metadata = new Metadata(this.getMetadata());
  77. return metadata.isNonGeographicCallingCode(this.countryCallingCode);
  78. }
  79. }, {
  80. key: "isEqual",
  81. value: function isEqual(phoneNumber) {
  82. return this.number === phoneNumber.number && this.ext === phoneNumber.ext;
  83. } // This function was originally meant to be an equivalent for `validatePhoneNumberLength()`,
  84. // but later it was found out that it doesn't include the possible `TOO_SHORT` result
  85. // returned from `parsePhoneNumberWithError()` in the original `validatePhoneNumberLength()`,
  86. // so eventually I simply commented out this method from the `PhoneNumber` class
  87. // and just left the `validatePhoneNumberLength()` function, even though that one would require
  88. // and additional step to also validate the actual country / calling code of the phone number.
  89. // validateLength() {
  90. // const metadata = new Metadata(this.getMetadata())
  91. // metadata.selectNumberingPlan(this.countryCallingCode)
  92. // const result = checkNumberLength(this.nationalNumber, metadata)
  93. // if (result !== 'IS_POSSIBLE') {
  94. // return result
  95. // }
  96. // }
  97. }, {
  98. key: "getType",
  99. value: function getType() {
  100. return getNumberType(this, {
  101. v2: true
  102. }, this.getMetadata());
  103. }
  104. }, {
  105. key: "format",
  106. value: function format(_format, options) {
  107. return formatNumber(this, _format, options ? _objectSpread(_objectSpread({}, options), {}, {
  108. v2: true
  109. }) : {
  110. v2: true
  111. }, this.getMetadata());
  112. }
  113. }, {
  114. key: "formatNational",
  115. value: function formatNational(options) {
  116. return this.format('NATIONAL', options);
  117. }
  118. }, {
  119. key: "formatInternational",
  120. value: function formatInternational(options) {
  121. return this.format('INTERNATIONAL', options);
  122. }
  123. }, {
  124. key: "getURI",
  125. value: function getURI(options) {
  126. return this.format('RFC3966', options);
  127. }
  128. }]);
  129. return PhoneNumber;
  130. }();
  131. export { PhoneNumber as default };
  132. var isCountryCode = function isCountryCode(value) {
  133. return /^[A-Z]{2}$/.test(value);
  134. };
  135. function getCountryAndCountryCallingCode(countryOrCountryCallingCode, metadataJson) {
  136. var country;
  137. var countryCallingCode;
  138. var metadata = new Metadata(metadataJson); // If country code is passed then derive `countryCallingCode` from it.
  139. // Also store the country code as `.country`.
  140. if (isCountryCode(countryOrCountryCallingCode)) {
  141. country = countryOrCountryCallingCode;
  142. metadata.selectNumberingPlan(country);
  143. countryCallingCode = metadata.countryCallingCode();
  144. } else {
  145. countryCallingCode = countryOrCountryCallingCode;
  146. /* istanbul ignore if */
  147. if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
  148. if (metadata.isNonGeographicCallingCode(countryCallingCode)) {
  149. country = '001';
  150. }
  151. }
  152. }
  153. return {
  154. country: country,
  155. countryCallingCode: countryCallingCode
  156. };
  157. }
  158. //# sourceMappingURL=PhoneNumber.js.map