Skip to content

upsert

upsert<K, V>(key, f): (m) => ReadonlyMap<K, V>

Defined in: Utils/Dict.ts:224

Returns a new dictionary with the value at key set by f. If the key does not exist, f receives None. If the key exists, f receives Some(currentValue).

Useful for incrementing counters, initialising defaults, or conditional updates.

K

V

K

(existing) => V

(m): ReadonlyMap<K, V>

ReadonlyMap<K, V>

ReadonlyMap<K, V>

import { Maybe } from "@nlozgachev/pipelined/core";

const increment = (opt: Maybe<number>) => Maybe.getOrElse(() => 0)(opt) + 1;
pipe(Dict.fromEntries([["views", 5]]), Dict.upsert("views", increment)); // { views: 6 }
pipe(Dict.fromEntries([["views", 5]]), Dict.upsert("likes", increment)); // { views: 5, likes: 1 }