A collection class for storing and manipulating items.

Type Parameters

  • K extends CollectionKeyType = string

    The type of the collection keys.

  • V = unknown

    The type of the collection values.

Hierarchy

  • Collection

Implements

  • Iterable<[K, V]>

Constructors

Properties

items: Map<K, V>

The items contained in the collection

Methods

  • The Symbol.iterator method returns an iterator object for the items in the current object. This method is used to make the object iterable, allowing it to be used in the for…of loop.

    Returns Iterator<[K, V], any, undefined>

    An iterator object that can be used to iterate through the items in the object.

    Example

    const collection = new Collection([1, 2, 3]);
    for (const [key, value] of collection) {
    console.log(key, value);
    }
    // expected output: 0 1
    // expected output: 1 2
    // expected output: 2 3
  • Returns all the elements in the collection.

    Returns K extends number
        ? V[]
        : Map<K, V>

    If the collection is an array, an array of values, otherwise the map itself.

    Example

    const collection = new Collection([1, 2, 3]);
    collection.all(); // [1, 2, 3]
    // or
    const collection = new Collection({a: 1, b: 2, c: 3});
    collection.all(); // Map { 'a' => 1, 'b' => 2, 'c' => 3 }
  • Alias for avg

    Parameters

    • Optional callback: string | ((value) => string)

    Returns number

    See

    avg

  • Calculates the average value of the elements in the collection.

    Parameters

    • Optional callback: string | ((item) => unknown)

      An optional callback function or property name to retrieve a value from each element. If a callback function is provided, it will be called with each element in the collection and should return the value to use for averaging. If a string is provided, it will be treated as the property name to retrieve the value from each element.

    Returns number

    • The average value of the elements in the collection. If the collection is empty or no valid values can be calculated, NaN will be returned.

    Example

    const collection = new Collection([1, 2, 3]);
    collection.avg(); // 2
    // or
    const collection = new Collection([{a: 1}, {a: 2}, {a: 3}]);
    collection.avg('a'); // 2
    // or
    const collection = new Collection([{a: 1}, {a: 2}, {a: 3}]);
    collection.avg((item) => item.a); // 2
    // or
    const collection = new Collection([{a: 1}, {a: 2}, {a: 3}]);
    collection.avg((item) => item.b); // NaN
  • Collapses the values of the collection into a new collection.

    Returns Collection<number, unknown>

    A new Collection object containing the collapsed values.

    Example

    const collection = new Collection([[1, 2], [3, 4], [5, 6]]);
    collection.collapse(); // Collection { 0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, 5 => 6 }
  • Creates a new collection containing all the elements of the current collection.

    Returns Collection<K, V>

    A new collection with all the elements of the current collection.

    Example

    const collection = new Collection([1, 2, 3]);
    collection.collect(); // Collection { 0 => 1, 1 => 2, 2 => 3 }
  • Combines an array or Collection (as keys) with the items (as values) of the current Collection.

    Type Parameters

    • T

      The type of the values in the given array or Collection.

    Parameters

    • values: T[] | Collection<any, T>

      The array or Collection with the keys to be combined.

    Returns Collection<any, any>

    A new Collection with combined items (keys from values, values from existing items).

    Example

    const collection = new Collection([1, 2, 3]);
    collection.combine([4, 5, 6]); // Collection { 1 => 4, 2 => 5, 3 => 6 }
  • Concatenates the items from the given source collection to the current collection.

    Parameters

    Returns Collection<K, V>

    A new collection with the concatenated items.

    Example

    const collection = new Collection([1, 2, 3]);
    collection.concat([4, 5, 6]); // Collection { 0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, 5 => 6 }
  • Checks if the map contains a specific key, value, or a custom function.

    Parameters

    • key: K | V | ((value, key) => boolean)

      The key, value, or custom function to search for.

    • Optional operator: any

      Optional. The operator to be used for custom function operations.

    • Optional value: any

      Optional. The value to be used for comparison operations.

    Returns boolean

    • Returns true if the map contains the specified key, value, or custom function; otherwise, false.

    Example

    const collection = new Collection([1, 2, 3]);
    collection.contains(2); // true
    // or
    const collection = new Collection([1, 2, 3]);
    collection.contains((item) => item === 2); // true
    // or
    const collection = new Collection([1, 2, 3]);
    collection.contains(2, '===', 2); // true
  • Checks if the given array contains exactly one item.

    Returns boolean

    True if the array contains exactly one item, false otherwise.

    Example

    const collection = new Collection([1]);
    collection.containsOneItem(); // true
    // or
    const collection = new Collection([1, 2]);
    collection.containsOneItem(); // false
  • Determine if an item exists, using strict comparison.

    Parameters

    • key: V | ((item) => boolean)

      A function that returns a boolean type, a TValue, or an array key

    • value: any = null

      A TValue or null

    Returns boolean

    boolean

    Example

    const collection = new Collection([1, 2, 3]);
    collection.containsStrict(2); // true
    // or
    const collection = new Collection([1, 2, 3]);
    collection.containsStrict((item) => item === 2); // true
    // or
    const collection = new Collection([1, 2, 3]);
    collection.containsStrict(2, 2); // true
  • Returns the number of items in the count.

    Returns number

    The number of items in the count.

    Example

    const collection = new Collection([1, 2, 3]);
    collection.count(); // 3
  • Dumps the collection and throws an error to stop code execution.

    Returns void

    Throws

    Error message indicating code execution has stopped.

    Example

    const collection = new Collection([1, 2, 3]);
    collection.dd(); // Logs collection to console & Error: Stopping code execution from Collection dd()
  • Executes a debugger statement.

    Returns void

    Example

    const collection = new Collection([1, 2, 3]);
    collection.debugger(); // Debugger stops code execution
  • Calculates the difference between the current collection and the given collection.

    Parameters

    Returns Collection<K, V>

    A new collection containing the items that are present in the current collection but not in the given collection.

    Example

    const collection = new Collection([1, 2, 3]);
    collection.diff([1, 2, 4]); // Collection { 2 => 3 }
  • Calculates the difference between this collection and the given collection based on key-value associations. Returns a new Collection object that contains all key-value pairs from this collection, which are not present in the given collection.

    Parameters

    Returns Collection<K, V>

    A new Collection object containing the key-value pairs that are in this collection but not in the given collection.

    Example

    const collection = new Collection({a: 1, b: 2, c: 3});
    collection.diffAssoc({a: 1, b: 2, c: 4, d: 5}); // Collection { 'c' => 3 }
  • Returns a new Collection that contains only the key-value pairs whose keys are not present in the provided items.

    Parameters

    Returns Collection<K, V>

    A new Collection instance that contains key-value pairs whose keys are not present in the provided items.

    Example

    const collection = new Collection({a: 1, b: 2, c: 3});
    collection.diffKeys({a: 1, c: 4, d: 5}); // Collection { 'b' => 2 }
  • Checks if the item does not exist in the collection.

    Parameters

    • item: K | V | ((value, key) => boolean)

      The item to check for existence.

    • Optional operator: any

      Optional operator parameter.

    • Optional value: any

      Optional value parameter.

    Returns boolean

    Returns true if the item does not exist in the collection, otherwise returns false.

    Example

    const collection = new Collection([1, 2, 3]);
    collection.doesntContain(2); // false
    // or
    const collection = new Collection([1, 2, 3]);
    collection.doesntContain((item) => item === 2); // false
    // or
    const collection = new Collection([1, 2, 3]);
    collection.doesntContain(2, '===', 2); // false
  • Dumps the current object to the console.

    Returns void

  • Execute a callback over each item.

    Parameters

    • callback: ((item, key) => unknown)

      The callback function to be applied to each element. The callback should accept two parameters: item and key. The item parameter represents the value of the current element. The key parameter represents the key of the current element.

    Returns Collection<K, V>

    Returns the collection itself.

    Example

    const collection = new Collection([1, 2, 3]);
    collection.each((item, key) => console.log(item, key)); // 1 0, 2 1, 3 2
  • Checks whether every element in the map satisfies the given condition.

    Parameters

    • key: ((value, key) => boolean | V)

      The function used to test each element in the map.

        • (value, key): boolean | V
        • Parameters

          • value: V
          • key: K

          Returns boolean | V

    • Optional operator: unknown

      Optional operator argument.

    • Optional value: unknown

      Optional value argument.

    Returns boolean

    true if every element satisfies the condition, otherwise false.

    Example

    const collection = new Collection([1, 2, 3]);
    collection.every((item) => item > 0); // true
    // or
    const collection = new Collection([1, 2, 3]);
    collection.every((item) => item > 1); // false
    // or
    const collection = new Collection([1, 2, 3]);
    collection.every((item) => item > 1, '===', 2); // false
  • Filters the entries of the collection based on the provided callback function. It creates and returns a new Collection instance containing the filtered entries.

    Parameters

    • Optional callback: ((value, key) => boolean)

      The callback function used to filter each entry. The function takes two parameters: the value and key of each entry. It should return true to keep the entry or false to remove it.

        • (value, key): boolean
        • Parameters

          • value: V
          • key: K

          Returns boolean

    Returns Collection<K, V>

    A new Collection instance with the filtered entries.

    Example

    const collection = new Collection([1, 2, 3]);
    collection.filter((item) => item > 1); // Collection { 1 => 2, 2 => 3 }
  • Retrieves the first value that matches the provided callback function. If no callback function is provided, returns the first value in the collection. If no values are in the collection, returns the default value.

    Type Parameters

    • D

      The type of the default value.

    Parameters

    • Optional callback: ((value, key) => boolean)

      The callback function used to match the values. The function should accept two arguments: the current value and the current key. It should return a boolean value indicating whether the value matches the criterion.

        • (value, key): boolean
        • Parameters

          • value: V
          • key: K

          Returns boolean

    • Optional defaultValue: D | (() => D)

      The default value to return if no match is found. If a function is provided, it will be executed to generate the default value.

    Returns undefined | V | D

    The first value in the collection that matches the callback function, or the default value if no match is found.

    Example

    const collection = new Collection({key1: 'value', key2: 'value2'});

    // Example with callback function
    const result1 = collection.first((value, key) => key === "key2");
    console.log(result1); // Output: "value2"

    // Example without callback function
    const result2 = collection.first();
    console.log(result2); // Output: "value1"

    // Example with default value
    const result3 = collection.first('key3', "default");
    console.log(result3); // Output: "default"
  • Flattens the collection to a one-dimensional array.

    Parameters

    • depth: number = Infinity

      The depth to which the collection should be flattened. Defaults to Infinity.

    Returns Collection<number, unknown>

    A new Collection object with the flattened array.

    Example

    const collection = new Collection([[1, 2], [3, 4], [5, 6]]);
    collection.flatten(); // Collection { 0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, 5 => 6 }
  • Retrieves the value associated with the specified key from the collection. If the key is found, the corresponding value is returned. If the key is not found, the fallback value is returned.

    Type Parameters

    • D

      The type of the fallback value.

    Parameters

    • key: K

      The key to retrieve the value for.

    • Optional fallback: D

      The fallback value to return if the key is not found. Default is undefined.

    Returns undefined | V | D

    The value associated with the key, or the fallback value if the key is not found.

    Example

    const collection = new Collection([1, 2, 3]);
    collection.get(2); // 3
    // or
    const collection = new Collection({a: 1, b: 2, c: 3});
    collection.get('b'); // 2
    // or
    const collection = new Collection([1, 2, 3]);
    collection.get(4, 'default'); // 'default'
  • Protected

    Returns a Map object containing items suitable for iteration.

    Parameters

    Returns Map<K, V>

    A Map object containing the objectable items.

  • Checks if all the specified keys are present in the items map.

    Parameters

    • Rest ...keys: K[]

      The keys to check for presence in the items map.

    Returns boolean

    Returns true if all the keys are present, false otherwise.

    Example

    const collection = new Collection([1, 3, 5]);
    collection.has(2); // true
    // or
    const collection = new Collection({a: 1, b: 2, c: 3});
    collection.has('a'); // true
  • Protected

    Checks whether the object is an array.

    Returns boolean

    Returns true if the object is an array, otherwise false.

  • Returns a collection of keys for the current instance.

    Returns Collection<number, K>

    A collection of keys for the current instance.

    Example

    const collection = new Collection({a: 1, b: 2, c: 3});
    collection.keys(); // Collection { 0 => 'a', 1 => 'b', 2 => 'c' }
    // or
    const collection = new Collection([1, 2, 3]);
    collection.keys(); // Collection { 0 => 0, 1 => 1, 2 => 2 }
  • Returns the last value in the collection that satisfies the provided callback function. If no callback function is provided, it returns the last value in the collection. If the collection is empty or the callback function doesn't find a match, it returns the defaultValue.

    Type Parameters

    • D

      The type of the default value.

    Parameters

    • Optional callback: ((value, key) => boolean)

      The callback function used to determine if a value satisfies a condition. It should return true if the value satisfies the condition, otherwise false.

        • (value, key): boolean
        • Parameters

          • value: V
          • key: K

          Returns boolean

    • Optional defaultValue: D

      The value to return if no match is found. Defaults to undefined.

    Returns undefined | {} | V | D

    The last value that satisfies the callback function, or the defaultValue if no match is found.

    Example

    const collection = new Collection([1, 2, 3]);
    collection.last((item) => item > 1); // 3
    // or
    const collection = new Collection([1, 2, 3]);
    collection.last((item) => item > 3, 'default'); // 'default'
    // or
    const collection = new Collection([1, 2, 3]);
    collection.last(); // 3
  • Maps each key-value pair in the Collection object to a new value using a provided callback function.

    Type Parameters

    • T

      The type of the new values.

    Parameters

    • callback: ((item, key) => T)

      The function to be applied to each key-value pair. It should take two arguments: the value of the current key-value pair and the key of the current key-value pair.

        • (item, key): T
        • Parameters

          • item: V
          • key: K

          Returns T

    Returns Collection<K, T>

    A new Collection object with the same keys as the original object, but with the values transformed by the callback function.

    Example

    const collection = new Collection([1, 2, 3]);
    collection.map((item) => item * 2); // Collection { 0 => 2, 1 => 4, 2 => 6 }
    // or
    const collection = new Collection({a: 1, b: 2, c: 3});
    collection.map((item) => item * 2); // Collection { 'a' => 2, 'b' => 4, 'c' => 6 }
  • Calculates the median value from an array of numbers.

    Parameters

    • Optional key: K

      Optional. The key to use for extracting values from objects in the array. If provided, the method will first use this.pluck(key) to extract values.

    Returns number

    The median value. If the array is empty, returns NaN.

    Example

    const collection = new Collection([1, 2, 3]);
    collection.median(); // 2
    // or
    const collection = new Collection([{a: 1}, {a: 2}, {a: 3}]);
    collection.median('a'); // 2
  • Returns an array containing the keys of the elements with the highest occurrence in the collection. If a key parameter is provided, mode returns the keys of the elements with the highest occurrence for that specific key.

    Parameters

    • Optional key: K

      Optional. The key parameter to filter the collection by.

    Returns number[]

    An array containing the keys of the elements with the highest occurrence in the collection. Returns an empty array if the collection is empty.

    Example

    const collection = new Collection([1, 2, 2, 3, 3, 3]);
    collection.mode(); // [3]
    // or
    const collection = new Collection([{a: 1}, {a: 2}, {a: 2}, {a: 3}, {a: 3}, {a: 3}]);
    collection.mode('a'); // [3]
  • Protected

    Returns a function that can be used as a filter in array methods like Array.filter().

    Parameters

    • key: any

      The key or function to use for comparison.

    • Optional operator: string

      The comparison operator.

    • Optional value: any

      The value to compare against.

    Returns any

    The filter function.

  • Retrieves values from a collection based on a given key.

    Parameters

    • value: string | number

      The key to retrieve values for. Can be a string or number.

    • Optional key: string | number

      Optional key to use for indexing the values. Can be a string or number.

    Returns Collection<string, unknown>

    A new Collection containing the retrieved values.

    Example

    const collection = new Collection([{a: 1}, {a: 2}, {a: 3}]);
    collection.pluck('a'); // Collection { 0 => 1, 1 => 2, 2 => 3 }
    // or
    const collection = new Collection([{a: 1, b: 1}, {a: 2, b: 2}, {a: 3, b: 3}]);
    collection.pluck('a', 'b'); // Collection { 1 => 1, 2 => 2, 3 => 3 }
  • Adds one or more items to the collection.

    Parameters

    • Rest ...items: V[]

      The items to add to the collection.

    Returns Collection<K, V>

    The updated collection.

    Example

    const collection = new Collection([1, 2, 3]);
    collection.push(4, 5, 6); // Collection { 0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, 5 => 6 }
  • Puts a new key-value pair into the map.

    Parameters

    • key: undefined | K

      The key for the new entry. If the key is undefined it fallbacks to the push method.

    • newValue: V

      The value for the new entry.

    Returns Collection<K, V>

    This collection instance, after the new entry is added.

    Example

    const collection = new Collection({a: 1, b: 2, c: 3});
    collection.put('d', 4); // Collection { 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4 }
    // or
    const collection = new Collection([1, 2, 3]);
    collection.put(undefined, 4); // Collection { 0 => 1, 1 => 2, 2 => 3, 3 => 4 }
  • Applies a callback function to each item in the map and reduces them to a single value.

    Type Parameters

    • R

      The type of the reduced value.

    Parameters

    • callback: ((result, item, key) => R)

      The function to execute on each item.

        • (result, item, key): R
        • Parameters

          • result: null | R
          • item: V
          • key: K

          Returns R

    • initial: null | R = null

      The initial value for the reduction.

    Returns null | R

    The reduced value.

    Example

    const collection = new Collection([1, 2, 3]);
    collection.reduce((result, item) => result + item); // 6
    // or
    const collection = new Collection([1, 2, 3]);
    collection.reduce((result, item) => result + item, 10); // 16
  • Reverses the order of the items in the collection.

    Returns Collection<K, V>

    A new collection with the items in reverse order.

    Example

    const collection = new Collection([1, 2, 3]);
    collection.reverse(); // Collection { 0 => 3, 1 => 2, 2 => 1 }
    // or
    const collection = new Collection({a: 1, b: 2, c: 3});
    collection.reverse(); // Collection { 'c' => 3, 'b' => 2, 'a' => 1 }
  • Sorts the elements of the collection.

    Parameters

    • Optional callback: ((a, b) => number)

      (Optional) A callback function to determine the sort order. If provided, the function should accept two elements from the collection and return a negative number if the first element should be placed before the second element, 0 if the order should remain unchanged, or a positive number if the second element should be placed before the first element. If not provided, the default sorting order is used.

        • (a, b): number
        • Parameters

          • a: V
          • b: V

          Returns number

    Returns Collection<K, V>

    A new Collection with the elements sorted.

    Example

    const collection = new Collection([3, 2, 1]);
    collection.sort(); // Collection { 0 => 1, 1 => 2, 2 => 3 }
    // or
    const collection = new Collection([3, 2, 1]);
    collection.sort((a, b) => b - a); // Collection { 0 => 3, 1 => 2, 2 => 1 }
    // or
    const collection = new Collection([{a: 3}, {a: 2}, {a: 1}]);
    collection.sort((a, b) => a.a - b.a); // Collection { 0 => { a: 1 }, 1 => { a: 2 }, 2 => { a: 3 } }
    // or
    const collection = new Collection([{a: 3}, {a: 2}, {a: 1}]);
    collection.sort(); // Collection { 0 => { a: 1 }, 1 => { a: 2 }, 2 => { a: 3 } }
  • Calculates the sum of the elements in the array.

    Parameters

    • Optional mapper: string | ((item) => number)

      Optional. A callback function or property name for mapping each value before performing the sum. If specified, the mapper function will be applied to each element in the array before summing. If a property name is provided, it will be used as a string accessor to retrieve the corresponding value from each element .

    Returns number

    The sum of the elements in the array.

    Example

    const collection = new Collection([1, 2, 3]);
    collection.sum(); // 6
    // or
    const collection = new Collection([{a: 1}, {a: 2}, {a: 3}]);
    collection.sum('a'); // 6
    // or
    const collection = new Collection([{a: 1}, {a: 2}, {a: 3}]);
    collection.sum((item) => item.a); // 6
  • Convert the collection to an array.

    Returns V[]

    The collection values as an array.

    Example

    const collection = new Collection([1, 2, 3]);
    collection.toArray(); // [1, 2, 3]
    // or
    const collection = new Collection({a: 1, b: 2, c: 3});
    collection.toArray(); // [1, 2, 3]
  • Converts the items of this object into a Map.

    Returns Map<K, V>

    A new Map object containing the items of this object.

    Example

    const collection = new Collection([1, 2, 3]);
    collection.toMap(); // Map { 0 => 1, 1 => 2, 2 => 3 }
    // or
    const collection = new Collection({a: 1, b: 2, c: 3});
    collection.toMap(); // Map { 'a' => 1, 'b' => 2, 'c' => 3 }
  • Converts the instance of the class to a plain object.

    Returns Record<K, V>

    The plain object representation of the class.

    Example

    const collection = new Collection([1, 2, 3]);
    collection.toObject(); // { '0': 1, '1': 2, '2': 3 }
    // or
    const collection = new Collection({a: 1, b: 2, c: 3});
    collection.toObject(); // { a: 1, b: 2, c: 3 }
  • Protected

    Retrieves the value from an item using a callback function or a property path.

    Parameters

    • Optional callback: string | Function

      The callback function or property path used to retrieve the value from the item.

    Returns Function

    The callback function to retrieve the value from

  • Returns a new Collection of values from the underlying items.

    Returns Collection<number, V>

    A new Collection of values.

    Example

    const collection = new Collection([1, 2, 3]);
    collection.values(); // Collection { 0 => 1, 1 => 2, 2 => 3 }
    // or
    const collection = new Collection({a: 1, b: 2, c: 3});
    collection.values(); // Collection { 'a' => 1, 'b' => 2, 'c' => 3 }
  • Create a new Collection instance.

    Type Parameters

    Parameters

    Returns Collection<K, V>

    The newly created Collection instance.

    Example

    const collection = Collection.make([1, 2, 3]); // Collection { 0 => 1, 1 => 2, 2 => 3 }
    

Generated using TypeDoc