LimitedCollection
Creates a new array with the results of calling a provided function on every element in the collection.
Param
The function that produces an element of the new array.
Param
The value to use as this
when executing the map function.
Example
1const collection = new Collection<number, string>();2collection.set(1, 'one');3collection.set(2, 'two');4collection.set(3, 'three');5const mappedArray = collection.map((value, key) => `${key}: ${value}`);6console.log(mappedArray); // Output: ['1: one', '2: two', '3: three']
Type parameters
Type parameter |
---|
K |
V |
Constructors
new LimitedCollection(options)
1new LimitedCollection<K, V>(options: Partial<LimitedCollectionOptions<K, V>>): LimitedCollection<K, V>
Parameters
Parameter | Type |
---|---|
options | Partial <LimitedCollectionOptions <K , V >> |
Returns
LimitedCollection
<K
, V
>
Source
Properties
Property | Modifier | Type |
---|---|---|
default | readonly | LimitedCollectionOptions <any , any > |
Accessors
closer
1get closer(): undefined | LimitedCollectionData<V>
Returns the element in the limited collection that is closest to expiration.
Example
1const collection = new LimitedCollection<number, string>();2collection.set(1, 'one', 1000);3collection.set(2, 'two', 2000);4collection.set(3, 'three', 500);5const closestElement = collection.closer;6console.log(closestElement); // Output: { value: 'three', expire: 500, expireOn: [current timestamp + 500] }
Returns
undefined
| LimitedCollectionData
<V
>
The element that is closest to expiration, or undefined
if the collection is empty.
Source
size
1get size(): number
Returns the number of elements in the limited collection.
Example
1const collection = new LimitedCollection<number, string>();2collection.set(1, 'one');3collection.set(2, 'two');4console.log(collection.size); // Output: 2
Returns
number
The number of elements in the collection.
Source
Methods
clear()
1clear(): void
Returns
void
Source
delete()
1delete(key: K): boolean
Removes an element from the limited collection.
Parameters
Parameter | Type | Description |
---|---|---|
key | K | The key of the element to remove. |
Returns
boolean
true
if the element was removed, false
otherwise.
Example
1const collection = new LimitedCollection<number, string>();2collection.set(1, 'one');3console.log(collection.delete(1)); // Output: true4console.log(collection.delete(2)); // Output: false
Source
entries()
1entries(): IterableIterator<[K, LimitedCollectionData<V>]>
Returns
IterableIterator
<[K
, LimitedCollectionData
<V
>]>
Source
get()
1get(key: K): undefined | V
Returns the value of an element in the limited collection.
Parameters
Parameter | Type | Description |
---|---|---|
key | K | The key of the element. |
Returns
undefined
| V
The value of the element, or undefined
if the element does not exist.
Example
1const collection = new LimitedCollection<number, string>();2collection.set(1, 'one');3const value = collection.get(1);4console.log(value); // Output: 'one'
Source
has()
1has(key: K): boolean
Checks if an element exists in the limited collection.
Parameters
Parameter | Type | Description |
---|---|---|
key | K | The key of the element. |
Returns
boolean
true
if the element exists, false
otherwise.
Example
1const collection = new LimitedCollection<number, string>();2collection.set(1, 'one');3console.log(collection.has(1)); // Output: true4console.log(collection.has(2)); // Output: false
Source
keys()
1keys(): () => IterableIterator<K>
Returns
Function
Returns an iterable of keys in the map
Returns
IterableIterator
<K
>
Source
raw()
1raw(key: K): undefined | LimitedCollectionData<V>
Returns the raw data of an element in the limited collection.
Parameters
Parameter | Type | Description |
---|---|---|
key | K | The key of the element. |
Returns
undefined
| LimitedCollectionData
<V
>
The raw data of the element, or undefined
if the element does not exist.
Example
1const collection = new LimitedCollection<number, string>();2collection.set(1, 'one');3const rawData = collection.raw(1);4console.log(rawData); // Output: { value: 'one', expire: -1, expireOn: -1 }
Source
set()
1set(2 key: K,3 value: V,4 customExpire: number): void
Adds an element to the limited collection.
Parameters
Parameter | Type | Description |
---|---|---|
key | K | The key of the element. |
value | V | The value of the element. |
customExpire | number | The custom expiration time for the element. |
Returns
void
Example
1const collection = new LimitedCollection<number, string>({ limit: 3 });2collection.set(1, 'one');3collection.set(2, 'two');4collection.set(3, 'three');5console.log(collection.size); // Output: 36collection.set(4, 'four');7console.log(collection.size); // Output: 38console.log(collection.get(1)); // Output: undefined
Source
values()
1values(): () => IterableIterator<LimitedCollectionData<V>>
Returns
Function
Returns an iterable of values in the map
Returns
IterableIterator
<LimitedCollectionData
<V
>>