Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch to expr-eval for dynamic module #1729

Merged
merged 12 commits into from
Feb 15, 2021
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