Collection

Represents a collection that extends the built-in Map class.

Signature

export declare class Collection<K, V> extends Map<K, V>
JSDoc
@templateK The type of the keys in the collection.@templateV The type of the values in the collection.

Methods

sweep(fn: (value: V, key: K, collection: this) => unknown): number;

Removes elements from the collection based on a filter function.

Parameters
fn
The filter function that determines which elements to remove.
thisArg
The value to use as `this` when executing the filter function.
Returns

The number of elements removed from the collection.

Examples
const collection = new Collection<number, string>();
collection.set(1, 'one');
collection.set(2, 'two');
collection.set(3, 'three');
const removedCount = collection.sweep((value, key) => key % 2 === 0);
console.log(removedCount); // Output: 1
console.log(collection.size); // Output: 2
map<T = any>(fn: (value: V, key: K, collection: this) => T): T[];

Creates a new array with the results of calling a provided function on every element in the collection.

Parameters
fn
The function that produces an element of the new array.
thisArg
The value to use as `this` when executing the map function.
Returns

A new array with the results of calling the provided function on every element in the collection.

Examples
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']
filter<S extends V>(fn: (value: V, key: K, collection: this) => value is S): S[];filter(fn: (value: V, key: K, collection: this) => boolean): V[];

Creates a new array with all elements that pass the test implemented by the provided function.

Parameters
fn
The function to test each element of the collection.
thisArg
The value to use as `this` when executing the filter function.
Returns

A new array with the elements that pass the test.

Examples
const collection = new Collection<number, string>();
collection.set(1, 'one');
collection.set(2, 'two');
collection.set(3, 'three');
const filteredArray = collection.filter((value, key) => key % 2 === 0);
console.log(filteredArray); // Output: ['two']
filterCollection<S extends V>(fn: (value: V, key: K, collection: this) => value is S): Collection<K, S>;filterCollection(fn: (value: V, key: K, collection: this) => boolean): Collection<K, V>;
reduce<T = any>(fn: (accumulator: T, value: V, key: K, collection: this) => T, initialValue?: T): T;

Apply a function against an accumulator and each element in the collection (from left to right) to reduce it to a single value.

Parameters
fn
The function to execute on each element in the collection.
initialValue
The initial value of the accumulator.
Returns

The value that results from the reduction.

Examples
const collection = new Collection<number, number>();
collection.set(1, 1);
collection.set(2, 2);
collection.set(3, 3);
const sum = collection.reduce((acc, value) => acc + value, 0);
console.log(sum); // Output: 6
every(fn: (value: V, key: K, collection: this) => boolean): boolean;

Checks if all elements in the collection pass a test implemented by the provided function.

Parameters
fn
The function to test each element of the collection.
Returns

`true` if all elements pass the test, otherwise `false`.

Examples
const collection = new Collection<number, number>();
collection.set(1, 1);
collection.set(2, 2);
collection.set(3, 3);
const allGreaterThanZero = collection.every(value => value > 0);
console.log(allGreaterThanZero); // Output: true
some(fn: (value: V, key: K, collection: this) => boolean): boolean;

Checks if at least one element in the collection passes a test implemented by the provided function.

Parameters
fn
The function to test each element of the collection.
Returns

`true` if at least one element passes the test, otherwise `false`.

Examples
const collection = new Collection<number, number>();
collection.set(1, 1);
collection.set(2, 2);
collection.set(3, 3);
const hasEvenValue = collection.some(value => value % 2 === 0);
console.log(hasEvenValue); // Output: true
find<S extends V>(fn: (value: V, key: K, collection: this) => value is S): S | undefined;find(fn: (value: V, key: K, collection: this) => boolean): V | undefined;

Returns the value of the first element in the collection that satisfies the provided testing function.

Parameters
fn
The function to test each element of the collection.
Returns

The value of the first element that passes the test. `undefined` if no element passes the test.

Examples
const collection = new Collection<number, number>();
collection.set(1, 1);
collection.set(2, 2);
collection.set(3, 3);
const firstEvenValue = collection.find(value => value % 2 === 0);
console.log(firstEvenValue); // Output: 2
findKey(fn: (value: V, key: K, collection: this) => boolean): K | undefined;

Returns the first key in the collection that satisfies the provided testing function.

Parameters
fn
The function to test each element of the collection.
Returns

The first key that passes the test. `undefined` if no element passes the test.

Examples
const collection = new Collection<number, number>();
collection.set(1, 1);
collection.set(2, 2);
collection.set(3, 3);
const firstEvenKey = collection.findKey(value => value % 2 === 0);
console.log(firstEvenKey); // Output: 2