UNPKG

@type-ddd/cnpj

Version:

Library that provides TypeScript type definitions for handling CNPJ (Cadastro Nacional da Pessoa Jurídica) in Domain-Driven Design contexts. It facilitates the validation and manipulation of CNPJ numbers, ensuring they adhere to the Brazilian legal standa

114 lines (113 loc) 3.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CNPJ = void 0; const rich_domain_1 = require("rich-domain"); const util_1 = require("./util"); const regexCnpj = /^([0-9]{2})[\.]([0-9]{3})[\.]((?!\2)[0-9]{3})[\/]([0-9]{4})[-]([0-9]{2})$|^[0-9]{14}$/; class CNPJ extends rich_domain_1.ValueObject { constructor(props) { super(props); } /** * @description return a cnpj value (only numbers). * @example example "22398345000188". * @summary If you want cnpj as pattern use `formatToCnpjPattern` before get value. */ value() { return this.props; } /** * @description add hyphen and dot to cnpj value. * @example before "22398345000188" * @example after "22.398.345/0001-88" */ toPattern() { return (0, util_1.formatValueToCnpjPattern)(this.props); } /** * @description add hyphen and dot to cnpj value. * @example before "22398345000188" * @example after "22.398.345/0001-88" */ static addMask(cnpj) { return (0, util_1.formatValueToCnpjPattern)(cnpj); } /** * @description remove hyphen and dot from cnpj value. * @example before "22.398.345/0001-88" * @example after "22398345000188" */ static removeSpecialChars(cnpj) { return (0, util_1.removeSpecialCharsFromCnpj)(cnpj); } /** * * @param cnpj value as string only number or pattern. * @returns true if cnpj match with instance value and false if not. * @example param "22398345000188" * @example param "22.398.345/0001-88" */ compare(cnpj) { if (typeof cnpj === 'string') { const formattedCnpj = (0, util_1.removeSpecialCharsFromCnpj)(cnpj); const instanceValue = this.props; return instanceValue === formattedCnpj; } if (cnpj instanceof CNPJ) { return cnpj.isEqual(this); } return false; } /** * @description check if cnpj value is a valid pattern and has a valid digit sum. * @param value cnpj as string * @returns true if value is valid and false if not. * @example "22.398.345/0001-88" * @example "22398345000188" */ static isValidProps(value) { const isValidPattern = CNPJ.REGEX.test(value); const isValidDigits = (0, util_1.default)(value); return isValidDigits && isValidPattern; } /** * @description check if cnpj value is a valid pattern and has a valid digit sum. * @param value cnpj as string * @returns true if value is valid and false if not. * @example "22.398.345/0001-88" * @example "22398345000188" */ static isValid(value) { return this.isValidProps(value); } /** * * @param value value as string * @returns instance of CNPJ or throw an error */ static init(value) { const isValidValue = CNPJ.isValidProps(value); if (!isValidValue) throw new Error(CNPJ.MESSAGE); return new CNPJ(value); } /** * @description create a cnpj value object * @param value cnpj numbers as string * @returns instance of Result with cnpj value * @example "22.398.345/0001-88" * @example "22398345000188" * @summary fails if provide an invalid pattern or a cnpj with invalid digit sum */ static create(value) { const isValidValue = CNPJ.isValidProps(value); if (!isValidValue) { return rich_domain_1.Result.fail(CNPJ.MESSAGE); } return rich_domain_1.Result.Ok(new CNPJ((0, util_1.removeSpecialCharsFromCnpj)(value))); } } exports.CNPJ = CNPJ; CNPJ.REGEX = regexCnpj; CNPJ.MESSAGE = 'Invalid value for cnpj'; exports.default = CNPJ;