LimitedCollection
Creates a new array with the results of calling a provided function on every element in the collection.
Signature
export declare class LimitedCollection<K, V>fn- The function that produces an element of the new array.
thisArg- The value to use as `this` when executing the map function.
A new array with the results of calling the provided function on every element in the collection.
const collection = new Collection<number, string>();
collection.set(1, 'one');
collection.set(2, 'two');
collection.set(3, 'three');
const mappedArray = collection.map((value, key) => `${key}: ${value}`);
console.log(mappedArray); // Output: ['1: one', '2: two', '3: three']Properties
14Methods
13Properties
static readonly default: LimitedCollectionOptions<any, any>;private readonly data;private readonly expirations;private readonly options;private timeout;private _closer;private _closerDirty;private _pendingReset;closerGetterget closer(): { value: V; expire: number; expireOn: number;} | undefined;Returns the element in the limited collection that is closest to expiration.
The element that is closest to expiration, or `undefined` if the collection is empty.
const collection = new LimitedCollection<number, string>();
collection.set(1, 'one', 1000);
collection.set(2, 'two', 2000);
collection.set(3, 'three', 500);
const closestElement = collection.closer;
console.log(closestElement); // Output: { value: 'three', expire: 500, expireOn: [current timestamp + 500] }sizeGetterget size(): number;Returns the number of elements in the limited collection.
The number of elements in the collection.
const collection = new LimitedCollection<number, string>();
collection.set(1, 'one');
collection.set(2, 'two');
console.log(collection.size); // Output: 2private resetTimeout;private stopTimeout;private startTimeout;private clearExpired;Methods
constructorConstructorconstructor(options?: Partial<LimitedCollectionOptions<K, V>>);set(key: K, value: V, customExpire?: number): void;Adds an element to the limited collection.
key- The key of the element.
value- The value of the element.
customExpire- The custom expiration time for the element.
const collection = new LimitedCollection<number, string>({ limit: 3 });
collection.set(1, 'one');
collection.set(2, 'two');
collection.set(3, 'three');
console.log(collection.size); // Output: 3
collection.set(4, 'four');
console.log(collection.size); // Output: 3
console.log(collection.get(1)); // Output: undefinedraw(key: K): { value: V; expire: number; expireOn: number;} | undefined;Returns the raw data of an element in the limited collection.
key- The key of the element.
The raw data of the element, or `undefined` if the element does not exist.
const collection = new LimitedCollection<number, string>();
collection.set(1, 'one');
const rawData = collection.raw(1);
console.log(rawData); // Output: { value: 'one', expire: -1, expireOn: -1 }get(key: K): V | undefined;Returns the value of an element in the limited collection.
key- The key of the element.
The value of the element, or `undefined` if the element does not exist.
const collection = new LimitedCollection<number, string>();
collection.set(1, 'one');
const value = collection.get(1);
console.log(value); // Output: 'one'has(key: K): boolean;Checks if an element exists in the limited collection.
key- The key of the element.
`true` if the element exists, `false` otherwise.
const collection = new LimitedCollection<number, string>();
collection.set(1, 'one');
console.log(collection.has(1)); // Output: true
console.log(collection.has(2)); // Output: falsedelete(key: K): boolean;Removes an element from the limited collection.
key- The key of the element to remove.
`true` if the element was removed, `false` otherwise.
const collection = new LimitedCollection<number, string>();
collection.set(1, 'one');
console.log(collection.delete(1)); // Output: true
console.log(collection.delete(2)); // Output: falsekeys(): MapIterator<K>;values(): IterableIterator<V>;rawValues(): IterableIterator<LimitedCollectionData<V>>;entries(): IterableIterator<[ K, V]>;rawEntries(): IterableIterator<[ K, LimitedCollectionData<V>]>;[Symbol.iterator](): IterableIterator<[ K, V]>;clear(): void;