Options
All
  • Public
  • Public/Protected
  • All
Menu

A reusable storage concept which blends the functionality of an Array with the efficient key-based lookup of a Map. This concept is reused throughout Foundry VTT where a collection of uniquely identified elements is required.

Hierarchy

Index

Constructors

  • Parameters

    • entries: any

    Returns Collection

Accessors

  • get contents(): any[]
  • Return an Array of all the entry values in the Collection

    Returns any[]

Methods

  • find(condition: ((arg0: any, arg1: number, arg2: Collection) => boolean)): any
  • Find an entry in the Map using a functional condition.

    see

    {Array#find}

    example

    Create a new Collection and reference its contents

    let c = new Collection([["a", "A"], ["b", "B"], ["c", "C"]]);
    c.get("a") === c.find(entry => entry === "A"); // true

    Parameters

    • condition: ((arg0: any, arg1: number, arg2: Collection) => boolean)

      The functional condition to test. Positional arguments are the value, the index of iteration, and the collection being searched.

        • (arg0: any, arg1: number, arg2: Collection): boolean
        • Parameters

          Returns boolean

    Returns any

    The value, if found, otherwise undefined

  • filter(condition: ((arg0: any, arg1: number, arg2: Collection) => boolean)): any[]
  • Filter the Collection, returning an Array of entries which match a functional condition.

    see

    {Array#filter}

    example

    Filter the Collection for specific entries

    let c = new Collection([["a", "AA"], ["b", "AB"], ["c", "CC"]]);
    let hasA = c.filters(entry => entry.slice(0) === "A");

    Parameters

    • condition: ((arg0: any, arg1: number, arg2: Collection) => boolean)

      The functional condition to test. Positional arguments are the value, the index of iteration, and the collection being filtered.

        • (arg0: any, arg1: number, arg2: Collection): boolean
        • Parameters

          Returns boolean

    Returns any[]

    An Array of matched values

  • forEach(fn: ((arg0: any) => void)): void
  • Apply a function to each element of the collection

    see

    Array#forEach

    example

    Apply a function to each value in the collection

    let c = new Collection([["a", {active: false}], ["b", {active: false}], ["c", {active: false}]]);
    c.forEach(e => e.active = true);

    Parameters

    • fn: ((arg0: any) => void)

      A function to apply to each element

        • (arg0: any): void
        • Parameters

          • arg0: any

          Returns void

    Returns void

  • get(key: string, [options]?: { strict: boolean }): any
  • Get an element from the Collection by its key.

    example

    Get an element from the Collection by key

    let c = new Collection([["a", "Alfred"], ["b", "Bob"], ["c", "Cynthia"]]);
    c.get("a"); // "Alfred"
    c.get("d"); // undefined
    c.get("d", {strict: true}); // throws Error

    Parameters

    • key: string

      The key of the entry to retrieve

    • [options]: { strict: boolean } = {}

      Additional options that affect how entries are retrieved

      • strict: boolean

    Returns any

    The retrieved entry value, if the key exists, otherwise undefined

  • getName(name: string, [options]?: { strict: boolean }): any
  • Get an entry from the Collection by name. Use of this method assumes that the objects stored in the collection have a "name" attribute.

    example

    Get an element from the Collection by name (if applicable)

    let c = new Collection([["a", "Alfred"], ["b", "Bob"], ["c", "Cynthia"]]);
    c.getName("Alfred"); // "Alfred"
    c.getName("D"); // undefined
    c.getName("D", {strict: true}); // throws Error

    Parameters

    • name: string

      The name of the entry to retrieve

    • [options]: { strict: boolean } = {}

      Additional options that affect how entries are retrieved

      • strict: boolean

    Returns any

    The retrieved entry value, if one was found, otherwise undefined

  • map(transformer: ((arg0: any, arg1: number, arg2: Collection) => any)): any[]
  • Transform each element of the Collection into a new form, returning an Array of transformed values

    Parameters

    • transformer: ((arg0: any, arg1: number, arg2: Collection) => any)

      A transformation function applied to each entry value. Positional arguments are the value, the index of iteration, and the collection being mapped.

        • Parameters

          Returns any

    Returns any[]

    An Array of transformed values

  • reduce(reducer: ((arg0: any, arg1: any, arg2: number, arg3: Collection) => any), initial: any): any
  • Reduce the Collection by applying an evaluator function and accumulating entries

    see

    {Array#reduce}

    example

    Reduce a collection to an array of transformed values

    let c = new Collection([["a", "A"], ["b", "B"], ["c", "C"]]);
    let letters = c.reduce((s, l) => {
    return s + l;
    }, ""); // "ABC"

    Parameters

    • reducer: ((arg0: any, arg1: any, arg2: number, arg3: Collection) => any)

      A reducer function applied to each entry value. Positional arguments are the accumulator, the value, the index of iteration, and the collection being reduced.

        • (arg0: any, arg1: any, arg2: number, arg3: Collection): any
        • Parameters

          Returns any

    • initial: any

      An initial value which accumulates with each iteration

    Returns any

    The accumulated result

  • some(condition: ((arg0: any, arg1: number, arg2: Collection) => boolean)): boolean
  • Test whether a condition is met by some entry in the Collection.

    see

    {Array#some}

    Parameters

    • condition: ((arg0: any, arg1: number, arg2: Collection) => boolean)

      The functional condition to test. Positional arguments are the value, the index of iteration, and the collection being tested.

        • (arg0: any, arg1: number, arg2: Collection): boolean
        • Parameters

          Returns boolean

    Returns boolean

    Was the test condition passed by at least one entry?

  • toJSON(): any[]
  • Convert the Collection to a primitive array of its contents.

    Returns any[]

    An array of contained values

  • [iterator](): IterableIterator<any>
  • When iterating over a Collection, we should iterate over its values instead of over its entries

    Returns IterableIterator<any>