Skip to content

Commit

Permalink
switch to expr-eval for dynamic module (#1729)
Browse files Browse the repository at this point in the history
* switch to expr-eval for dynamic module

* added expr-eval module

* Update Module.js

* update options test to correctly label varied options

* Update options-test.js

* make expressions/options case insensitive

* Update Module.js

Co-authored-by: Harsh Khandeparkar <34770591+HarshKhandeparkar@users.noreply.github.com>
  • Loading branch information
jywarren and harshkhandeparkar authored Feb 15, 2021
1 parent 0bdf71a commit 4609d0a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"data-uri-to-buffer": "^3.0.0",
"downloadjs": "^1.4.7",
"eslint": "^6.1.0",
"expr-eval": "^2.0.2",
"fisheyegl": "^0.1.2",
"font-awesome": "~4.7.0",
"geotiff": "^1.0.0-beta.6",
Expand Down
12 changes: 5 additions & 7 deletions src/modules/Dynamic/Module.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,18 @@ module.exports = function Dynamic(options, UI) {
options.blue = options.blue || defaults.blue;
options.green = options.green || defaults.green;

const Parser = require('expr-eval').Parser;
function generator(expression) {
var func = 'f = function (r, g, b, a) { var R = r, G = g, B = b, A = a; return ' + expression + ';}';
var f;
eval(func);
return f;
let expr = Parser.parse('R = r; G = g; B = b; A = a; ' + expression);
return expr.toJSFunction("r,g,b,a,R,G,B,A");
}

var channels = ['red', 'green', 'blue', 'alpha'];

channels.forEach(function(channel) {
if (channel === 'alpha'){
options['alpha_function'] = function() { return 255; };
}
else{
} else {
options[channel + '_function'] = generator(options[channel]);
}
});
Expand Down
4 changes: 2 additions & 2 deletions test/core/templates/options-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = (moduleName, options, benchmark, input) => {
looksSame(result, benchmark[0], function(err, res) {
if (err) console.log(err);

t.equal(res.equal, true, `${moduleName} module works correctly with initial option ${options[0][moduleName]}`);
t.equal(res.equal, true, `${moduleName} module works correctly with initial option ${JSON.stringify(options[0])}`);
});
// Change the option of the given module.
sequencer.steps[1].setOptions(options[1]);
Expand All @@ -54,7 +54,7 @@ module.exports = (moduleName, options, benchmark, input) => {
looksSame(newResult, benchmark[1], function(err, res) {
if (err) console.log(err);

t.equal(res.equal, true, `${moduleName} module works correctly when the option is changed to ${options[1][moduleName]}`);
t.equal(res.equal, true, `${moduleName} module works correctly when the option is changed to ${JSON.stringify(options[1])}`);
sequencer = null;
t.end();
});
Expand Down

0 comments on commit 4609d0a

Please sign in to comment.