tc39 / proposal-upsert

ECMAScript Proposal, specs, and reference implementation for Map.prototype.upsert
https://tc39.es/proposal-upsert/
MIT License
204 stars 14 forks source link

Additional examples to single update function with upsert functionality #54

Closed malthe closed 1 month ago

malthe commented 1 year ago

Under "Why not have a single function that updates the value if no if the key is mapped?", instead of using a try/finally clause, we could write:

counts.emplace(key, (existing) => existing ?? 0 + 1)

Factory with just insertion:

counts.emplace(key, (existing) => existing ?? someFactory());

Insertion and update, assuming the created value here is always truthy, a reasonable assumption.

counts.emplace(key, (existing) => existing && doSomething(existing) || someFactory())

I don't think these examples are adequately presented in the proposal.

dminor commented 1 month ago

The new direction for the proposal is only looking at the insert use case, not updates.