pixkey
Version:
Identify, validate, format and normalize a Pix key type from a string
117 lines (116 loc) • 3.53 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PIX_KEY_PHONE = exports.PIX_KEY_EMAIL = exports.PIX_KEY_RANDOM = exports.PIX_KEY_CNPJ = exports.PIX_KEY_CPF = void 0;
exports.validate = validate;
exports.normalize = normalize;
exports.format = format;
const js_brasil_1 = require("js-brasil");
const uuid_validate_1 = __importDefault(require("uuid-validate"));
const mobile_1 = require("libphonenumber-js/mobile");
const email_validator_1 = require("email-validator");
exports.PIX_KEY_CPF = "cpf";
exports.PIX_KEY_CNPJ = "cnpj";
exports.PIX_KEY_RANDOM = "random";
exports.PIX_KEY_EMAIL = "email";
exports.PIX_KEY_PHONE = "phone";
/**
* Validates a given pix key and returns a list of possible key types.
*/
function validate(pixKey) {
const keyTypes = [];
pixKey = pixKey.trim();
if (js_brasil_1.validateBr.cpf(pixKey)) {
keyTypes.push(exports.PIX_KEY_CPF);
}
if (js_brasil_1.validateBr.cnpj(pixKey)) {
keyTypes.push(exports.PIX_KEY_CNPJ);
}
if ((0, uuid_validate_1.default)(pixKey)) {
keyTypes.push(exports.PIX_KEY_RANDOM);
}
if ((0, mobile_1.isValidPhoneNumber)(pixKey, 'BR')) {
keyTypes.push(exports.PIX_KEY_PHONE);
}
if ((0, email_validator_1.validate)(pixKey)) {
keyTypes.push(exports.PIX_KEY_EMAIL);
}
return keyTypes;
}
/**
* Normalizes a pix key to a standard format depending on its type.
*/
function normalize(pixKey, as = null) {
pixKey = pixKey.trim();
let useAs = validate(pixKey);
if (useAs.length > 1) {
if (as && useAs.includes(as)) {
useAs = as;
}
else {
return null;
}
}
else if (!useAs.length) {
return null;
}
else {
useAs = useAs[0];
}
switch (useAs) {
case exports.PIX_KEY_CPF:
case exports.PIX_KEY_CNPJ:
return pixKey.replace(/[^0-9]/g, '');
case exports.PIX_KEY_RANDOM:
case exports.PIX_KEY_EMAIL:
return pixKey;
case exports.PIX_KEY_PHONE: {
const phoneNumber = (0, mobile_1.parsePhoneNumber)(pixKey, 'BR');
return phoneNumber.number.toString();
}
default:
return null;
}
}
/**
* Formats a pix key in a user-friendly way depending on its type.
*/
function format(pixKey, as = null) {
const normalized = normalize(pixKey, as);
if (!normalized)
return null;
let useAs = validate(normalized);
if (useAs.length > 1) {
if (as && useAs.includes(as)) {
useAs = as;
}
else {
return null;
}
}
else if (!useAs.length) {
return null;
}
else {
useAs = useAs[0];
}
switch (useAs) {
case exports.PIX_KEY_CPF:
return js_brasil_1.maskBr.cpf(normalized);
case exports.PIX_KEY_CNPJ:
return js_brasil_1.maskBr.cnpj(normalized);
case exports.PIX_KEY_RANDOM:
case exports.PIX_KEY_EMAIL:
return normalized;
case exports.PIX_KEY_PHONE: {
const phoneNumber = (0, mobile_1.parsePhoneNumber)(normalized, 'BR');
return phoneNumber.country === 'BR'
? phoneNumber.formatNational()
: phoneNumber.formatInternational();
}
default:
return null;
}
}