js-util

Home > rc-js-util > mapInitializeGet

mapInitializeGet() function

Gets the value from a Map for a given key, where the value is undefined or hasn’t been set, the callback’s return will be inserted into the Map and returned.

Signature:

export declare function mapInitializeGet<TMapKey extends object, TKey extends TMapKey, TMapValue, TValue extends TMapValue>(map: WeakMap<TMapKey, TMapValue>, key: TKey, getValue: (key: TKey) => TValue): TMapValue;

Parameters

Parameter Type Description
map WeakMap<TMapKey, TMapValue> The Map to search and modify.
key TKey The key to search for / set in map.
getValue (key: TKey) => TValue The function to call if a value is not found.

Returns:

TMapValue

The value in map, otherwise the result of the getValue.

Remarks

See mapInitializeGet().