jexl-functions-fork
Version:
Package with available JEXL functions
118 lines (117 loc) • 4.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var jexl_1 = require("@digifi/jexl");
var module_1 = require("../utils/module");
exports.default = (0, module_1.createModule)(function (_a) {
var coerceToStringWithValidation = _a.coerceToStringWithValidation, validateTextLength = _a.validateTextLength, coerceToNumber = _a.coerceToNumber, validateArrayMaxSize = _a.validateArrayMaxSize;
var SUBSTITUTE = function (text, textToReplace, replacement) {
if (typeof text !== 'string') {
throw new jexl_1.ExecutionError('Text argument should be a string.');
}
if (typeof textToReplace !== 'string') {
throw new jexl_1.ExecutionError('Text to replace argument should be a string.');
}
if (typeof replacement !== 'string') {
throw new jexl_1.ExecutionError('Replacement should be a string.');
}
validateTextLength(text);
validateTextLength(textToReplace);
validateTextLength(replacement);
return text.replaceAll(textToReplace, replacement);
};
var REPLACE = function (text, position, length, replacement) {
if (typeof text !== 'string') {
throw new jexl_1.ExecutionError('Text argument should be a string.');
}
if (typeof replacement !== 'string') {
throw new jexl_1.ExecutionError('Replacement should be a string.');
}
validateTextLength(text);
validateTextLength(replacement);
return text.substring(0, position - 1) + replacement + text.substring(position - 1 + coerceToNumber(length));
};
var CLEAN = function (text) {
var regex = /[\0-\x1F]/g;
return coerceToStringWithValidation(text).replace(regex, '');
};
var CONCAT = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
validateArrayMaxSize(args);
return args.join('');
};
var EXACT = function (firstText, secondText) {
return coerceToStringWithValidation(firstText) === coerceToStringWithValidation(secondText);
};
var FIND = function (valueToSearch, text, position) {
if (position === void 0) { position = 0; }
return coerceToStringWithValidation(text)
.indexOf(coerceToStringWithValidation(valueToSearch), coerceToNumber(position)) + 1;
};
var LEFT = function (text, length) {
if (length === void 0) { length = 1; }
return coerceToStringWithValidation(text)
.substring(0, coerceToNumber(length));
};
var LEN = function (text) {
return coerceToStringWithValidation(text).length;
};
var LOWER = function (text) {
return coerceToStringWithValidation(text).toLowerCase();
};
var MID = function (text, start, length) {
var begin = coerceToNumber(start) - 1;
return coerceToStringWithValidation(text)
.substring(begin, begin + coerceToNumber(length));
};
var PROPER = function (text) {
return coerceToStringWithValidation(text)
.split(' ')
.map(function (word) { return word.charAt(0).toUpperCase() + word.substring(1).toLowerCase(); })
.join(' ');
};
var REPT = function (text, times) {
return coerceToStringWithValidation(text).repeat(coerceToNumber(times));
};
var RIGHT = function (text, length) {
var textString = coerceToStringWithValidation(text);
return textString.substring(textString.length - coerceToNumber(length));
};
var SEARCH = function (valueToSearch, text, position) {
if (position === void 0) { position = 0; }
return coerceToStringWithValidation(text)
.toLowerCase()
.indexOf(coerceToStringWithValidation(valueToSearch).toLowerCase(), coerceToNumber(position)) + 1;
};
var TRIM = function (text) {
return coerceToStringWithValidation(text).replace(/\s+/g, ' ').trim();
};
var UPPER = function (text) {
return coerceToStringWithValidation(text).toUpperCase();
};
var VALUE = function (text) {
return coerceToNumber(coerceToStringWithValidation(text));
};
return {
CLEAN: CLEAN,
CONCAT: CONCAT,
EXACT: EXACT,
FIND: FIND,
LEFT: LEFT,
LEN: LEN,
LOWER: LOWER,
MID: MID,
PROPER: PROPER,
REPT: REPT,
RIGHT: RIGHT,
SEARCH: SEARCH,
TRIM: TRIM,
UPPER: UPPER,
VALUE: VALUE,
REPLACE: REPLACE,
SUBSTITUTE: SUBSTITUTE,
CONCATENATE: CONCAT,
};
});