4
\$\begingroup\$

How can I refactor this into one line? Is that possible, even with ES6 syntax?

export const zeroBalance = (coin) => {
    coin.balance = 0;
    return coin;
};
\$\endgroup\$
  • 1
    \$\begingroup\$ Do you need to mutate the object? \$\endgroup\$ – xehpuk Jul 3 '17 at 9:24
  • \$\begingroup\$ @xehpuk yes, though I guess I could make a new object from both my local search coins object and the remote api coins. In my case above, I use the local search modal just for the search, once a full coin comes back from the api, there is no balance key for my UI, so I add it. \$\endgroup\$ – Leon Gaban Jul 5 '17 at 12:19
  • 1
    \$\begingroup\$ uh.. remove the linebreaks..? \$\endgroup\$ – I wrestled a bear once. Jul 5 '17 at 15:11
2
\$\begingroup\$

How can I refactor this into one line? Is that possible, even with ES6 syntax?

export const zeroBalance = (coin) => {
  coin.balance = 0;
  return coin;
};

Yes, that's easily possible. Newlines aren't required in ECMAScript:

export const zeroBalance = coin => { coin.balance = 0; return coin; };

Ruby has an implementation of the K Combinator as Object#tap for exactly that use case. ECMAScript doesn't, but it's easy to implement:

 function k(obj, f) { f(obj); return obj };

This would allow you to rewrite your code like this:

export const zeroBalance = coin => k(coin, c => c.balance = 0);
\$\endgroup\$
  • \$\begingroup\$ Yeah I guess this is the right answer, I asked the original question incorrectly. This is the minified view, was hoping there would be a clean sexy way to do it, but thanks :) \$\endgroup\$ – Leon Gaban Jul 2 '17 at 23:31
  • 1
    \$\begingroup\$ See the update. \$\endgroup\$ – Jörg W Mittag Jul 2 '17 at 23:52
2
\$\begingroup\$

You can use the comma operator, which takes the value of the last expression. It's not used very much, so may make the code less readable, but if you wanna make it one line, this is the way to do it in a single statement. You need parentheses because of operator presedence. If you omitted them, the comma would be interpreted as a separator between const variable definitions.

export const zeroBalance = (coin) => (coin.balance = 0, coin);
\$\endgroup\$
  • \$\begingroup\$ Uh, nice, I totally forgot about that. \$\endgroup\$ – Jörg W Mittag Jul 3 '17 at 10:40
2
\$\begingroup\$

As it's currently written, this function creates a pitfall: it modifies supplied object and returns the result. I suggest to modify it to be a pure function (it can be a one-liner):

export const zeroBalance = coin => Object.assign({}, coin, { balance: 0 });
\$\endgroup\$
  • 2
    \$\begingroup\$ You can alternatively use the spread operator: coin => ({ ...coin, balance: 0 }). Adding an example of how the function would be used could be useful (updatedCoin = zeroBalance(coin) vs zeroBalance(coin); coin // now has zero balance) and some explanation about why a pure function might help (debugging, testing, etc) \$\endgroup\$ – Craig Ayre Jul 3 '17 at 9:55
  • \$\begingroup\$ @CraigAyre please don't push unfinilazed ES proposals, OP specifically said 'in ES6'; agree with the rest of your comment, though. \$\endgroup\$ – Pavlo Jul 4 '17 at 10:16
1
\$\begingroup\$

You could make balance be a function on the coin that modifies the value and return this, then you could do coin.balance(0). (Then again, you just move the two lines from one place to another).

Other than that, there it is not possible to my knowledge. You can't change a property on the object and return the object at the same line.

\$\endgroup\$
1
\$\begingroup\$

Don't.

There is no advantage to making this function single line. You have two distinct operations, and keeping them separate makes the function much clearer. You gain nothing by making it single line, and you lose readability.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.