UNPKG

libphonenumber-js

Version:

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

123 lines (108 loc) 4.48 kB
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } 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; } var AsYouTypeState = /*#__PURE__*/ function () { function AsYouTypeState(_ref) { var _this = this; var onCountryChange = _ref.onCountryChange, onCallingCodeChange = _ref.onCallingCodeChange; _classCallCheck(this, AsYouTypeState); _defineProperty(this, "update", function (properties) { for (var _i = 0, _Object$keys = Object.keys(properties); _i < _Object$keys.length; _i++) { var key = _Object$keys[_i]; _this[key] = properties[key]; } }); this.onCountryChange = onCountryChange; this.onCallingCodeChange = onCallingCodeChange; } _createClass(AsYouTypeState, [{ key: "reset", value: function reset(defaultCountry, defaultCallingCode) { this.international = false; this.IDDPrefix = undefined; this.missingPlus = undefined; this.callingCode = undefined; this.digits = ''; this.resetNationalSignificantNumber(); this.initCountryAndCallingCode(defaultCountry, defaultCallingCode); } }, { key: "resetNationalSignificantNumber", value: function resetNationalSignificantNumber() { this.nationalSignificantNumber = this.getNationalDigits(); this.nationalSignificantNumberMatchesInput = true; this.nationalPrefix = undefined; this.carrierCode = undefined; this.complexPrefixBeforeNationalSignificantNumber = undefined; } }, { 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; return this.onCallingCodeChange(this.country, callingCode); } }, { key: "startInternationalNumber", value: function startInternationalNumber() { // 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(); } }, { 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; } }]); return AsYouTypeState; }(); export { AsYouTypeState as default }; //# sourceMappingURL=AsYouTypeState.js.map