betmap
Version:
Better Map()
113 lines (112 loc) • 4.11 kB
TypeScript
/**
* Event types
*/
export type BMapEventType = 'add' | 'update' | 'delete';
/**
* Event listener. `entries` contains the added/updated/deleted entries.
*/
export type BMapListener<K, V> = (entries: BMap<K, V>) => void;
/**
* Better Map()
*/
export declare class BMap<K, V> extends Map<K, V> {
private readonly _listeners;
/**
* Custom serialization function
*/
toJSON(): Array<[K, V]>;
/**
* Register listener
*/
on(event: BMapEventType, listener: BMapListener<K, V>): this;
/**
* Notify subscribers
*/
private _notify;
/**
* Set key-value, return whether it's new.
*/
private _set;
set(key: K, value: V): this;
/**
* Batch add/update entries
*/
bSet(entries: Array<[K, V]>): this;
/**
* Batch get multiple entries from the BMap.
* @param keys An array of keys to retrieve.
* @returns A new BMap containing the requested entries.
*/
bGet(keys: K[]): BMap<K, V>;
/**
* Delete by key. Returns deleted entry if key existed
*/
private _delete;
delete(key: K): boolean;
/**
* Batch delete entries by key
*/
bDelete(keys: K[]): boolean[];
clear(): void;
/**
* Filter the map based on a predicate function.
* @param predicate A function that determines whether a key-value pair should be included.
* @returns A new BMap containing the filtered key-value pairs.
*/
filter(predicate: (key: K, value: V) => boolean): BMap<K, V>;
/**
* Returns the key-value pair of the first element in the map where predicate is true, and undefined otherwise.
* @param predicate A function that returns true for the first matching key-value pair.
* @returns The first matching key-value pair, or undefined if none is found.
*/
find(predicate: (key: K, value: V) => boolean): [K, V] | undefined;
/**
* Check if at least one key-value pair satisfies a condition.
* @param predicate A function that returns true for a matching key-value pair.
* @returns True if at least one matching key-value pair is found, false otherwise.
*/
some(predicate: (key: K, value: V) => boolean): boolean;
/**
* Check if every key-value pair satisfies a condition.
* @param predicate A function that returns true for a matching key-value pair.
* @returns True if every key-value pair matches the condition, false otherwise.
*/
every(predicate: (key: K, value: V) => boolean): boolean;
/**
* Map over the entries of the BMap and apply a transformation function.
* @param callback A function that transforms each key/value.
* @returns A new BMap with transformed entries.
*/
mapEntries<K2, V2>(callback: (key: K, value: V, map: BMap<K, V>) => [K2, V2]): BMap<K2, V2>;
/**
* Map over the values of the BMap and apply a transformation function.
* @param callback A function that transforms each value.
* @returns A new BMap with transformed values.
*/
mapValues<V2>(callback: (key: K, value: V, map: BMap<K, V>) => V2): BMap<K, V2>;
/**
* Map over the keys of the BMap and apply a transformation function.
* @param callback A function that transforms each key.
* @returns A new BMap with transformed keys.
*/
mapKeys<K2>(callback: (key: K, value: V, map: BMap<K, V>) => K2): BMap<K2, V>;
/**
* Sort the entries of the BMap in place.
* @param compareFunction A function that defines the sort order.
* @returns The sorted BMap.
*/
sort(compareFunction: (a: {
key: K;
value: V;
}, b: {
key: K;
value: V;
}) => number): BMap<K, V>;
/**
* Merge two maps, resolving conflicts based on a customizable merge strategy.
* @param otherMap The map to merge into the current map.
* @param mergeStrategy A function that defines the merge strategy for conflicts.
* @returns The merged BMap.
*/
merge(otherMap: Map<K, V>, mergeStrategy: (key: K, existingValue: V, incomingValue: V) => V): BMap<K, V>;
}