getOrInsert
Returns a map entry or stores the provided value when missing
84 bytes
since v12.7.0
Usage
Retrieves the current value for the key or inserts the provided value when the key does not exist.
import * as import _
_ from 'radashi'
const const counts: Map<string, number>
counts = new var Map: MapConstructornew <string, number>(iterable?: Iterable<readonly [string, number]> | null | undefined) => Map<string, number> (+3 overloads)
Map<string, number>()
import _
_.function getOrInsert<string, number>(map: Map<string, number>, key: string, value: number): number (+1 overload)
Returns a map entry or stores and returns the provided value when missing.
getOrInsert(const counts: Map<string, number>
counts, 'clicks', 1) // => 1import _
_.function getOrInsert<string, number>(map: Map<string, number>, key: string, value: number): number (+1 overload)
Returns a map entry or stores and returns the provided value when missing.
getOrInsert(const counts: Map<string, number>
counts, 'clicks', 5) // => 1Related Work
Inspired by TC39’s Upsert proposal.