Options
All
  • Public
  • Public/Protected
  • All
Menu

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

Index

Constructors

constructor

Properties

Private storedItems

storedItems: T[] = []

Accessors

length

  • get length(): number

Methods

[Symbol.iterator]

  • [Symbol.iterator](): Iterator<T, any, undefined>

all

  • all(): T[]

Private allAsNumbers

Private allAssociated

Private allOperator

associate

asyncPipe

at

average

clone

collapse

concat

contains

  • Returns true if the given key matches the given condition for any item in the collection.

    example
    const userExists = users.contains('username', '=', 'jdoe')
    

    Type parameters

    • T2

    Parameters

    Returns boolean

count

  • count(): number

diff

diffUsing

  • Return a collection of items that ARE in this collection, but NOT In the items collection using a helper function to determine whether two items are equal.

    example
    potentialUsers.diffUsing(storedUsers, (u1, u2) => u1.username.toLowerCase() === u2.username.toLowerCase())
    

    Type parameters

    • T2

    Parameters

    Returns Collection<T>

each

every

everyWhere

filter

find

  • find<T2>(func: KeyFunction<T, T2>): undefined | number

first

firstWhere

firstWhereNot

forPage

  • forPage(page: number, perPage: number): Collection<T>

get

groupBy

  • 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

implode

  • implode(delimiter: string): string

includes

  • includes(item: T): boolean

intersect

isEmpty

  • isEmpty(): boolean

isNotEmpty

  • isNotEmpty(): boolean

join

  • join(delimiter: string): string

keyBy

  • 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

keyMap

  • 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

last

lastWhere

lastWhereNot

map

max

median

merge

min

mode

nth

  • Return every nth item in the collection.

    example
    const items = collect(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
    
    items.nth(3)  // => Collection['a', 'd', 'g']
    

    Parameters

    • n: number

    Returns Collection<T>

pad

partialMap

  • Create a new collection by mapping the items in this collection using the given function, excluding any for which the function returns undefined.

    Type parameters

    • T2

    Parameters

    Returns Collection<NonNullable<T2>>

pipe

pluck

  • 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

    Parameters

    Returns Collection<T2>

pop

prepend

promiseMap

  • 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>>

push

put

random

reduce

  • 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 undefined | T2

reject

reverse

search

shift

shuffle

slice

  • slice(start: number, end: number): Collection<T>

some

  • some(func: (item: T) => boolean): boolean
  • 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) => boolean
        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns boolean

sort

sortBy

sortByDesc

sortDesc

splice

  • splice(start: number, deleteCount?: number): Collection<T>

sum

  • 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

take

tap

  • Apply the given function to this collection then return the collection. This is intended to help with chaining.

    example
    collection.tap(coll => {
            coll.push(item)
        })
        .where('someKey', '>', 4)
        // ... &c.
    

    Type parameters

    • T2

    Parameters

    Returns Collection<T>

toArray

  • toArray(): any[]

toJSON

  • toJSON(replacer?: undefined, space?: number): string
  • Cast the collection to a JSON string, optionally specifying the replacer and indentation.

    Parameters

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

    Returns string

unique

  • 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>

unless

when

where

  • Filter the collection by the given where-condition.

    example
    const users = collect([
     {uid: 1, name: 'John'},
     {uid: 2, name: 'Jane'},
     {uid: 3, name: 'James'},
    ])
    
    users.where('uid', '<', 3)  // => Collection[{uid: 1, name: 'John'}, {uid: 2, name: 'Jane'}]
    

    Type parameters

    • T2

    Parameters

    Returns Collection<T>

whereIn

whereMax

whereMin

whereNot

whereNotIn

Static collect

Static fill

  • fill<T2>(size: number, item: T2): Collection<T2>

Static normalize

  • normalize<T2>(itemOrItems: undefined | T2 | (undefined | T2)[]): Collection<T2>
  • Create a new collection from an item or array of items. Filters out undefined items.

    Type parameters

    • T2

    Parameters

    • itemOrItems: undefined | T2 | (undefined | T2)[]

    Returns Collection<T2>

Static size

Extollo Logo

extollo (v. latin) - to lift up, to elevate

Extollo is a free & libre application framework in TypeScript.