UNPKG

@qrvey/formula-lang

Version:

QFormula support for qrvey projects

87 lines 3.37 kB
import { AST_PRIMITIVES } from '../constants'; import { ERROR_DICTIONARY, ERROR_LIST } from '../errors/dictionary'; import { isBooleanExpression } from '../utils/isBooleanExpression'; import { inferPrimitive, isSinglePrimitive } from '../utils/primitiveFunctions'; /** * `IF` Returns one value if a logical expression is TRUE and another if it is FALSE. */ export const IF = { identifier: 'IF', parameters: [ { identifier: 'LOGICAL_EXPRESSION', optional: false, expectedPrimitive: AST_PRIMITIVES.BOOLEAN, validator: [isBooleanExpression], }, { identifier: 'VALUE_IF_TRUE', optional: false, generic: true, validator: [samePrimitiveType], }, { identifier: 'VALUE_IF_FALSE', optional: false, generic: true, validator: [samePrimitiveType], }, ], transpiler: { elasticsearch, snowflake, redshift, postgresql, }, primitiveResult(args) { var _a, _b, _c, _d; const truePrimitive = (_b = (_a = args[1]) === null || _a === void 0 ? void 0 : _a.primitive) !== null && _b !== void 0 ? _b : AST_PRIMITIVES.UNKNOWN; const falsePrimitive = (_d = (_c = args[2]) === null || _c === void 0 ? void 0 : _c.primitive) !== null && _d !== void 0 ? _d : null; if (!falsePrimitive) return truePrimitive; return inferPrimitive(truePrimitive, falsePrimitive); }, }; function samePrimitiveType(_param, _dataType, context) { var _a, _b; const fPrimitive = (_a = context.fnNode.arguments[1]) === null || _a === void 0 ? void 0 : _a.primitive; const firstPrimitive = fPrimitive && isSinglePrimitive(fPrimitive) ? fPrimitive : AST_PRIMITIVES.UNKNOWN; const sPrimitive = (_b = context.fnNode.arguments[2]) === null || _b === void 0 ? void 0 : _b.primitive; const secondPrimitive = sPrimitive && isSinglePrimitive(sPrimitive) ? sPrimitive : AST_PRIMITIVES.UNKNOWN; const ifPrimitive = calculateIfPrimitive(firstPrimitive, secondPrimitive); const valid = firstPrimitive === secondPrimitive; if (!valid) return Object.assign(Object.assign({ valid }, ERROR_DICTIONARY[ERROR_LIST.inferredPrimitive]), { mismatchData: { primitive: ifPrimitive, } }); return { valid }; } function calculateIfPrimitive(firstPrimitive, secondPrimitive) { if (firstPrimitive !== AST_PRIMITIVES.UNKNOWN) return firstPrimitive; return secondPrimitive; } function elasticsearch(logicalExpression, valueIfTrue, valueIfFalse) { return `(${logicalExpression} ? ${valueIfTrue} : ${valueIfFalse})`; } function snowflake(logicalExpression, valueIfTrue, valueIfFalse) { return `IFF(${logicalExpression}, ${valueIfTrue}, ${valueIfFalse})`; } function sql(logicalExpression, valueIfTrue, valueIfFalse) { return ` (CASE WHEN ${logicalExpression} THEN ${valueIfTrue} ELSE ${valueIfFalse} END)`.trim(); } function redshift(logicalExpression, valueIfTrue, valueIfFalse) { return sql(logicalExpression, valueIfTrue, valueIfFalse); } function postgresql(logicalExpression, valueIfTrue, valueIfFalse) { return sql(logicalExpression, valueIfTrue, valueIfFalse); } //# sourceMappingURL=if.js.map