UNPKG

@digifi/jexl-functions

Version:
119 lines (118 loc) 4.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.evalCriteriaParseResult = exports.parseCriteriaExpression = exports.CRITERIA_OPERATORS_SET = void 0; const CRITERIA_OPERATORS = ['>=', '<=', '<>', '=', '>', '<']; const CRITERIA_OPERATORS_WITH_NULLISH_VALUE_CHECK = ['<>']; exports.CRITERIA_OPERATORS_SET = new Set(CRITERIA_OPERATORS); const DEFAULT_OPERATION = '='; const SPLIT_REGEX = new RegExp(`^(${CRITERIA_OPERATORS.join('|')})`); const SYSTEM_CRITERIA = ['#TRUE', '#FALSE', '#UNDEFINED', '#NULL', '#EMPTY', '#NOT_EMPTY', '#BLANK', '#NOT_BLANK']; const isSystemCriteria = (criteria) => { return SYSTEM_CRITERIA.includes(criteria); }; const isSystemCriteriaParseResult = (result) => { return !!result.systemCriteria; }; const coerceOperandToNumber = (operand) => { const coerced = Number(operand); return Number.isNaN(coerced) ? operand : coerced; }; const parseCriteriaExpression = (criteria) => { if (Array.isArray(criteria)) { const [operator, rightOperand] = criteria; return { operator, rightOperand, disableCoercing: true, nullishValuesComparable: CRITERIA_OPERATORS_WITH_NULLISH_VALUE_CHECK.includes(operator), }; } if (isSystemCriteria(criteria)) { return { systemCriteria: criteria, }; } const tokens = criteria .split(SPLIT_REGEX) .filter((token) => !!token); const [operation = '', operand = ''] = tokens; const isOperationExists = exports.CRITERIA_OPERATORS_SET.has(operation); return { operator: isOperationExists ? operation : DEFAULT_OPERATION, rightOperand: isOperationExists ? operand.trimStart() : criteria.trimStart(), nullishValuesComparable: CRITERIA_OPERATORS_WITH_NULLISH_VALUE_CHECK.includes(operation), }; }; exports.parseCriteriaExpression = parseCriteriaExpression; const evalOperationParseResult = (parseResult, leftOperand) => { if (!parseResult.nullishValuesComparable && (leftOperand === null || leftOperand === undefined)) { return false; } const coercedRightOperand = (parseResult.disableCoercing || typeof leftOperand !== 'number' ? parseResult.rightOperand : coerceOperandToNumber(parseResult.rightOperand)); const typedLeftOperand = leftOperand; switch (parseResult.operator) { case '>=': { return typedLeftOperand >= coercedRightOperand; } case '<=': { return typedLeftOperand <= coercedRightOperand; } case '<>': { return leftOperand !== coercedRightOperand; } case '=': { return leftOperand === coercedRightOperand; } case '>': { return typedLeftOperand > coercedRightOperand; } case '<': { return typedLeftOperand < coercedRightOperand; } default: { return leftOperand === coercedRightOperand; } } }; const evalSystemCriteriaParseResult = (parseResult, leftOperand) => { switch (parseResult.systemCriteria) { case '#TRUE': { return leftOperand === true; } case '#FALSE': { return leftOperand === false; } case '#UNDEFINED': { return leftOperand === undefined; } case '#NULL': { return leftOperand === null; } case '#EMPTY': { return leftOperand === null || leftOperand === undefined || leftOperand === ''; } case '#NOT_EMPTY': { return leftOperand !== null && leftOperand !== undefined && leftOperand !== ''; } case '#BLANK': { return leftOperand === ''; } case '#NOT_BLANK': { return leftOperand !== ''; } default: { return false; } } }; const evalCriteriaParseResult = (parseResult, leftOperand) => { if (isSystemCriteriaParseResult(parseResult)) { return evalSystemCriteriaParseResult(parseResult, leftOperand); } return isSystemCriteriaParseResult(parseResult) ? evalSystemCriteriaParseResult(parseResult, leftOperand) : evalOperationParseResult(parseResult, leftOperand); }; exports.evalCriteriaParseResult = evalCriteriaParseResult;