mbloch/mapshaper

Template expressions and object literals

indus opened this issue · 3 comments

I'm facing a problem with this commands:

-require myFn.js
-run "myFn('foo',{optA:true,optB:false})" 

The curly braces of the options-property are interpreted as template identifiers and result in an error.

Maybe the template identifier should be changed to double curly braces in the parseTemplateParts function. It is not uncommon to use {{template}} but it would break existing code.

related: #608

Hi!

Originally, the -run command did not support embedded template expressions, so there was no ambiguity around curly braces. I went with single braces because the double-brace syntax felt clunky after working with single-brace frameworks like Svelte. You're using the original -run command syntax, which I'm supporting for backwards compatibility. You can work around the problem by enclosing your entire expression in curly braces, like this:

-require myFn.js
-run "{myFn('foo',{optA:true,optB:false})}" 

Maybe I should have gone with double-braces, but at least you have a workaround.

Thanks - this works.

I found a strange quirk:

// test.js
module.exports = {
    fnA: function (opt) {
        return "-each this.geojson=fnB()";
    },
    fnB: function () {
        return null;
    }
}
-require "./test.js" -run "fnA()" 

... works as expected

-require "./test.js" -run "{fnA()}" 

... results in an error SyntaxError in JS source: Unexpected token 'this'

I think this was why I avoided the template literal in the first place. But now I need (read "want" 😁 ) it to provide options as object and not as positional function parameters.

please tell me if I'm being a pain...
i can also get a huge amount of use out of mapshaper as it is