Skip to content

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: MapConstructor
new <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.

@seehttps://radashi.js.org/reference/object/getOrInsert

@example

const counts = new Map<string, number>()
getOrInsert(counts, 'clicks', 1)
getOrInsert(counts, 'clicks', 5)
// => 1

@version12.7.0

getOrInsert
(
const counts: Map<string, number>
counts
, 'clicks', 1) // => 1
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.

@seehttps://radashi.js.org/reference/object/getOrInsert

@example

const counts = new Map<string, number>()
getOrInsert(counts, 'clicks', 1)
getOrInsert(counts, 'clicks', 5)
// => 1

@version12.7.0

getOrInsert
(
const counts: Map<string, number>
counts
, 'clicks', 5) // => 1

Inspired by TC39’s Upsert proposal.