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 _ from 'radashi'
const counts = new Map<string, number>()
_.getOrInsert(counts, 'clicks', 1) // => 1
_.getOrInsert(counts, 'clicks', 5) // => 1

Inspired by TC39’s Upsert proposal.