UNPKG

@devix-tecnologia/value-objects

Version:

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

153 lines (152 loc) 4.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CpfCnpj = void 0; class CpfCnpj { constructor(numDoc) { this._isValid = false; const doc = String(numDoc).replace(/[^\d]+/g, ''); this._docId = doc; if (doc.length === 11 && this.isCpfValid()) { this._docType = 'CPF'; this._isValid = true; } else if (doc.length === 14 && this.isCnpjValid()) { this._docType = 'CNPJ'; this._isValid = true; } else { this._docType = 'INVALID'; } } get type() { return this._docType; } get version() { return CpfCnpj.VERSION; } get validation() { return this._docType === 'CPF' ? 'Módulo 11 para CPF' : 'Módulo 11 para CNPJ'; } get formatted() { this.isValid({ raiseException: true }); if (this._docType === 'CPF') { return this._docId.replace(/^(\d{3})(\d{3})(\d{3})(\d{2})$/, '$1.$2.$3-$4'); } return this._docId.replace(/^(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/, '$1.$2.$3/$4-$5'); } get onlyNumbers() { this.isValid({ raiseException: true }); return this._docId; } toJSON() { return { type: this.type, value: this.onlyNumbers, formatted: this.formatted, isValid: this._isValid, version: this.version, }; } toString() { return this.formatted; } isValid(config = { raiseException: false }) { if (config.raiseException && !this._isValid) { throw new Error(`Invalid ${this._docType.toLowerCase()}`); } return this._isValid; } equals(other) { if (!this._isValid) return false; try { const otherDoc = other instanceof CpfCnpj ? other : new CpfCnpj(other); return this.onlyNumbers === otherDoc.onlyNumbers; } catch (_a) { return false; } } static newCpf() { let doc_id = ''; for (let i = 0; i < 9; i += 1) { doc_id += Math.floor(Math.random() * 9); } doc_id += this.CpfVerifierDigits(doc_id); doc_id += this.CpfVerifierDigits(doc_id); return new CpfCnpj(doc_id); } static newCnpj() { let doc_id = ''; for (let i = 0; i < 12; i += 1) { doc_id += Math.floor(Math.random() * 9); } doc_id += this.CnpjVerifierDigits(doc_id); doc_id += this.CnpjVerifierDigits(doc_id); return new CpfCnpj(doc_id); } isCpfValid() { const blocklist = [ '00000000000', '11111111111', '22222222222', '33333333333', '44444444444', '55555555555', '66666666666', '77777777777', '88888888888', '99999999999', '12345678909', ]; if (blocklist.includes(this._docId)) return false; let numbers = this._docId.slice(0, 9); numbers += CpfCnpj.CpfVerifierDigits(numbers); numbers += CpfCnpj.CpfVerifierDigits(numbers); return numbers.slice(-2) === this._docId.slice(-2); } isCnpjValid() { const blocklist = [ '00000000000000', '11111111111111', '22222222222222', '33333333333333', '44444444444444', '55555555555555', '66666666666666', '77777777777777', '88888888888888', '99999999999999', ]; if (blocklist.includes(this._docId)) return false; return true; } static CpfVerifierDigits(doc_ident) { const numbers = doc_ident.split('').map((number) => parseInt(number, 10)); const modulus = numbers.length + 1; const multiplied = numbers.map((number, index) => number * (modulus - index)); const mod = multiplied.reduce((buffer, number) => buffer + number) % 11; return mod < 2 ? 0 : 11 - mod; } static CnpjVerifierDigits(doc_ident) { let index = 2; const reverse = doc_ident .split('') .reduce((buffer, number) => { return [parseInt(number, 10)].concat(buffer); }, []); const sum = reverse.reduce((buffer, number) => { buffer += number * index; index = index === 9 ? 2 : index + 1; return buffer; }, 0); const mod = sum % 11; return mod < 2 ? 0 : 11 - mod; } } exports.CpfCnpj = CpfCnpj; CpfCnpj.VERSION = '1.0.0';