/** * A value that {@link Map.get} can distinguish from a missing entry. `undefined` * is excluded because these helpers use `get() !== undefined` to detect absence. */ type Defined = {} | null; /** * A {@link Map} or a {@link WeakMap}. The ES2026 proposal adds `getOrInsert` * and `getOrInsertComputed` to both. */ type MapOrWeakMap = Map | WeakMap; function hasNative(name: keyof MapES2026): boolean { return ( typeof (Map.prototype as unknown as MapES2026)[name] === 'function' && typeof (WeakMap.prototype as unknown as MapES2026)[ name ] === 'function' ); } interface MapES2026 { getOrInsert(key: K, defaultValue: V): V; getOrInsertComputed(key: K, compute: (key: K) => V): V; } /** * Returns the value for {@link key} in {@link map}. If no mapping exists, * inserts {@link defaultValue} and returns it. * * Mirrors the ES2026 `Map.prototype.getOrInsert` proposal. */ function getOrInsertPolyfill( map: MapOrWeakMap, key: K, defaultValue: NoInfer, ): V { const existing = map.get(key as K & WeakKey); if (existing !== undefined) { return existing; } map.set(key as K & WeakKey, defaultValue); return defaultValue; } /** * Returns the value for {@link key} in {@link map}. If no mapping exists, * calls {@link compute} with the key, inserts the result, and returns it. * * Mirrors the ES2026 `Map.prototype.getOrInsertComputed` proposal. */ function getOrInsertComputedPolyfill( map: MapOrWeakMap, key: K, compute: (key: K) => NoInfer, ): V { const existing = map.get(key as K & WeakKey); if (existing !== undefined) { return existing; } const value = compute(key); map.set(key as K & WeakKey, value); return value; } function getOrInsertNative( map: MapOrWeakMap, key: K, defaultValue: NoInfer, ): V { return (map as unknown as MapES2026).getOrInsert(key, defaultValue); } function getOrInsertComputedNative( map: MapOrWeakMap, key: K, compute: (key: K) => NoInfer, ): V { return (map as unknown as MapES2026).getOrInsertComputed(key, compute); } export const getOrInsert = hasNative('getOrInsert') ? getOrInsertNative : getOrInsertPolyfill; export const getOrInsertComputed = hasNative('getOrInsertComputed') ? getOrInsertComputedNative : getOrInsertComputedPolyfill; /** * Creates an empty {@link Map}. Pass this to {@link getOrInsertComputed} * instead of an inline `() => new Map()` so that a hit does not allocate a * closure. */ export const newMap = (): Map => new Map(); /** * Creates an empty {@link WeakMap}. Pass this to {@link getOrInsertComputed} * instead of an inline `() => new WeakMap()` so that a hit does not allocate a * closure. */ export const newWeakMap = (): WeakMap => new WeakMap();