@type-ddd/cpf
Version:
This package provides TypeScript type definitions for handling CPF (Cadastro de Pessoa Física) in Domain-Driven Design contexts
114 lines (113 loc) • 3.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CPF = void 0;
const rich_domain_1 = require("rich-domain");
const util_1 = require("./util");
class CPF extends rich_domain_1.ValueObject {
constructor(value) {
super(value);
}
/**
* @description return a cpf value (only numbers).
* @example example "52734865211".
* @summary If you want cpf as pattern use `formatToCpfPattern` before get value.
*/
value() {
return this.props;
}
/**
* @description add hyphen and dot to cpf value.
* @example before "52734865211"
* @example after "527.348.652-11"
*/
toPattern() {
return (0, util_1.formatValueToCpfPattern)(this.props);
}
/**
* @description add hyphen and dot to cpf value.
* @example before "52734865211"
* @example after "527.348.652-11"
*/
static addMask(cpf) {
return (0, util_1.formatValueToCpfPattern)(cpf);
}
/**
* @description remove hyphen and dot from cpf value.
* @example before "527.348.652-11"
* @example after "52734865211"
*/
static removeSpecialChars(cpf) {
return this.util.string(cpf).removeSpecialChars();
}
/**
*
* @param cpf value as string only number or pattern Or instance of CPF.
* @returns true if cpf match with instance value and false if not.
* @example param "52734865211"
* @example param "527.348.652-11"
*/
compare(cpf) {
if (typeof cpf === 'string') {
const valueA = this.util.string(cpf).removeSpecialChars();
const valueB = this.util
.string(this.props)
.removeSpecialChars();
return valueA === valueB;
}
if (cpf instanceof CPF)
return cpf.isEqual(this);
return false;
}
/**
* @description check if cpf value is a valid pattern and has a valid digit sum.
* @param value cpf as string
* @returns true if value is valid and false if not.
* @example "527.348.652-11"
* @example "72725477824"
*/
static isValidProps(value) {
const isValidPattern = CPF.REGEX.test(value);
const isValidDigits = (0, util_1.default)(value);
return isValidDigits && isValidPattern;
}
/**
* @description check if cpf value is a valid pattern and has a valid digit sum.
* @param value cpf as string
* @returns true if value is valid and false if not.
* @example "527.348.652-11"
* @example "72725477824"
*/
static isValid(value) {
return this.isValidProps(value);
}
/**
*
* @param value value as string
* @returns instance of CPF or throw an error
*/
static init(value) {
const isValidValue = CPF.isValidProps(value);
if (!isValidValue)
throw new Error(CPF.MESSAGE);
return new CPF(this.util.string(value).removeSpecialChars());
}
/**
* @description create a cpf value object
* @param value cpf numbers as string
* @returns instance of Result with cpf value
* @example "527.348.652-11"
* @example "72725477824"
* @summary fails if provide an invalid pattern or a cpf with invalid digit sum
*/
static create(value) {
const isValidValue = CPF.isValidProps(value);
if (!isValidValue) {
return rich_domain_1.Result.fail(CPF.MESSAGE);
}
return rich_domain_1.Result.Ok(new CPF(this.util.string(value).removeSpecialChars()));
}
}
exports.CPF = CPF;
CPF.REGEX = /^([0-9]{3})[\.]((?!\1)[0-9]{3})[\.]([0-9]{3})[-]([0-9]{2})$|^[0-9]{11}$/;
CPF.MESSAGE = 'Invalid value for cpf';
exports.default = CPF;