Skip to content

Collection

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

Extends

  • Map<K, V>

Type parameters

Type parameterDescription
KThe type of the keys in the collection.
VThe type of the values in the collection.

Constructors

new Collection(entries)

1
new Collection<K, V>(entries?: null | readonly readonly [K, V][]): Collection<K, V>

Parameters

ParameterType
entries?null | readonly readonly [K, V][]

Returns

Collection<K, V>

Inherited from

Map<K, V>.constructor

Source

node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2015.collection.d.ts:50

new Collection(iterable)

1
new Collection<K, V>(iterable?: null | Iterable<readonly [K, V]>): Collection<K, V>

Parameters

ParameterType
iterable?null | Iterable<readonly [K, V]>

Returns

Collection<K, V>

Inherited from

Map<K, V>.constructor

Source

node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2015.collection.d.ts:49

Properties

PropertyModifierTypeDescriptionInherited from
[toStringTag]readonlystring-Map.[toStringTag]
sizereadonlynumberMap.size
[species]readonlyMapConstructor-Map.[species]

Methods

[iterator]()

1
iterator: IterableIterator<[K, V]>

Returns an iterable of entries in the map.

Returns

IterableIterator<[K, V]>

Inherited from

Map.[iterator]

Source

node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2015.iterable.d.ts:119


clear()

1
clear(): void

Returns

void

Inherited from

Map.clear

Source

node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2015.collection.d.ts:20


delete()

1
delete(key: K): boolean

Parameters

ParameterType
keyK

Returns

boolean

true if an element in the Map existed and has been removed, or false if the element does not exist.

Inherited from

Map.delete

Source

node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2015.collection.d.ts:24


entries()

1
entries(): IterableIterator<[K, V]>

Returns an iterable of key, value pairs for every entry in the map.

Returns

IterableIterator<[K, V]>

Inherited from

Map.entries

Source

node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2015.iterable.d.ts:124


every()

1
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

ParameterTypeDescription
fn(value: V, key: K, collection: this) => booleanThe function to test each element of the collection.

Returns

boolean

true if all elements pass the test, otherwise false.

Example

1
const collection = new Collection<number, number>();
2
collection.set(1, 1);
3
collection.set(2, 2);
4
collection.set(3, 3);
5
const allGreaterThanZero = collection.every(value => value > 0);
6
console.log(allGreaterThanZero); // Output: true

Source

seyfert/src/collection.ts:120


filter()

1
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

ParameterTypeDescription
fn(value: V, key: K, collection: this) => booleanThe function to test each element of the collection.

Returns

V[]

A new array with the elements that pass the test.

Example

1
const collection = new Collection<number, string>();
2
collection.set(1, 'one');
3
collection.set(2, 'two');
4
collection.set(3, 'three');
5
const filteredArray = collection.filter((value, key) => key % 2 === 0);
6
console.log(filteredArray); // Output: ['two']

Source

seyfert/src/collection.ts:67


find()

1
find(fn: (value: V, key: K, collection: this) => boolean): undefined | V

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

Parameters

ParameterTypeDescription
fn(value: V, key: K, collection: this) => booleanThe function to test each element of the collection.

Returns

undefined | V

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

Example

1
const collection = new Collection<number, number>();
2
collection.set(1, 1);
3
collection.set(2, 2);
4
collection.set(3, 3);
5
const firstEvenValue = collection.find(value => value % 2 === 0);
6
console.log(firstEvenValue); // Output: 2

Source

seyfert/src/collection.ts:164


findKey()

1
findKey(fn: (value: V, key: K, collection: this) => boolean): undefined | K

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

Parameters

ParameterTypeDescription
fn(value: V, key: K, collection: this) => booleanThe function to test each element of the collection.

Returns

undefined | K

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

Example

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

Source

seyfert/src/collection.ts:185


forEach()

1
forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void, thisArg?: any): void

Executes a provided function once per each key/value pair in the Map, in insertion order.

Parameters

ParameterType
callbackfn(value: V, key: K, map: Map<K, V>) => void
thisArg?any

Returns

void

Inherited from

Map.forEach

Source

node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2015.collection.d.ts:28


get()

1
get(key: K): undefined | V

Returns a specified element from the Map object. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Map.

Parameters

ParameterType
keyK

Returns

undefined | V

Returns the element associated with the specified key. If no element is associated with the specified key, undefined is returned.

Inherited from

Map.get

Source

node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2015.collection.d.ts:33


has()

1
has(key: K): boolean

Parameters

ParameterType
keyK

Returns

boolean

boolean indicating whether an element with the specified key exists or not.

Inherited from

Map.has

Source

node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2015.collection.d.ts:37


keys()

1
keys(): IterableIterator<K>

Returns an iterable of keys in the map

Returns

IterableIterator<K>

Inherited from

Map.keys

Source

node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2015.iterable.d.ts:129


map()

1
map<T>(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.

Type parameters

Type parameterValue
Tany

Parameters

ParameterTypeDescription
fn(value: V, key: K, collection: this) => TThe function that produces an element of the new array.

Returns

T[]

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

Example

1
const collection = new Collection<number, string>();
2
collection.set(1, 'one');
3
collection.set(2, 'two');
4
collection.set(3, 'three');
5
const mappedArray = collection.map((value, key) => `${key}: ${value}`);
6
console.log(mappedArray); // Output: ['1: one', '2: two', '3: three']

Source

seyfert/src/collection.ts:44


reduce()

1
reduce<T>(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.

Type parameters

Type parameterValue
Tany

Parameters

ParameterTypeDescription
fn(accumulator: T, value: V, key: K, collection: this) => TThe function to execute on each element in the collection.
initialValue?TThe initial value of the accumulator.

Returns

T

The value that results from the reduction.

Example

1
const collection = new Collection<number, number>();
2
collection.set(1, 1);
3
collection.set(2, 2);
4
collection.set(3, 3);
5
const sum = collection.reduce((acc, value) => acc + value, 0);
6
console.log(sum); // Output: 6

Source

seyfert/src/collection.ts:90


set()

1
set(key: K, value: V): this

Adds a new element with a specified key and value to the Map. If an element with the same key already exists, the element will be updated.

Parameters

ParameterType
keyK
valueV

Returns

this

Inherited from

Map.set

Source

node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2015.collection.d.ts:41


some()

1
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

ParameterTypeDescription
fn(value: V, key: K, collection: this) => booleanThe function to test each element of the collection.

Returns

boolean

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

Example

1
const collection = new Collection<number, number>();
2
collection.set(1, 1);
3
collection.set(2, 2);
4
collection.set(3, 3);
5
const hasEvenValue = collection.some(value => value % 2 === 0);
6
console.log(hasEvenValue); // Output: true

Source

seyfert/src/collection.ts:142


sweep()

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

Removes elements from the collection based on a filter function.

Parameters

ParameterTypeDescription
fn(value: V, key: K, collection: this) => unknownThe filter function that determines which elements to remove.

Returns

number

The number of elements removed from the collection.

Example

1
const collection = new Collection<number, string>();
2
collection.set(1, 'one');
3
collection.set(2, 'two');
4
collection.set(3, 'three');
5
const removedCount = collection.sweep((value, key) => key % 2 === 0);
6
console.log(removedCount); // Output: 1
7
console.log(collection.size); // Output: 2

Source

seyfert/src/collection.ts:23


values()

1
values(): IterableIterator<V>

Returns an iterable of values in the map

Returns

IterableIterator<V>

Inherited from

Map.values

Source

node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.es2015.iterable.d.ts:134


groupBy()

1
static groupBy<K, T>(items: Iterable<T>, keySelector: (item: T, index: number) => K): Map<K, T[]>

Groups members of an iterable according to the return value of the passed callback.

Type parameters

Type parameter
K
T

Parameters

ParameterTypeDescription
itemsIterable<T>An iterable.
keySelector(item: T, index: number) => KA callback which will be invoked for each item in items.

Returns

Map<K, T[]>

Inherited from

Map.groupBy

Source

node_modules/.pnpm/typescript@5.4.5/node_modules/typescript/lib/lib.esnext.collection.d.ts:25