UNPKG

space-lift

Version:

TypeScript Array, Object, Map, Set, Union, Enum utils

70 lines (69 loc) 2.78 kB
import { ObjectWrapper } from './object'; import type { Pipe } from './lift'; import { Draft, NoReturn, AtomicObject } from './immupdate'; /** A Map wrapper providing extra functionalities and more chaining opportunities */ export declare class MapWrapper<K, V, M extends ReadonlyMap<K, V>> { private _value; constructor(_value: M); private _isLiftWrapper; value(): M; private _clone; /** * Sets a new key/value. */ set(key: K, value: V): MapWrapper<unknown, unknown, Map<K, V>>; /** * Deletes a key/value. */ delete(key: K): MapWrapper<unknown, unknown, Map<K, V>>; /** * Maps this Map's keys and values, unless void or undefined is returned, in which case the entry is filtered. * This is effectively a filter + map combined in one. */ collect<KK, VV>(iterator: (key: K, value: V) => [KK, VV] | undefined | void): MapWrapper<KK, VV, MapOf<M, KK, VV>>; /** * Filters this Map's keys and values by aplying a predicate to all values and refine its type. */ filter<VV extends V>(predicate: (key: K, value: V) => value is VV): MapWrapper<K, VV, MapOf<M, K, VV>>; /** * Filters this Map's keys and values. */ filter(predicate: (key: K, value: V) => boolean): this; /** * Returns the first element in this Map or undefined. */ first(): V | undefined; /** * Returns the last element in this Map or undefined. */ last(): V | undefined; /** * Maps this map's values. */ mapValues<VV>(mapFunction: (value: V) => VV): MapWrapper<K, VV, MapOf<M, K, VV>>; /** * Pipes this Map with an arbitrary transformation function. */ pipe: typeof import("./lift").pipe; /** * If this key is missing, set a default value. */ setDefaultValue(key: K, value: V): MapWrapper<K, V, M>; /** * If the key is found, run the drafted value through an update function. * For primitives, the update function must return a new value whereas for objects, the drafted value can be modified directly. */ updateValue(key: K, updater: (value: Draft<V>) => V extends AtomicObject ? V : NoReturn): MapWrapper<K, V, M>; /** * Transforms this Map into an Array of [key, value] tuples. */ toArray(): import("./array").ArrayWrapper<[K, V][]>; /** * Transforms this Map into an Object. * Only available if this Map's keys are a subtype of string or number. */ toObject<KK extends string | number>(this: MapWrapper<KK, V, any>): ObjectWrapper<Record<KK, V | undefined>>; } export declare function setMapPipe(_pipe: Pipe): void; declare type MapOf<T extends ReadonlyMap<unknown, unknown>, K, V> = T extends Map<any, any> ? Map<K, V> : ReadonlyMap<K, V>; export {};