Skip to content

Commit

Permalink
Fix tinymath memoization
Browse files Browse the repository at this point in the history
  • Loading branch information
wylieconlon committed Jun 15, 2021
1 parent 4b65c74 commit 9d1b40d
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/kbn-tinymath/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ const memoizeOne = require('memoize-one');
const { parse: parseFn } = require('../grammar');
const { functions: includedFunctions } = require('./functions');

module.exports = { parse, evaluate, interpret };

function parse(input, options) {
if (input == null) {
throw new Error('Missing expression');
Expand All @@ -24,15 +22,17 @@ function parse(input, options) {
}

try {
return memoizeOne(parseFn)(input, options);
return parseFn(input, options);
} catch (e) {
throw new Error(`Failed to parse expression. ${e.message}`);
}
}

const memoizedParse = memoizeOne(parse);

function evaluate(expression, scope = {}, injectedFunctions = {}) {
scope = scope || {};
return interpret(parse(expression), scope, injectedFunctions);
return interpret(memoizedParse(expression), scope, injectedFunctions);
}

function interpret(node, scope, injectedFunctions) {
Expand Down Expand Up @@ -80,3 +80,5 @@ function isOperable(args) {
return typeof arg === 'number' && !isNaN(arg);
});
}

module.exports = { parse: memoizedParse, evaluate, interpret };

0 comments on commit 9d1b40d

Please sign in to comment.