twing
Version:
First-class Twig engine for Node.js
86 lines (85 loc) • 2.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getContextValueSynchronously = exports.getContextValue = void 0;
const getContextValue = (charset, templateName, isStrictVariables, context, name, isAlwaysDefined, shouldIgnoreStrictCheck, shouldTestExistence) => {
const specialNames = new Map([
['_self', templateName],
['_context', context],
['_charset', charset]
]);
const isSpecial = () => {
return specialNames.has(name);
};
let result;
if (shouldTestExistence) {
if (isSpecial()) {
result = true;
}
else {
result = context.has(name);
}
}
else if (isSpecial()) {
result = specialNames.get(name);
}
else if (isAlwaysDefined) {
result = context.get(name);
}
else {
if (shouldIgnoreStrictCheck || !isStrictVariables) {
result = context.has(name) ? context.get(name) : null;
}
else {
result = context.get(name);
if (result === undefined) {
return Promise.reject(new Error(`Variable "${name}" does not exist.`));
}
}
}
return Promise.resolve(result);
};
exports.getContextValue = getContextValue;
const getContextValueSynchronously = (charset, templateName, isStrictVariables, context, globals, name, isAlwaysDefined, shouldIgnoreStrictCheck, shouldTestExistence) => {
const specialNames = new Map([
['_self', templateName],
['_context', context],
['_charset', charset]
]);
const isSpecial = () => {
return specialNames.has(name);
};
let result;
if (shouldTestExistence) {
if (isSpecial()) {
result = true;
}
else {
result = context.has(name) || globals.has(name);
}
}
else if (isSpecial()) {
result = specialNames.get(name);
}
else if (isAlwaysDefined) {
result = context.get(name);
if (result === undefined) {
result = globals.get(name);
}
}
else {
if (shouldIgnoreStrictCheck || !isStrictVariables) {
result = context.has(name) ? context.get(name) : (globals.has(name) ? globals.get(name) : null);
}
else {
result = context.get(name);
if (result === undefined) {
result = globals.get(name);
}
if (result === undefined) {
throw new Error(`Variable "${name}" does not exist.`);
}
}
}
return result;
};
exports.getContextValueSynchronously = getContextValueSynchronously;