Workaround for missing ES6 feature: default values for function parameters (combined with object destructuring) #4509
-
I want more powerful Code Template Library functions having optional "keyword" arguments (inspired by Python's **kwargs). In ES6 you can achieve that simply by combining object destructuring and default values as follows: function foo(bar, {baz}={}) {
console.log(bar); // to be replaced by logger.debug(bar) in Mirth
console.log(baz);
}
foo("spam", {baz: "eggs"}); // logs "spam" and "eggs"
foo("spam"); // logs "spam" and undefined
foo(); // logs undefined and undefined Also see: https://javascript.info/destructuring-assignment Limitation in Mirth (Rhino ES6 or version=200): object destructuring works but default values don't. So in Mirth, a working code is limited to: function foo(bar, {baz}) { // noticed the missing default here?
logger.debug(bar);
logger.debug(baz);
}
foo("spam", {baz: "eggs"}); // logs "spam" and "eggs"
foo("spam", {}); // logs "spam" and undefined
foo("spam"); // fails
foo(); // fails The reason why the last 2 function calls fail is that JavaScript tries to destructure the 2nd parameter but it's Still, I want this power because it brings so much value... and everything is possible! What I want to avoid: submitting an empty object as last parameter explicitly. To you, Mirth pro's and developers: How to achieve that? What's the best approach? Extend and replace the Rhino .jar or is it easier to "fix" that limitation in Mirth or even by using JavaScript itself (e.g. in Mirth's GlobalScripts)? Any ideas appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
The only way to fix unsupported syntax would be to fix it in Rhino itself. The maintainers are happy to accept PRs for missing functionality. This is not as elegant, but javascript existed for a long time without destructuring assignments and function parameters with defaults. Your function here: function foo(bar, {baz}={}) {
console.log(bar); // to be replaced by logger.debug(bar) in Mirth
console.log(baz);
} Becomes the following when transpiled by Babel if you want to use the same calling syntax: function foo(bar) {
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
baz = _ref.baz;
logger.debug(bar);
logger.debug(baz);
} |
Beta Was this translation helpful? Give feedback.
-
Great, it's even easier: function foo(bar, baz) {
let {spam, eggs} = baz || {};
logger.debug([bar, String(spam), String(eggs)].join(", "));
}
foo("bar", {spam: "spam", eggs: "eggs"}); // logs "bar, spam, eggs"
foo("bar", {spam: "spam"}); // logs "bar, spam, undefined"
foo("bar", {eggs: "eggs"}); // logs "bar, undefined, eggs"
foo("bar", {foo: "foo"}); // logs "bar, undefined, undefined"
foo("bar"); // logs "bar, undefined, undefined" |
Beta Was this translation helpful? Give feedback.
-
Thanks for the advice. And funny, for some reason it's no problem in Rhino and no error is thrown: /*
// Use this helper if running in Rhino
var logger={debug:message => java.lang.System.out.println(message)};
// Use this helper if running in JavaScript console
var logger={debug:message => console.log(message)};
*/
var {spam, eggs} = undefined || {}; logger.debug([spam,eggs].join(","));
var {spam, eggs} = null || {}; logger.debug([spam,eggs].join(","));
var {spam, eggs} = 1 || {}; logger.debug([spam,eggs].join(","));
var {spam, eggs} = true || {}; logger.debug([spam,eggs].join(","));
var {spam, eggs} = "string" || {}; logger.debug([spam,eggs].join(","));
var {spam, eggs} = ["array"] || {}; logger.debug([spam,eggs].join(",")); All of them log a comma and no error is thrown! I also tested in the browser and it works, see it live in action here: https://js.do/code/es6_destruct_non-object |
Beta Was this translation helpful? Give feedback.
The only way to fix unsupported syntax would be to fix it in Rhino itself. The maintainers are happy to accept PRs for missing functionality.
This is not as elegant, but javascript existed for a long time without destructuring assignments and function parameters with defaults. Your function here:
Becomes the following when transpiled by Babel if you want to use the same calling syntax: