How to override a methods parameter defaults, without wrap (since it's about to be removed). Consider the following.
let foo = (bar, { logToConsole = false }) => {
if (logToConsole) console.log(bar);
return bar;
};
// I want foo to always log, but I want to leave the option open to
// override that on a per instance basis (which excludes doing a
// `R.partial`). Looking at the methods available, `wrap` seems to
// do what I want...
foo = R.wrap(foo, (fn, bar, options) => {
options = R.merge({ logToConsole: true }, options);
return fn(bar, options);
}
So, reassigning foo, with the default flipped. Every time I call foo
after, it will have the default changed.
Since wrap
is deprecated, what should I be using instead?
I followed along this thread, but didn't see how to solve this pattern with the examples there.
Thanks for the help!
That discussion involved David showing that every use I would come up with for wrap could be rewritten more simply, especially with ES6 handy.
I'm not sure whether he's right, but it's not far off. I've been using something like
wrap
for years, doing AOP-style work in JS, and I still do, just not using it inside Ramda. Whether they all can be rewritten in a different manner, writing them all consistently is useful to me, but David's point was a good one.Your case can, I believe, be rewritten pretty straightforwardly:
(I'm assuming that
command
in the original is supposed to bebar
. Is that correct?)Update
If you're looking to reassign
foo
, you can wrap that one up like this:This would have the same behavior as
foo2
above. (Obviously it won't work with yourconst
declaration offoo
.)