UNPKG

@thunder04/supermap

Version:

Extended JS Map with Array-like methods

80 lines (79 loc) 4.93 kB
declare const kDateCache: unique symbol; export declare class SuperMap<K, V> extends Map<K, V> { #private; private [kDateCache]; constructor(options?: Partial<SuperMapOptions<K, V>>); /** Converts the Map to an array of entries. */ toArray(): [K, V][]; delete(key: K): boolean; /** * Identical to [Map.prototype.set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/set) but with a third argument. * @param ttl Time to live duration of this entry (in milliseconds). * - It has no effect if `options.intervalTime` isn't provided. * - `options.expireAfter` sums with `ttl`. */ set(key: K, value: V, ttl?: number): this; /** Update an entry without changing its TTL. If the entry doesn't exist, it returns `false`. */ update(key: K, value: V): boolean; /** * Identical to [Map.prototype.clear](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/clear) but with a second argument. * @param stopInterval If set to `true`, the sweeping interval is also stopped. */ clear(stopInterval?: boolean): void; /** * Gets the first key or value of the Map. * @param key If set to `true`, it will return the first key instead of value. */ first(key?: false): V | undefined; first(key: true): K | undefined; /** * Gets the last key or value of the Map. *This method should be avoided as it iterates the whole Map*. * @param key If set to `true`, it will return the last key instead of value. */ last(key?: false): V | undefined; last(key: true): K | undefined; /** Identical to [Array.prototype.some](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) */ some(func: (value: V, key: K, self: this) => boolean): boolean; /** Identical to [Array.prototype.every](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every) */ every(func: (value: V, key: K, self: this) => boolean): boolean; /** Deletes the entries that pass the `sweeper` callback and calls the `options.onSweep` callback (if provided). */ sweep(sweeper: (value: V, key: K, self: this) => boolean): number; /** Identical to [Array.prototype.filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) */ filter(func: (value: V, key: K, self: this) => boolean): import(".")<K, V>; /** * Identical to [Array.prototype.map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) * but this method also accepts a filter callback to filter the entries before mapping them without iterating the whole map again. * Prefer using this method instead of `<SuperMap>.filter(...).map(...)`. * @param filterFn Optional filter callback to filter entries. */ map<T>(mapFn: (value: V, key: K, self: this) => T, filterFn?: (value: V, key: K, self: this) => boolean): T[]; /** * Identical to [Array.prototype.find](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) but with a second argument. * @param returnKey If set to `true`, it will return the found key instead of value. */ find(func: (value: V, key: K, self: this) => boolean, returnKey: true): K | null; find(func: (value: V, key: K, self: this) => boolean, returnKey?: false): V | null; /** Identical to [Array.prototype.reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce) */ reduce<T>(fn: (accumulator: T | undefined, value: V, key: K, self: this) => T, initialValue?: T): NonNullable<T>; /** Identical to [Array.prototype.concat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat) */ concat(...children: ReadonlyArray<SuperMap<K, V>>): import(".")<K, V>; /** * Identical to [Array.prototype.concat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat) * but this method mutates this instance instead of creating a new one. */ concatMut(...children: ReadonlyArray<SuperMap<K, V>>): this; /** Identical to [Array.prototype.sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort). */ sort(sortFn: (vA: V, vB: V, kA: K, kB: K, self: this) => number): this; /** Starts or restarts the sweeping interval. It gets automatically called in the constructor if the `intervalTime` option has been provided. */ startInterval(): boolean; /** Stops the sweeping interval. */ stopInterval(): boolean; } //@ts-expect-error export = SuperMap; export interface SuperMapOptions<K = any, V = any> { onSweep: (value: V, key: K) => any; intervalTime: number; expireAfter: number; itemsLimit: number; }