advanced-map-initialized
Version:
Map-like class with default values
41 lines (40 loc) • 1.45 kB
TypeScript
import Base from 'advanced-map-base';
import { MapLike, MapLikeConstructor, InitFunc } from 'advanced-map-types';
/**
* Objects of this class call their `init` function
* every time`get()`is invoked upon non-existing key.
* As a result, `get()` never return `undefined`.
*/
declare class Initialized<Key, Value, Data extends MapLike<Key, Value> = Map<Key, Value>> extends Base<Key, Value, Data> {
private readonly init;
/**
* @param Map A constructor (a.k.a class) that creates a Map-like object.
* @param init Function that takes a yet-to-initialized key and returns default value.
*/
constructor(Map: MapLikeConstructor<Data>, init: InitFunc<Key, Value>);
/**
* Check if a certain key is initialized/set.
* @returns `true` if given key is initialized or set, `false` otherwise.
*/
has(key: Key): boolean;
/**
* If `key` exists, return corresponding value.
* Otherwise, call `init` function and add its returning value to the map.
* @param key Map's key.
* @returns Corresponding value.
*/
get(key: Key): Value;
/**
* Set `key` to point to `value`.
* @param key Key.
* @param value Value.
*/
set(key: Key, value: Value): this;
/**
* Delete a key if it exists.
* @param key Key to be deleted.
* @returns `true` if deleted key existed and `false` otherwise.
*/
delete(key: Key): boolean;
}
export = Initialized;