PhoneNumber.js 7.6 KB

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