diff --git a/src/jsep-eval.js b/src/jsep-eval.js index eb48f66..2ef63a3 100644 --- a/src/jsep-eval.js +++ b/src/jsep-eval.js @@ -2,7 +2,9 @@ // evaluates javascript expression statements parsed with jsep // -const _ = require('lodash'); +const get = require('lodash/get'); +const map = require('lodash/map'); +const includes = require('lodash/includes'); const jsep = require('jsep'); const assert = require('assert'); @@ -61,8 +63,8 @@ const undefOperator = () => undefined; const getParameterPath = (node, context) => { assert(node, 'Node missing'); const type = node.type; - assert(_.includes(types, type), 'invalid node type'); - assert(_.includes([types.MEMBER, types.IDENTIFIER], type), 'Invalid parameter path node type: ', type); + assert(includes(types, type), 'invalid node type'); + assert(includes([types.MEMBER, types.IDENTIFIER], type), 'Invalid parameter path node type: ', type); // the easy case: 'IDENTIFIER's if (type === types.IDENTIFIER) { return node.name; @@ -74,7 +76,7 @@ const getParameterPath = (node, context) => { const object = node.object; const property = node.property; // object is either 'IDENTIFIER', 'MEMBER', or 'THIS' - assert(_.includes([types.MEMBER, types.IDENTIFIER, types.THIS], object.type), 'Invalid object type'); + assert(includes([types.MEMBER, types.IDENTIFIER, types.THIS], object.type), 'Invalid object type'); assert(property, 'Member expression property is missing'); let objectPath = ''; @@ -89,7 +91,7 @@ const getParameterPath = (node, context) => { const propertyPath = evaluateExpressionNode(property, context); return objectPath + '[' + propertyPath + ']'; } else { - assert(_.includes([types.MEMBER, types.IDENTIFIER], property.type), 'Invalid object type'); + assert(includes([types.MEMBER, types.IDENTIFIER], property.type), 'Invalid object type'); const propertyPath = property.name || getParameterPath(property, context); return (objectPath ? objectPath + '.': '') + propertyPath; } @@ -97,7 +99,7 @@ const getParameterPath = (node, context) => { const evaluateExpressionNode = (node, context) => { assert(node, 'Node missing'); - assert(_.includes(types, node.type), 'invalid node type'); + assert(includes(types, node.type), 'invalid node type'); switch (node.type) { case types.LITERAL: { return node.value; @@ -106,23 +108,23 @@ const evaluateExpressionNode = (node, context) => { return context; } case types.COMPOUND: { - const expressions = _.map(node.body, el => evaluateExpressionNode(el, context)); + const expressions = map(node.body, el => evaluateExpressionNode(el, context)); return expressions.pop(); } case types.ARRAY: { - const elements = _.map(node.elements, el => evaluateExpressionNode(el, context)); + const elements = map(node.elements, el => evaluateExpressionNode(el, context)); return elements; } case types.UNARY: { const operator = operators.unary[node.operator] || undefOperator; - assert(_.includes(operators.unary, operator), 'Invalid unary operator'); + assert(includes(operators.unary, operator), 'Invalid unary operator'); const argument = evaluateExpressionNode(node.argument, context); return operator(argument); } case types.LOGICAL: // !!! fall-through to BINARY !!! // case types.BINARY: { const operator = operators.binary[node.operator] || undefOperator; - assert(_.includes(operators.binary, operator), 'Invalid binary operator'); + assert(includes(operators.binary, operator), 'Invalid binary operator'); const left = evaluateExpressionNode(node.left, context); const right = evaluateExpressionNode(node.right, context); return operator(left, right); @@ -134,15 +136,15 @@ const evaluateExpressionNode = (node, context) => { return test ? consequent : alternate; } case types.CALL : { - assert(_.includes([types.MEMBER, types.IDENTIFIER, types.THIS], node.callee.type), 'Invalid function callee type'); + assert(includes([types.MEMBER, types.IDENTIFIER, types.THIS], node.callee.type), 'Invalid function callee type'); const callee = evaluateExpressionNode(node.callee, context); - const args = _.map(node.arguments, arg => evaluateExpressionNode(arg, context)); + const args = map(node.arguments, arg => evaluateExpressionNode(arg, context)); return callee.apply(null, args); } case types.IDENTIFIER: // !!! fall-through to MEMBER !!! // case types.MEMBER: { const path = getParameterPath(node, context); - return _.get(context, path); + return get(context, path); } default: return undefined;