tc39 / proposal-upsert

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

req: WeakMap.emplace #52

Closed runspired closed 1 year ago

runspired commented 1 year ago

I would love for this capability to exist with WeakMap's as well. I end up using this pattern quite often for both Map's and WeakMaps.

const Records = new WeakMap();

function getRecord(key) {
  let record = Records.get(key);
  if (!record) {
    record = new Record();
    Records.set(key, record);
  }

  return record;
}
LinusU commented 1 year ago

This proposal proposes to add emplace both to Map and WeakMap already :tada:

https://github.com/tc39/proposal-upsert/blob/d1daff560e3d53abd996099ee3da674374ea9226/spec.emu#L39-L58