Closed theScottyJam closed 3 years ago
That's been discussed previously, and is argued against in the README: https://github.com/tc39/proposal-upsert#why-not-have-an-api-that-exposes-an-entry-to-collection-types
Ah whoops, guess I didn't read the FAQ though. Thanks.
I moved the discussion over to the TC39 forms to see if we can get a new proposal started for it - see here.
This proposal seems to be trying to solve a whole lot of different types of problems with a single function. The thing I notice is that we've already solved all of these problems with objects - we've provided all sorts of useful operators and syntax, such as
??=
,++
, etc that just can't be used with maps. Perhaps there's a way to bring support of Javascript operators into maps.Inspired by React refs, I propose that instead of providing a swiss-army knife upsert() function, we instead provide the following getRef() function, that can be defined roughly as follows:
map.getRef(key)
will return a reference object to a specific map entry. That map entry can then be read or updated through the use of thevalue
getter/setter. Anexists()
function is also provided on the ref, to let you robustly check if the property actually exists on the map (as ref.value would give undefined if the value does not exist, or if it's been set to undefined).Some usage examples: