rc-js-util
Version:
A collection of TS and C++ utilities to help writing performant and correct applications, achieved through strict typing and (removable) invariant checking.
25 lines (24 loc) • 625 B
text/typescript
/**
* @public
* Get the stored value if present, then delete the key.
*
* @remarks
* See {@link (mapDeleteGet: 1)}.
*/
export function mapDeleteGet<TKey, TValue>(map: Map<TKey, TValue>, key: TKey): TValue | undefined;
/**
* @public
* {@inheritDoc (mapDeleteGet: 1)}
*/
export function mapDeleteGet<TKey extends object, TValue>(map: WeakMap<TKey, TValue>, key: TKey): TValue | undefined;
export function mapDeleteGet<TKey extends object, TValue>
(
map: Map<TKey, TValue> | WeakMap<TKey, TValue>,
key: TKey,
)
: TValue | undefined
{
const value = map.get(key);
map.delete(key);
return value;
}