UNPKG

ts-flex-query

Version:
58 lines 2.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.letIfDefined = letIfDefined; exports.unwrapLetIfDefined = unwrapLetIfDefined; const lodash_1 = require("lodash"); const expressions_1 = require("../../expressions"); const helpers_1 = require("../../helpers"); const object_value_selector_1 = require("../../helpers/object-value-selector"); const if_1 = require("../basic/if"); const func_1 = require("./func"); const let_1 = require("./let"); const no_op_1 = require("./no-op"); const value_1 = require("./value"); const nullCheckFunction = () => (0, func_1.func)('in', (0, no_op_1.noOp)(), (0, value_1.value)([null, undefined])); /** Applies the given selector if the input is defined. Otherwise, the input value (null or undefined) is propagated. */ function letIfDefined(selector) { return (0, let_1.letIn)((0, if_1.ifThenElse)(nullCheckFunction(), (0, no_op_1.noOp)(), (0, object_value_selector_1.createQueryFromObjectValueSelector)(selector))); } /** Unwraps an expression which was created using the @see letIfDefined operator. */ function unwrapLetIfDefinedInternal(expression) { let inputVariable; let baseExpression; let ifExpression; if (expression instanceof expressions_1.LetExpression) { inputVariable = new expressions_1.VariableExpression(expression.input.dataType, expression.variableSymbol); baseExpression = expression.input; if (!(expression.body instanceof expressions_1.IfExpression)) { return undefined; } ifExpression = expression.body; } else if (expression instanceof expressions_1.IfExpression) { ifExpression = expression; if (!(expression.thenExpression instanceof expressions_1.VariableExpression)) { return undefined; } inputVariable = expression.thenExpression; baseExpression = inputVariable; } else { return undefined; } const conditionFunction = ifExpression.condition; const expectedFunction = (0, helpers_1.pipeExpression)(inputVariable, nullCheckFunction()); if (!(0, lodash_1.isEqual)(conditionFunction, expectedFunction) || !(0, lodash_1.isEqual)(ifExpression.thenExpression, inputVariable)) { return undefined; } return { baseExpression, baseExpressionVariableSymbol: inputVariable.symbol, body: ifExpression.elseExpression }; } /** Unwraps an expression which was created using the @see letIfDefined operator. */ function unwrapLetIfDefined(expression) { return unwrapLetIfDefinedInternal(expression); } //# sourceMappingURL=let-if-defined.js.map