UNPKG

@digifi/jexl-functions

Version:
121 lines (120 loc) 4.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const errors_1 = require("../errors"); const module_1 = require("../utils/module"); exports.default = (0, module_1.createModule)(({ coerceToStringWithValidation, validateTextLength, coerceToNumber, validateArrayLikeValueMaxSize, }) => { const SUBSTITUTE = (text, textToReplace, replacement) => { if (typeof text !== 'string') { throw new errors_1.JexlFunctionExecutionError('Text argument should be a string.'); } if (typeof replacement !== 'string') { throw new errors_1.JexlFunctionExecutionError('Replacement should be a string.'); } const isTextToReplaceArray = Array.isArray(textToReplace); if (typeof textToReplace !== 'string' && !isTextToReplaceArray) { throw new errors_1.JexlFunctionExecutionError('Text to replace argument should be either a string or an array.'); } if (isTextToReplaceArray && !textToReplace.every((item) => typeof item === 'string')) { throw new errors_1.JexlFunctionExecutionError('All items in text to replace array argument should be strings.'); } validateTextLength(text); validateTextLength(replacement); if (isTextToReplaceArray) { validateArrayLikeValueMaxSize(textToReplace); return textToReplace.reduce((accumulator, item) => { return accumulator.replaceAll(item, replacement); }, text); } validateTextLength(textToReplace); return text.replaceAll(textToReplace, replacement); }; const REPLACE = (text, position, length, replacement) => { if (typeof text !== 'string') { throw new errors_1.JexlFunctionExecutionError('Text argument should be a string.'); } if (typeof replacement !== 'string') { throw new errors_1.JexlFunctionExecutionError('Replacement should be a string.'); } validateTextLength(text); validateTextLength(replacement); return text.substring(0, position - 1) + replacement + text.substring(position - 1 + coerceToNumber(length)); }; const CLEAN = (text) => { // eslint-disable-next-line no-control-regex const regex = /[\0-\x1F]/g; return coerceToStringWithValidation(text).replace(regex, ''); }; const CONCAT = (...args) => { validateArrayLikeValueMaxSize(args); return args.join(''); }; const EXACT = (firstText, secondText) => { return coerceToStringWithValidation(firstText) === coerceToStringWithValidation(secondText); }; const FIND = (valueToSearch, text, position = 0) => { return coerceToStringWithValidation(text) .indexOf(coerceToStringWithValidation(valueToSearch), coerceToNumber(position)) + 1; }; const LEFT = (text, length = 1) => { return coerceToStringWithValidation(text) .substring(0, coerceToNumber(length)); }; const LEN = (text) => { return coerceToStringWithValidation(text).length; }; const LOWER = (text) => { return coerceToStringWithValidation(text).toLowerCase(); }; const MID = (text, start, length) => { const begin = coerceToNumber(start) - 1; return coerceToStringWithValidation(text) .substring(begin, begin + coerceToNumber(length)); }; const PROPER = (text) => { return coerceToStringWithValidation(text) .split(' ') .map((word) => word.charAt(0).toUpperCase() + word.substring(1).toLowerCase()) .join(' '); }; const REPT = (text, times) => { return coerceToStringWithValidation(text).repeat(coerceToNumber(times)); }; const RIGHT = (text, length) => { const textString = coerceToStringWithValidation(text); return textString.substring(textString.length - coerceToNumber(length)); }; const SEARCH = (valueToSearch, text, position = 0) => { return coerceToStringWithValidation(text) .toLowerCase() .indexOf(coerceToStringWithValidation(valueToSearch).toLowerCase(), coerceToNumber(position)) + 1; }; const TRIM = (text) => { return coerceToStringWithValidation(text).replace(/\s+/g, ' ').trim(); }; const UPPER = (text) => { return coerceToStringWithValidation(text).toUpperCase(); }; const VALUE = (text) => { return coerceToNumber(coerceToStringWithValidation(text)); }; return { CLEAN, CONCAT, EXACT, FIND, LEFT, LEN, LOWER, MID, PROPER, REPT, RIGHT, SEARCH, TRIM, UPPER, VALUE, REPLACE, SUBSTITUTE, CONCATENATE: CONCAT, }; });