UNPKG

@kubit-ui-web/react-components

Version:

Kubit React Components is a customizable, accessible library of React web components, designed to enhance your application's user experience

49 lines 2.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatMask = exports.matchInputValue = exports.cleanInputValue = exports.cleanInputValueObject = exports.cleanInputValueLettersHyphenSpace = exports.cleanInputValueLetters = exports.cleanInputValueNumberWithSpaces = exports.cleanInputValueNumber = exports.cleanInputValueAlphanumericWithSpaces = exports.cleanInputValueAlphanumeric = void 0; const input_1 = require("../../components/input/types/input"); const cleanInputValueAlphanumeric = (value) => value.replace(/[\W_]+/g, ''); exports.cleanInputValueAlphanumeric = cleanInputValueAlphanumeric; const cleanInputValueAlphanumericWithSpaces = (value) => value.replace(/[^\w\s]+/g, ''); exports.cleanInputValueAlphanumericWithSpaces = cleanInputValueAlphanumericWithSpaces; const cleanInputValueNumber = (value) => value.replace(/[^\d]/g, ''); exports.cleanInputValueNumber = cleanInputValueNumber; const cleanInputValueNumberWithSpaces = (value) => value.replace(/[^\d\s]/g, ''); exports.cleanInputValueNumberWithSpaces = cleanInputValueNumberWithSpaces; const cleanInputValueLetters = (value) => value.replace(/[^a-zñA-ZÑ]/g, ''); exports.cleanInputValueLetters = cleanInputValueLetters; const cleanInputValueLettersHyphenSpace = (value) => value.replace(/[^a-zñA-ZÑ\s-]/g, ''); exports.cleanInputValueLettersHyphenSpace = cleanInputValueLettersHyphenSpace; exports.cleanInputValueObject = { [input_1.MASK_TYPE.ALPHANUMERIC]: (value) => (0, exports.cleanInputValueAlphanumeric)(value), [input_1.MASK_TYPE.ALPHANUMERIC_WITH_SPACES]: (value) => (0, exports.cleanInputValueAlphanumericWithSpaces)(value), [input_1.MASK_TYPE.NUMBERS]: (value) => (0, exports.cleanInputValueNumber)(value), [input_1.MASK_TYPE.NUMBERS_WITH_SPACES]: (value) => (0, exports.cleanInputValueNumberWithSpaces)(value), [input_1.MASK_TYPE.LETTERS]: (value) => (0, exports.cleanInputValueLetters)(value), [input_1.MASK_TYPE.LETTERS_HYPHEN_SPACE]: (value) => (0, exports.cleanInputValueLettersHyphenSpace)(value), }; const cleanInputValue = (value, maskType) => exports.cleanInputValueObject[maskType || input_1.MASK_TYPE.ALPHANUMERIC](value); exports.cleanInputValue = cleanInputValue; const matchInputValue = (oldValue, value, regex) => { const newRegex = typeof regex === 'string' ? new RegExp(regex) : regex; if (newRegex.test(value) || value === '') { return value; } return oldValue; }; exports.matchInputValue = matchInputValue; const formatMask = (unformattedValue, mask) => { let i = 0; let lastIndex = -1; const getValueIndex = (_, prevIndex) => { if (i >= unformattedValue?.length) { return '#'; } lastIndex = prevIndex; return unformattedValue[i++]; }; const replaceCodeForValue = mask.replace(/#/g, getValueIndex); return replaceCodeForValue.substring(0, lastIndex + 1); }; exports.formatMask = formatMask; //# sourceMappingURL=mask.utility.js.map