UNPKG

serverless-artillery

Version:

A serverless performance testing tool. `serverless` + `artillery` = crush. a.k.a. Orbital Laziers [sic]

52 lines (47 loc) 1.28 kB
/** * Filtrex provides compileExpression() to compile user expressions to JavaScript. * * See https://github.com/joewalnes/filtrex for tutorial, reference and examples. * MIT License. * * Includes Jison by Zachary Carter. See http://jison.org/ * * -Joe Walnes */ module.exports = function compileExpression(expression, extraFunctions /* optional */) { var parser = require('./parser'); var functions = { abs: Math.abs, ceil: Math.ceil, floor: Math.floor, log: Math.log, max: Math.max, min: Math.min, random: Math.random, round: Math.round, sqrt: Math.sqrt, }; if (extraFunctions) { for (var name in extraFunctions) { if (extraFunctions.hasOwnProperty(name)) { functions[name] = extraFunctions[name]; } } } var tree = parser.parse(expression); var js = []; js.push('return '); function toJs(node) { if (Array.isArray(node)) { node.forEach(toJs); } else { js.push(node); } } tree.forEach(toJs); js.push(';'); var func = new Function('functions', 'data', js.join('')); return function(data) { return func(functions, data); }; }