UNPKG

@farris/expression-engine-vue

Version:

26 lines (25 loc) 1.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Compiler = void 0; const index_1 = require("../function/index"); class Compiler { compile(expr, context) { const contexts = context && context.contexts || {}; const args = contexts.arguments || {}; const signature = Object.keys(args).join(','); const defaultFunction = new index_1.DefaultFunctions(context); contexts.DefaultFunction = defaultFunction; const hasReturnStatement = expr.match(/([\s\r\n{;}]+return)|(^return)\s+/g); if (!hasReturnStatement) { expr = `return ${expr}`; } const scopeNames = Object.getOwnPropertyNames(contexts); const scopeVariable = `__scope__${new Date().valueOf()}`; return new Function(scopeVariable, ` ${scopeNames.map((key) => `var ${key} = ${scopeVariable}['${key}'];`).join('\r\n')} return function anonymous(${signature}) { try{ \n${expr}\n }catch(e){console.error(e);} };`); } } exports.Compiler = Compiler;