/** {@link Map} like API. */ export interface MapLike { /** Returns a specified element. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it. */ get: (key: K) => V | undefined; /** Whether an element with the specified key exists or not. */ has: (key: K) => boolean; /** Adds a new element with a specified key and value. */ set: (key: K, value: V) => void; } /** Insertable API. */ export interface Insertable { /** Add entry. */ insert: InsertCallback; } export interface InsertCallback { (key: K, that: T): V; } /** Updatable API. */ export interface Updatable { /** Update the value. */ update: UpdateCallback; } export interface UpdateCallback { (existing: V, key: K, that: T): V; } /** Handler for emplace. */ export interface EmplaceHandler extends Insertable, Updatable { } /** Add a value to a {@link map} if it does not already have something at {@link key}, and will also update an existing value at {@link key}. * * @example * ```ts * import { emplace } from "https://deno.land/x/upsert/emplace.ts"; * import { assert } from "https://deno.land/std/testing/asserts.ts"; * * declare const map: Map; * declare const key: string; * * const result = emplace(map, key, { * insert: () => 0, * update: (existing) => ++existing, * }); * assert(map.has(key)); * ``` * * ## Just insert if missing * You might omit an `update` if you're handling data that doesn't change, but can * still be appended. * * ```ts * import { emplace } from "https://deno.land/x/upsert/emplace.ts"; * import { assert } from "https://deno.land/std/testing/asserts.ts"; * import { * assertType, * type IsExact, * } from "https://deno.land/std/testing/types.ts"; * * declare const map: Map; * declare const key: string; * declare const value: number; * * const result = emplace(map, key, { insert: () => value }); * * assert(map.has(key)); * assertType>(true); * ``` * * ## Just update if present * You might want to omit an `insert` if you want to perform a function on all * existing values. * * ```ts * import { emplace } from "https://deno.land/x/upsert/emplace.ts"; * import { assert } from "https://deno.land/std/testing/asserts.ts"; * import { * assertType, * type IsExact, * } from "https://deno.land/std/testing/types.ts"; * * declare const map: Map; * declare const key: string; * * const result = emplace(map, key, { update: (existing) => ++existing }); * * assertType>(true); * ``` */ export declare function emplace(map: Readonly> & M, key: K, handler: Readonly>): V; export declare function emplace(map: Readonly> & M, key: K, handler: Readonly>): V; export declare function emplace(map: Readonly> & M, key: K, handler: Readonly>): V | undefined; /** Add the entry if the {@link key} does not exist. * * @example * ```ts * import { insert } from "https://deno.land/x/upsert/mod.ts"; * import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; * * declare const key: string; * declare const value: number; * const map = new Map(); * * insert(map, key, () => value); * * assertEquals(map, new Map([[key, value]])); * ``` */ export declare function insert>(map: Readonly> & M, key: K, callback: InsertCallback): V; /** Update the entry if the {@link key} exists. * * @example * ```ts * import { update } from "https://deno.land/x/upsert/mod.ts"; * import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; * * declare const key: string; * const map = new Map([[key, 0]]); * * update(map, key, (existing) => ++existing); * * assertEquals(map, new Map([[key, 1]])); * ``` */ export declare function update>(map: Readonly> & M, key: K, callback: UpdateCallback): V | undefined;