Class Collection<T>

A helper class for working with arrays of items in a more robust fashion. Provides helpers for accessing sub-keys, filtering, piping, and aggregate functions.

Type Parameters

  • T

Hierarchy

  • Collection

Constructors

Properties

pushSubscribers: Subscription<T>[] = []
storedItems: T[] = []

Accessors

Methods

  • Helper to notify subscribers that an item has been pushed to the collection.

    Parameters

    • item: T

    Returns void

  • Return an object mapping key values to arrays of records with that key.

    Example

    const users = collect([
    {uid: 1, name: 'John', type: 'admin'},
    {uid: 2, name: 'Jane', type: 'user'},
    {uid: 3, name: 'James', type: 'user'},
    ])

    users.groupBy('type') // => {admin: [{uid: 1, ...}], user: [{uid: 2, ...}, {uid: 3, ...}]}

    Type Parameters

    • T2

    Parameters

    Returns any

  • Join the items in the collection to a string delimited by the given delimiter.

    Parameters

    • delimiter: string

    Returns string

  • Join the items in the collection to a string delimited by the given delimiter.

    Parameters

    • delimiter: string

    Returns string

  • Convert this collection to an object keyed by the given field.

    Example

    const users = collect([{uid: 1, name: 'John'}, {uid: 2, name: 'Jane'}])
    users.keyBy('name') // => {John: {uid: 1, name: 'John'}, Jane: {uid: 2, name: 'Jane'}}

    Parameters

    Returns {
        [key: string]: T;
    }

    • [key: string]: T
  • Convert this collection to an object keyed by the given field, whose values are the output of the value operator.

    Example

    const users = collect([{uid: 1, name: 'John'}, {uid: 2, name: 'Jane'}])
    users.keyMap('name', 'uid') // => {John: 1, Jane: 2}

    Type Parameters

    • T2

    Parameters

    Returns {
        [key: string]: T2;
    }

    • [key: string]: T2
  • Map a method on the underlying type, passing it any required parameters. This is delightfully type-safe.

    Type Parameters

    • T2 extends string | number | symbol

    Parameters

    • method: T2
    • Rest ...params: Parameters<MethodType<T, T2, ((...args: any[]) => any)>>

    Returns Collection<ReturnType<MethodType<T, T2, ((...args: any[]) => any)>>>

  • Map the collection to a collection of the values of the key.

    Example

    const users = collect([
    {uid: 1, name: 'John', type: 'admin'},
    {uid: 2, name: 'Jane', type: 'user'},
    {uid: 3, name: 'James', type: 'user'},
    ])

    users.pluck('name') // => Collection['John', 'Jane', 'James']

    Type Parameters

    • T2 extends string | number | symbol

    Parameters

    • key: T2

    Returns Collection<T[T2]>

  • Like map(), but the callback can be async.

    Example

    A trivial example, but demonstrative:

    const collection = collect([1, 2, 3])

    collection.map(async item => item + 1) // => Collection[Promise<1>, Promise<2>, Promise<3>]

    collection.promiseMap(async item => item + 1) // => Promise<Collection[2, 3, 4]>

    Type Parameters

    • T2

    Parameters

    Returns Promise<Collection<T2>>

  • Reduce this collection to a single value using the given reducer function.

    Example

    const items = collect([1, 3, 5, 7])

    items.reduce((sum, item) => sum + item, 0) // => 16

    Type Parameters

    • T2

    Parameters

    Returns T2

  • Returns true if the given function returns truthy for any item in the collection. Stops executing if a single truth case is found.

    Parameters

    • func: ((item: T) => Maybe<boolean>)
        • (item: T): Maybe<boolean>
        • Parameters

          • item: T

          Returns Maybe<boolean>

    Returns boolean

  • Return the sum of the items in the collection, optionally by key.

    Example

    const items = collect([{k1: 1}, {k1: 3}, {k1: 5}, {k1: 7}])

    items.sum('k1') // => 16

    Type Parameters

    • T2

    Parameters

    Returns number

  • Cast the collection to a JSON string, optionally specifying the replacer and indentation.

    Parameters

    • replacer: undefined = undefined
    • space: number = 4

    Returns string

  • Return all distinct items in this collection. If a key is specified, returns all unique values of that key.

    Example

    const users = collect([
    {uid: 1, name: 'John', type: 'admin'},
    {uid: 2, name: 'Jane', type: 'user'},
    {uid: 3, name: 'James', type: 'user'},
    ])

    users.unique('type') // => Collection['admin', 'user']

    Type Parameters

    • T2

    Parameters

    Returns Collection<T | T2>