@widergy/web-utils
Version:
Utility GO! Web utils
311 lines (310 loc) • 14 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizeAccountWithDelimiter = exports.phoneDashedNormalizer = exports.rutNormalizer = exports.noEmojis = exports.replaceByRegex = exports.normalizeByRegex = exports.parseRegex = exports.normalizeCBU = exports.cuitFormatter = exports.maxLength = exports.onlyLettersAndNumbers = exports.withoutPaste = exports.changeCase = exports.normalizeAll = exports.normalizeUpperCase = exports.noConsecutiveSpaces = exports.removeWhitespaces = exports.normalizeDocumentNumber = exports.noInitialZero = exports.withoutNumbers = exports.numberMaxLength = exports.normalizeLowerCase = exports.normalizeAccount = exports.normalizeDate = exports.onlyLetters = exports.onlyLettersAndSpaces = exports.normalizeName = exports.numbersWithDecimals = exports.onlyNumbers = exports.padTwo = exports.normalizeNumberWithDecimalsInput = exports.noInitialSpace = void 0;
exports.formatCreditCardNumber = formatCreditCardNumber;
exports.formatCVC = formatCVC;
exports.formatExpirationDate = formatExpirationDate;
exports.dateToString = dateToString;
const mobile_detect_1 = __importDefault(require("mobile-detect"));
const payment_1 = __importDefault(require("payment"));
const regularExpressions_1 = require("./constants/regularExpressions");
const creditCard_1 = require("./creditCard");
const noInitialSpace = (value) => value && (value === null || value === void 0 ? void 0 : value.charAt(0)) === ' ' ? value.slice(1, value.length) : value;
exports.noInitialSpace = noInitialSpace;
const normalizeNumberWithDecimalsInput = (value, previousValue) => regularExpressions_1.numberWithDecimalsRegex.test(value) ? value : previousValue;
exports.normalizeNumberWithDecimalsInput = normalizeNumberWithDecimalsInput;
const padTwo = (number) => `0${number}`.slice(-2);
exports.padTwo = padTwo;
const onlyNumbers = (value) => (!value ? value : value.replace(/[^\d]/g, ''));
exports.onlyNumbers = onlyNumbers;
const numbersWithDecimals = (value) => !value ? value : value.replace(/[^\d,.]/g, '');
exports.numbersWithDecimals = numbersWithDecimals;
const normalizeName = (value) => !value ? value : value.replace(/[^a-zA-Z'´áéíóúÁÉÍÓÚñÑ ]/g, '');
exports.normalizeName = normalizeName;
const onlyLettersAndSpaces = (value) => !value ? value : value.replace(/[^a-zA-Z ]/gi, '');
exports.onlyLettersAndSpaces = onlyLettersAndSpaces;
const onlyLetters = (value) => !value ? value : (0, exports.onlyLettersAndSpaces)(value).replace(/[ ]/gi, '');
exports.onlyLetters = onlyLetters;
const normalizeDate = (value) => {
if (!value)
return value;
const onlyNums = (0, exports.onlyNumbers)(value);
const day = onlyNums.slice(0, 2);
const month = onlyNums.slice(2, 4);
const deviceDetector = new mobile_detect_1.default(window.navigator.userAgent);
if (!deviceDetector.phone()) {
if (onlyNums.length < 3)
return day;
if (onlyNums.length === 3)
return `${day}/${onlyNums.slice(2)}`;
if (onlyNums.length === 4)
return `${day}/${month}`;
if (onlyNums.length === 5)
return `${day}/${month}/${onlyNums.slice(4)}`;
return `${day}/${month}/${onlyNums.slice(4, 8)}`;
}
if (onlyNums.length === 8)
return `${day}/${month}/${onlyNums.slice(4)}`;
if (onlyNums.length > 8)
return `${day}/${month}/${onlyNums.slice(4, 8)}`;
return value.replace(/[^\d\u002F]/g, '');
};
exports.normalizeDate = normalizeDate;
const normalizeAccount = (value, accountLength) => !value ? value : (0, exports.onlyNumbers)(value).slice(0, accountLength);
exports.normalizeAccount = normalizeAccount;
const normalizeLowerCase = (value) => (!value ? value : value.toLowerCase());
exports.normalizeLowerCase = normalizeLowerCase;
const numberMaxLength = (length) => (value) => !value ? value : (0, exports.onlyNumbers)(value).slice(0, length);
exports.numberMaxLength = numberMaxLength;
const withoutNumbers = (value) => (!value ? value : value.replace(/[\d]/, ''));
exports.withoutNumbers = withoutNumbers;
function formatCreditCardNumber(value) {
if (!value)
return value;
let payment = {};
payment = payment_1.default;
(0, creditCard_1.fixCreditCardsPatterns)(payment);
const issuer = payment.fns.cardType(value);
const clearValue = (0, exports.onlyNumbers)(value);
let nextValue = clearValue;
switch (issuer) {
case 'amex':
nextValue = clearValue.slice(0, 15);
break;
case 'dinersclub':
nextValue = clearValue.slice(0, 14);
break;
case 'maestro':
if (clearValue.match(regularExpressions_1.maestroFirstPatternRegex)) {
nextValue = clearValue.slice(0, 18);
}
else if (clearValue.match(regularExpressions_1.maestroSecondPatternRegex)) {
nextValue = clearValue.slice(0, 19);
}
break;
default:
nextValue = clearValue.slice(0, 16);
break;
}
return nextValue;
}
function formatCVC(cvcValue, cardNumber) {
if (!cvcValue)
return cvcValue;
const clearValue = (0, exports.onlyNumbers)(cvcValue);
let maxLength = 3;
if (cardNumber) {
const issuer = payment_1.default.fns.cardType(cardNumber);
maxLength = issuer === 'amex' ? 4 : 3;
}
return clearValue.slice(0, maxLength);
}
function formatExpirationDate(value) {
if (!value)
return value;
const clearValue = (0, exports.onlyNumbers)(value);
if (clearValue.length >= 3) {
return `${clearValue.slice(0, 2)}/${clearValue.slice(2, 4)}`;
}
return clearValue;
}
const noInitialZero = (value) => (!value ? value : value.replace(/^0/g, ''));
exports.noInitialZero = noInitialZero;
const normalizeDocumentNumber = (docTypeId, docTypeKey, docTypeMapping) => (value, prevValue, allValues) => {
const tempValue = (0, exports.noInitialZero)(value);
let type = allValues[docTypeKey] || allValues.docType || allValues.document_type || allValues[`id${docTypeId}`];
type = (docTypeMapping === null || docTypeMapping === void 0 ? void 0 : docTypeMapping[type]) || type;
switch (type) {
case 'dni':
case 'dnr':
case 'provincial_ci':
case 'provincial_ci':
case 'identity_card':
case 'enlistment_book':
case 'civic_notebook':
return (0, exports.numberMaxLength)(8)(tempValue);
case 'passport':
return (0, exports.numberMaxLength)(9)(tempValue);
default:
return (0, exports.numberMaxLength)(11)(tempValue);
}
};
exports.normalizeDocumentNumber = normalizeDocumentNumber;
const removeWhitespaces = (value) => (value ? value.replace(regularExpressions_1.whitespacesRegex, '') : value);
exports.removeWhitespaces = removeWhitespaces;
const noConsecutiveSpaces = (value) => value ? value.replace(regularExpressions_1.doubleWhitespacesRegex, ' ') : value;
exports.noConsecutiveSpaces = noConsecutiveSpaces;
const normalizeUpperCase = (value) => (value ? value.toUpperCase() : value);
exports.normalizeUpperCase = normalizeUpperCase;
const normalizeAll = (normalizers) => (value, previousValue, allValues, previousAllValues) => {
let i = 0;
const normalizersLength = normalizers.length;
let currentValue = value;
while (i < normalizersLength) {
const currentNormalizer = normalizers[i];
if (typeof currentNormalizer === 'function') {
currentValue = currentNormalizer(currentValue, previousValue, allValues, previousAllValues);
}
i += 1;
}
return currentValue;
};
exports.normalizeAll = normalizeAll;
function dateToString(date, format = 'YYYY-MM-DD') {
const year = date.getYear() + 1900;
const month = `0${date.getMonth() + 1}`.substr(-2);
const day = `0${date.getDate()}`.substr(-2);
format = format.replace('YYYY', year);
format = format.replace('MM', month);
format = format.replace('DD', day);
return format;
}
const changeCase = (s, options) => {
if (options.from === 'snake' && options.to === 'camel') {
return s.replace(/(_\w)/g, (m) => m[1].toUpperCase());
}
if (options.from === 'camel' && options.to === 'snake') {
return s.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
}
if (options.from === 'text' && options.to === 'snake') {
return s
.trim()
.normalize('NFD')
.replace(/\p{Diacritic}/gu, '')
.toLowerCase()
.replace(/\s/g, '_');
}
return s;
};
exports.changeCase = changeCase;
const withoutPaste = (value, previousValue) => {
const valueLength = value.length;
const previousValueLength = previousValue ? previousValue.length : 0;
if (valueLength > 1 && previousValueLength === 0) {
return undefined;
}
if ((valueLength === 1 && previousValueLength === 0) ||
value.substring(0, valueLength - 1) === previousValue ||
valueLength < previousValueLength ||
valueLength - previousValueLength === 1) {
return value;
}
return previousValue;
};
exports.withoutPaste = withoutPaste;
const onlyLettersAndNumbers = (value) => !value ? value : value.replace(/[^a-zA-Z0-9ñÑáéíóúÁÉÍÓÚ ]/g, '');
exports.onlyLettersAndNumbers = onlyLettersAndNumbers;
const maxLength = (length) => (value) => !value ? value : value.slice(0, length);
exports.maxLength = maxLength;
const cuitFormatter = (value) => {
if (!value)
return value;
const onlyNums = (0, exports.numberMaxLength)(11)(value);
const valueArray = [...onlyNums];
if (valueArray.length > 2) {
valueArray.splice(2, 0, '-');
}
if (valueArray.length > 11) {
valueArray.splice(11, 0, '-');
}
return valueArray.join('');
};
exports.cuitFormatter = cuitFormatter;
const normalizeCBU = (cbuEntityKey, cbuNumberKey) => (value = {}) => !value
? value
: {
[cbuNumberKey]: (0, exports.onlyNumbers)(value[cbuNumberKey]) && (0, exports.numberMaxLength)(22)(value[cbuNumberKey]),
[cbuEntityKey]: value[cbuEntityKey],
};
exports.normalizeCBU = normalizeCBU;
const parseRegex = (str) => {
if (str instanceof RegExp)
return str;
const match = str.match(/^\/(.*)\/([gimsuy]*)$/);
if (match)
return new RegExp(match[1], match[2]);
};
exports.parseRegex = parseRegex;
const normalizeByRegex = (regex) => (value, previousValue) => {
const regexVerified = (0, exports.parseRegex)(regex);
return (regexVerified === null || regexVerified === void 0 ? void 0 : regexVerified.test(value)) ? value : previousValue;
};
exports.normalizeByRegex = normalizeByRegex;
const replaceByRegex = (regex, replacement = '') => (value) => {
if (!value)
return value;
const regexVerified = (0, exports.parseRegex)(regex);
return value.replace(regexVerified, replacement);
};
exports.replaceByRegex = replaceByRegex;
const noEmojis = (value) => (0, exports.replaceByRegex)(regularExpressions_1.emojiRegex)(value);
exports.noEmojis = noEmojis;
const rutNormalizer = (value) => {
let newVal = value;
newVal = newVal.slice(0, 10).toUpperCase();
newVal = newVal.replace(/[kK]\B/g, '').replace(/[^\dkK]/g, '');
if (newVal.length > 7)
newVal = `${newVal.substr(0, newVal.length - 1)}-${newVal.substr(newVal.length - 1)}`;
return newVal;
};
exports.rutNormalizer = rutNormalizer;
const phoneDashedNormalizer = (value) => {
if (value.length > 4) {
return `${value.slice(0, 4)}-${value.slice(4, 8)}`;
}
return value;
};
exports.phoneDashedNormalizer = phoneDashedNormalizer;
const normalizeAccountWithDelimiter = (prefixSlice = 2, suffixSlice = 9, delimiter = '-') => (value) => {
const valueSanitized = (0, exports.onlyNumbers)(value);
if (!valueSanitized || valueSanitized.length <= prefixSlice)
return valueSanitized;
const prefix = valueSanitized.slice(0, prefixSlice);
const suffix = valueSanitized.slice(prefixSlice, suffixSlice);
if (!suffix)
return prefix;
return `${prefix}${delimiter}${suffix}`;
};
exports.normalizeAccountWithDelimiter = normalizeAccountWithDelimiter;
const NORMALIZE_UTILS = {
noInitialSpace: exports.noInitialSpace,
normalizeNumberWithDecimalsInput: exports.normalizeNumberWithDecimalsInput,
padTwo: exports.padTwo,
onlyNumbers: exports.onlyNumbers,
numbersWithDecimals: exports.numbersWithDecimals,
normalizeName: exports.normalizeName,
onlyLettersAndSpaces: exports.onlyLettersAndSpaces,
onlyLetters: exports.onlyLetters,
normalizeDate: exports.normalizeDate,
normalizeAccount: exports.normalizeAccount,
normalizeLowerCase: exports.normalizeLowerCase,
numberMaxLength: exports.numberMaxLength,
withoutNumbers: exports.withoutNumbers,
formatCreditCardNumber,
formatCVC,
formatExpirationDate,
normalizeDocumentNumber: exports.normalizeDocumentNumber,
removeWhitespaces: exports.removeWhitespaces,
noConsecutiveSpaces: exports.noConsecutiveSpaces,
noInitialZero: exports.noInitialZero,
normalizeUpperCase: exports.normalizeUpperCase,
normalizeAll: exports.normalizeAll,
phoneDashedNormalizer: exports.phoneDashedNormalizer,
rutNormalizer: exports.rutNormalizer,
dateToString,
changeCase: exports.changeCase,
withoutPaste: exports.withoutPaste,
onlyLettersAndNumbers: exports.onlyLettersAndNumbers,
maxLength: exports.maxLength,
cuitFormatter: exports.cuitFormatter,
normalizeCBU: exports.normalizeCBU,
normalizeByRegex: exports.normalizeByRegex,
replaceByRegex: exports.replaceByRegex,
noEmojis: exports.noEmojis,
parseRegex: exports.parseRegex,
normalizeAccountWithDelimiter: exports.normalizeAccountWithDelimiter
};
exports.default = NORMALIZE_UTILS;