twing
Version:
First-class Twig engine for Node.js
37 lines (36 loc) • 1.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFunction = void 0;
/**
* Get a function by name.
*
* @param {string} name function name
* @returns {TwingFunction} A TwingFunction instance or null if the function does not exist
*/
const getFunction = (functions, name) => {
const result = functions.get(name);
if (result) {
return result;
}
for (let [pattern, twingFunction] of functions) {
let count = 0;
pattern = pattern.replace(/\*/g, function () {
count++;
return '(.*?)';
});
if (count) {
const regExp = new RegExp('^' + pattern + '$', 'g');
const match = regExp.exec(name);
const matches = [];
if (match) {
for (let i = 1; i <= count; i++) {
matches.push(match[i]);
}
twingFunction.nativeArguments = matches;
return twingFunction;
}
}
}
return null;
};
exports.getFunction = getFunction;