js-pick-notation icon indicating copy to clipboard operation
js-pick-notation copied to clipboard

Real-live example in LiveScript

Open pepkin88 opened this issue 4 years ago • 1 comments

The feature from this proposal is implemented in LiveScript. You could use it as a playground and/or inspiration on how to handle the edge cases.

Note that the LiveScript implementation of this feature is a little bit different, than the proposed one. In your example obj.{p1: new_p1, p2: {p21}, p3 = 3}, new_p1 is the property name in the newly created object. But in the LS implementation it would be p1.

I believe this could be the reasoning behind such order in the LS implementation:

obj2 = obj.{new_p1: p1, p2}
# being equivalent to
{p1, p2} = obj
obj2 = {new_p1: p1, p2}
# or
obj2 = {new_p1: obj.p1, obj.p2}
# in each case `new_p1` is on the left side of the colon

When setting multiple properties on an object, the order is consistent with object destructuring (which work similar as in JS). In the LS docs this operation is called substructuring:

dest.{a: c, b} = src
# equivalent to
{a: c, b} = src
dest.{c, b} = {c, b}
# which is equivalent to
dest.c = src.a
dest.b = src.b

pepkin88 avatar Jun 15 '21 16:06 pepkin88

You could also consider using the keyword as, just like with exports:

export { import1 as name1, import2 as name2, …, nameN } from …;

I think with it it would be the least confusing, as to what is the new property name.

pepkin88 avatar Jun 15 '21 16:06 pepkin88