twing
Version:
First-class Twig engine for Node.js
46 lines (45 loc) • 1.95 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeIfNodeSynchronously = exports.executeIfNode = void 0;
const node_1 = require("../node");
const evaluate_1 = require("../helpers/evaluate");
const executeIfNode = async (node, executionContext) => {
const { tests: testsNode, else: elseNode } = node.children;
const count = (0, node_1.getChildrenCount)(testsNode);
const { nodeExecutor: execute } = executionContext;
let index = 0;
while (index < count) {
const condition = testsNode.children[index];
const conditionResult = await execute(condition, executionContext);
if ((0, evaluate_1.evaluate)(conditionResult)) {
// the condition is satisfied, we execute the belonging body and return the result
const body = testsNode.children[index + 1];
return execute(body, executionContext);
}
index += 2;
}
if (elseNode !== undefined) {
return execute(elseNode, executionContext);
}
};
exports.executeIfNode = executeIfNode;
const executeIfNodeSynchronously = (node, executionContext) => {
const { tests: testsNode, else: elseNode } = node.children;
const count = (0, node_1.getChildrenCount)(testsNode);
const { nodeExecutor: execute } = executionContext;
let index = 0;
while (index < count) {
const condition = testsNode.children[index];
const conditionResult = execute(condition, executionContext);
if ((0, evaluate_1.evaluate)(conditionResult)) {
// the condition is satisfied, we execute the belonging body and return the result
const body = testsNode.children[index + 1];
return execute(body, executionContext);
}
index += 2;
}
if (elseNode !== undefined) {
return execute(elseNode, executionContext);
}
};
exports.executeIfNodeSynchronously = executeIfNodeSynchronously;