UNPKG

@digifi/jexl-functions

Version:
207 lines (206 loc) 6.82 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const errors_1 = require("../errors"); const module_1 = require("../utils/module"); const information_1 = __importDefault(require("./information")); const lodash_1 = require("lodash"); exports.default = (0, module_1.createModule)(({ validateArrayLikeValueMaxSize, coerceNullishValueToArray, }, options) => { const { ISDATESTRING, ISEMPTY } = (0, information_1.default)(); const AND = (...args) => { validateArrayLikeValueMaxSize(args); // Filter args to keep previous functions working const filteredArgs = args.filter((arg) => { return arg !== null && arg !== undefined && typeof arg !== 'string'; }); return filteredArgs.every((arg) => !!arg); }; const OR = (...args) => { validateArrayLikeValueMaxSize(args); // Filter args to keep previous functions working const filteredArgs = args.filter((arg) => { return arg !== null && arg !== undefined && typeof arg !== 'string'; }); return filteredArgs.some((arg) => !!arg); }; const XOR = (...args) => { validateArrayLikeValueMaxSize(args); const passedCount = args.reduce((previousPassedCount, arg) => { return arg ? previousPassedCount + 1 : previousPassedCount; }, 0); return !!(Math.floor(Math.abs(passedCount)) & 1); }; const IF = (condition, ...values) => { let [thenValue, elseValue] = values; // Keep it to do not break previous functions thenValue = values.length >= 1 ? thenValue : true; elseValue = values.length === 2 ? elseValue : false; if (thenValue === undefined || thenValue === null) { thenValue = 0; } if (elseValue === undefined || elseValue === null) { elseValue = 0; } return condition ? thenValue : elseValue; }; const NOT = (logical) => { return !logical; }; const IFS = (...args) => { validateArrayLikeValueMaxSize(args, options.defaultMaxArraySize * 2); for (let i = 0; i < args.length / 2; i++) { if (args[i * 2]) { return args[i * 2 + 1]; } } throw new errors_1.JexlFunctionExecutionError('Condition parameter is not defined'); }; const TRUE = () => { return true; }; const FALSE = () => { return false; }; const SWITCH = (value, ...args) => { validateArrayLikeValueMaxSize(args, options.defaultMaxArraySize * 2); const switchCount = Math.floor(args.length / 2); const defaultCase = args.length % 2 === 0 ? null : args[args.length - 1]; for (let i = 0; i < switchCount; i++) { if (value === args[i * 2]) { return args[i * 2 + 1]; } } return defaultCase; }; const GT = (value, compareTo) => { if (ISEMPTY(compareTo) || ISEMPTY(value)) { return false; } if (ISDATESTRING(compareTo)) { compareTo = Date.parse(compareTo); } if (ISDATESTRING(value)) { value = Date.parse(value); } const typedValue = value; const typedCompareTo = compareTo; return typedValue > typedCompareTo; }; const LT = (value, compareTo) => { if (ISEMPTY(compareTo) || ISEMPTY(value)) { return false; } if (ISDATESTRING(compareTo)) { compareTo = Date.parse(compareTo); } if (ISDATESTRING(value)) { value = Date.parse(value); } const typedValue = value; const typedCompareTo = compareTo; return typedValue < typedCompareTo; }; const LTE = (value, compareTo) => { if (ISEMPTY(compareTo) || ISEMPTY(value)) { return false; } if (ISDATESTRING(compareTo)) { compareTo = Date.parse(compareTo); } if (ISDATESTRING(value)) { value = Date.parse(value); } if (compareTo !== 0 && !compareTo) { return true; } const typedValue = value; const typedCompareTo = compareTo; return typedValue <= typedCompareTo; }; const GTE = (value, compareTo) => { if (ISEMPTY(compareTo) || ISEMPTY(value)) { return false; } if (ISDATESTRING(compareTo)) { compareTo = Date.parse(compareTo); } if (ISDATESTRING(value)) { value = Date.parse(value); } if (value !== 0 && !value) { return false; } const typedValue = value; const typedCompareTo = compareTo; return typedValue >= typedCompareTo; }; const RANGE = (value, min, max) => { if (ISEMPTY(min) || ISEMPTY(max) || ISEMPTY(value)) { return false; } if (ISDATESTRING(min)) { min = Date.parse(min); } if (ISDATESTRING(max)) { max = Date.parse(max); } if (ISDATESTRING(value)) { value = Date.parse(value); } if (value !== 0 && !value) { return false; } const typedValue = value; const typedMin = min; const typedMax = max; if (typedMin > typedMax) { return typedValue <= typedMin && typedValue >= typedMax; } return typedValue >= typedMin && typedValue <= typedMax; }; const EQUAL = (value, compareTo) => { if (ISDATESTRING(compareTo)) { compareTo = Date.parse(compareTo); } if (ISDATESTRING(value)) { value = Date.parse(value); } return value === compareTo; }; const INCLUDES = (value, includesSet) => { const includesSetArray = coerceNullishValueToArray(includesSet); if (!(0, lodash_1.isArrayLike)(includesSetArray)) { throw new errors_1.JexlFunctionExecutionError('Second argument is invalid.'); } validateArrayLikeValueMaxSize(includesSetArray); return includesSetArray.includes(value); }; const NOTEQUAL = (value, compareTo) => { return !EQUAL(value, compareTo); }; const NINCLUDES = (value, notIncludesSet) => { return !INCLUDES(value, notIncludesSet); }; return { AND, OR, XOR, IF, NOT, IFS, TRUE, FALSE, SWITCH, GTE, LTE, LT, GT, EQUAL, NOTEQUAL, NINCLUDES, INCLUDES, RANGE, }; });