Proposal: minus operator for the spread operator

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Proposal: minus operator for the spread operator

devlato
Hey folks,
 
I've figured out a potential improvement for the object spread syntax and want to share my thoughts.
I've drafted my thoughts into a proposal available here: https://github.com/devlato/proposal-plus-minus-spread
 
Long story short, I want to propose a new **minus operator**, that would allow specify keys to remove the values from a result of object spread. It would look like this:
 
const result = {
  ...objA,
  ...objB,
  -keyToRemove,
};

// Or like this – the same result!
const result = {
  ...objA,
  -keyToRemove,
  ...objB,
};

// And this!
const result = {
  -keyToRemove,
  ...objA,
  ...objB,
};
 
What do you folks think?
 
Warmest regards,
Denis
 

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: minus operator for the spread operator

Felipe Nascimento de Moura
hm, personally, I find it useful when extracting properties from objects!

I have myself been in situations where I had to deal with "all other properties except...".
An alternative would be simply extract it normally, but ignore it (which triggers complaints from linters).

Perhaps `!` could be a better option (makes me think of .gitignore and other list remove syntaxes.

const { propA, propB, !keyToRemove, ...other } = objectA;

Note that, if you are not getting the "rest" of the properties in an `...other` constant, it would not be as useful.
In this case, keyToRemove would NOT be declared, and other would have all properties except propA, propB and keyToRemove.

Other thing I find rather annoying is the way we have to deal with some APIs when we are trying to "clean" their returned objects.
Like so:


function cleanData (data) {
const { propA, propB, propC, propD, propE } = data;
return { propA, propB, propC, propD, propE };
}

Perhaps something like this could become an option?!


function cleanData (data) {
const { propA, propB, propC, propD, propE } as newObj = data;
return newObj;
}




[ ]s

--

Felipe N. Moura
Web Developer, Google Developer ExpertFounder of BrazilJSNasc and On2.

Website:  http://felipenmoura.com / http://nasc.io/ 
Twitter:    @felipenmoura
---------------------------------
 Changing  the  world  is the least I expect from  myself! 


On Sun, May 3, 2020 at 6:57 AM devlato <[hidden email]> wrote:
Hey folks,
 
I've figured out a potential improvement for the object spread syntax and want to share my thoughts.
I've drafted my thoughts into a proposal available here: https://github.com/devlato/proposal-plus-minus-spread
 
Long story short, I want to propose a new **minus operator**, that would allow specify keys to remove the values from a result of object spread. It would look like this:
 
const result = {
  ...objA,
  ...objB,
  -keyToRemove,
};

// Or like this – the same result!
const result = {
  ...objA,
  -keyToRemove,
  ...objB,
};

// And this!
const result = {
  -keyToRemove,
  ...objA,
  ...objB,
};
 
What do you folks think?
 
Warmest regards,
Denis
 
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss