Skip to content

Collection

Package: @erinjs/collection

Extended Map with utility methods for key-value collections. Similar to discord.js Collection.

Properties

None

Methods

clone()

Creates an identical shallow copy of this collection.

Returns: Collection<K, V>

Parameters:

None


concat()

Combines this collection with others into a new collection.

Returns: Collection<K, V>

Parameters:

NameTypeOptionalDescription
collectionsReadonlyCollection<K, V>[]No

every()

Tests whether all elements pass the test implemented by the provided function.

Returns: boolean

Parameters:

NameTypeOptionalDescription
fn(value: V, key: K) => booleanNo

filter()

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

Returns: Collection<K, V>

Parameters:

NameTypeOptionalDescription
fn(value: V, key: K) => booleanNo

find()

Searches for a single item where the given function returns a truthy value.

Returns: V | undefined

Parameters:

NameTypeOptionalDescription
fn(value: V, key: K) => booleanNo

findKey()

Returns the key of the first item where the given function returns a truthy value.

Returns: K | undefined

Parameters:

NameTypeOptionalDescription
fn(value: V, key: K) => booleanNo

first()

Obtains the first value(s) in this collection.

Returns: V | undefined

Parameters:

None


first()

Returns: V[]

Parameters:

NameTypeOptionalDescription
amountnumberNo

first()

Returns: V | V[] | undefined

Parameters:

NameTypeOptionalDescription
amountnumberYes

last()

Obtains the last value(s) in this collection.

Returns: V | undefined

Parameters:

None


last()

Returns: V[]

Parameters:

NameTypeOptionalDescription
amountnumberNo

last()

Returns: V | V[] | undefined

Parameters:

NameTypeOptionalDescription
amountnumberYes

map()

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

Returns: T[]

Parameters:

NameTypeOptionalDescription
fn(value: V, key: K) => TNo

partition()

Partitions the collection into two collections: one that passes the predicate and one that fails.

Returns: [Collection<K, V>, Collection<K, V>]

Parameters:

NameTypeOptionalDescription
fn(value: V, key: K) => booleanNo

random()

Returns a random value from this collection.

Returns: V | undefined

Parameters:

None


random()

Returns: V[]

Parameters:

NameTypeOptionalDescription
amountnumberNo

random()

Returns: V | V[] | undefined

Parameters:

NameTypeOptionalDescription
amountnumberYes

reduce()

Applies a function against an accumulator and each element to reduce the collection to a single value.

Returns: T

Parameters:

NameTypeOptionalDescription
fn(accumulator: T, value: V, key: K) => TNo
initialValueTNo

some()

Tests whether at least one element passes the test implemented by the provided function.

Returns: boolean

Parameters:

NameTypeOptionalDescription
fn(value: V, key: K) => booleanNo

sort()

Sorts the collection in place and returns it.

Returns: this

Parameters:

NameTypeOptionalDescription
compareFn(a: V, b: V, aKey: K, bKey: K) => numberYes

tap()

Invokes the given function and returns this collection (for chaining).

Returns: this

Parameters:

NameTypeOptionalDescription
fn(collection: this) => voidNo

toJSON()

Returns an array of the values in this collection.

Returns: V[]

Parameters:

None


toString()

Returns a string representation of the collection (array of values).

Returns: string

Parameters:

None

Released under the Apache-2.0 License.