UNPKG

@devix-tecnologia/value-objects

Version:

Coleção de objetos de valores para implementação em sistemas.

128 lines (127 loc) 4.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Telefone = void 0; const country_codes_js_1 = require("./country_codes.js"); const ddd_js_1 = require("./ddd.js"); /** * Cria objeto de valor para número de telefone. * Valida DDD e código do país. * É possível cadastrar usando formato string para números Brasileiros. * Aceita formatos como: 27 97645-5555 ou 55 27 97645-5555. * Desconsidera espaços e qq caracter não numérico */ class Telefone { constructor(phoneNum) { this._phoneNum = ''; this._areaCode = ''; this._countryCode = ''; this._isValid = false; if (typeof phoneNum === 'string') { this.parseFromString(phoneNum); } else { this.parseFromObject(phoneNum); } } parseFromString(phoneStr) { let tel = phoneStr.replace(/[^\d]+/g, ''); if ((tel.length === 12 || tel.length === 11) && tel[0] === '0') { tel = tel.slice(1); } if (tel.length === 10 || tel.length === 11) { this._countryCode = '55'; this._areaCode = tel.slice(0, 2); this._phoneNum = tel.slice(2); this._isValid = this.isDDDValid(); } else if (tel.length === 12 || tel.length === 13) { this._countryCode = tel.slice(0, 2); this._areaCode = tel.slice(2, 4); this._phoneNum = tel.slice(4); this._isValid = this.isDDDValid() && this._countryCode === '55'; } else { throw new Error('Could not parse phone number from string. Try creating the record using an object { countryCode: string; areaCode: string; phoneNum: string }'); } } parseFromObject(phone) { this._countryCode = phone.countryCode.replace(/[^\d]+/g, ''); this._areaCode = phone.areaCode.replace(/[^\d]+/g, ''); this._phoneNum = phone.phoneNum.replace(/[^\d]+/g, ''); if (this._countryCode === '55') { this._isValid = this.isDDDValid() && this.isCountryCodeValid(); } else { this._isValid = this.isCountryCodeValid(); } } get type() { return 'PHONE'; } get version() { return Telefone.VERSION; } get validation() { if (this._countryCode === '55') { return 'Validação de DDD brasileiro e código de país'; } return 'Validação de código de país'; } get formatted() { this.isValid({ raiseException: true }); const num = this._phoneNum.split(''); const lastDigits = num.splice(-4, 4); return `+${this._countryCode} ${this._areaCode} ${num.join('')}-${lastDigits.join('')}`; } get onlyNumbers() { this.isValid({ raiseException: true }); return `${this._countryCode}${this._areaCode}${this._phoneNum}`; } get country() { return country_codes_js_1.countryCodes[this._countryCode].code; } get estate() { if (this._countryCode === '55') return ddd_js_1.DDD[this._areaCode]; return null; } toJSON() { return { type: this.type, value: this.onlyNumbers, formatted: this.formatted, isValid: this._isValid, version: this.version, country: this.country, estate: this.estate, }; } toString() { return this.formatted; } isValid(config = { raiseException: false }) { if (config.raiseException && !this._isValid) { throw new Error('Invalid phone number'); } return this._isValid; } equals(other) { if (!this._isValid) return false; try { const otherPhone = other instanceof Telefone ? other : new Telefone(other); return this.onlyNumbers === otherPhone.onlyNumbers; } catch (_a) { return false; } } isDDDValid() { return ddd_js_1.DDD[this._areaCode] !== undefined; } isCountryCodeValid() { return country_codes_js_1.countryCodes[this._countryCode] !== undefined; } } exports.Telefone = Telefone; Telefone.VERSION = '1.0.0';