UNPKG

libphonenumber-js

Version:

A simpler (and smaller) rewrite of Google Android's libphonenumber library in javascript

130 lines (129 loc) 5.56 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } // This "state" object simply holds the state of the "AsYouType" parser: // // * `country?: string` // * `callingCode?: string` // * `digits: string` // * `international: boolean` // * `missingPlus: boolean` // * `IDDPrefix?: string` // * `carrierCode?: string` // * `nationalPrefix?: string` // * `nationalSignificantNumber?: string` // * `nationalSignificantNumberMatchesInput: boolean` // * `complexPrefixBeforeNationalSignificantNumber?: string` // // `state.country` and `state.callingCode` aren't required to be in sync. // For example, `state.country` could be `"AR"` and `state.callingCode` could be `undefined`. // So `state.country` and `state.callingCode` are totally independent. // var AsYouTypeState = /*#__PURE__*/function () { function AsYouTypeState(_ref) { var onCountryChange = _ref.onCountryChange, onCallingCodeChange = _ref.onCallingCodeChange; _classCallCheck(this, AsYouTypeState); this.onCountryChange = onCountryChange; this.onCallingCodeChange = onCallingCodeChange; } return _createClass(AsYouTypeState, [{ key: "reset", value: function reset(_ref2) { var country = _ref2.country, callingCode = _ref2.callingCode; this.international = false; this.missingPlus = false; this.IDDPrefix = undefined; this.callingCode = undefined; this.digits = ''; this.resetNationalSignificantNumber(); this.initCountryAndCallingCode(country, callingCode); } }, { key: "resetNationalSignificantNumber", value: function resetNationalSignificantNumber() { this.nationalSignificantNumber = this.getNationalDigits(); this.nationalSignificantNumberMatchesInput = true; this.nationalPrefix = undefined; this.carrierCode = undefined; this.complexPrefixBeforeNationalSignificantNumber = undefined; } }, { key: "update", value: function update(properties) { for (var _i = 0, _Object$keys = Object.keys(properties); _i < _Object$keys.length; _i++) { var key = _Object$keys[_i]; this[key] = properties[key]; } } }, { key: "initCountryAndCallingCode", value: function initCountryAndCallingCode(country, callingCode) { this.setCountry(country); this.setCallingCode(callingCode); } }, { key: "setCountry", value: function setCountry(country) { this.country = country; this.onCountryChange(country); } }, { key: "setCallingCode", value: function setCallingCode(callingCode) { this.callingCode = callingCode; this.onCallingCodeChange(callingCode, this.country); } }, { key: "startInternationalNumber", value: function startInternationalNumber(country, callingCode) { // Prepend the `+` to parsed input. this.international = true; // If a default country was set then reset it // because an explicitly international phone // number is being entered. this.initCountryAndCallingCode(country, callingCode); } }, { key: "appendDigits", value: function appendDigits(nextDigits) { this.digits += nextDigits; } }, { key: "appendNationalSignificantNumberDigits", value: function appendNationalSignificantNumberDigits(nextDigits) { this.nationalSignificantNumber += nextDigits; } /** * Returns the part of `this.digits` that corresponds to the national number. * Basically, all digits that have been input by the user, except for the * international prefix and the country calling code part * (if the number is an international one). * @return {string} */ }, { key: "getNationalDigits", value: function getNationalDigits() { if (this.international) { return this.digits.slice((this.IDDPrefix ? this.IDDPrefix.length : 0) + (this.callingCode ? this.callingCode.length : 0)); } return this.digits; } }, { key: "getDigitsWithoutInternationalPrefix", value: function getDigitsWithoutInternationalPrefix() { if (this.international) { if (this.IDDPrefix) { return this.digits.slice(this.IDDPrefix.length); } } return this.digits; } }]); }(); export { AsYouTypeState as default }; //# sourceMappingURL=AsYouTypeState.js.map