Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Collection<T>

Type parameters

  • T

Hierarchy

  • Map<string | number, T>
    • Collection

Index

Constructors

constructor

  • new Collection(base?: AbstractClass | null, from?: T[] | Record<string | number | symbol, T> | null): Collection
  • Another way is to use base: new () => T or base: T&Function but this does not work with abstract classes.

    example
    const collection = new Collection<array>(null, ["foo", "bar", "baz"]);
    const collection = new Collection<array>(Array, ["foo", "bar", "baz"])
    const collection = new Collection<array>(Array)

    Parameters

    • Optional base: AbstractClass | null
    • Optional from: T[] | Record<string | number | symbol, T> | null

    Returns Collection

Properties

__@toStringTag

__@toStringTag: string

size

size: number

Static Map

Map: MapConstructor

Accessors

isEmpty

  • get isEmpty(): boolean
  • since

    0.4.0

    example
    const collection = Collection.from<string>(["foo", "bar", "baz"]);
    collection.isEmpty // => false

    Returns boolean

    true if collection is empty else false

Methods

__@iterator

  • __@iterator(): IterableIterator<[string | number, T]>
  • Returns an iterable of entries in the map.

    Returns IterableIterator<[string | number, T]>

add

  • add(v: T): void
  • since

    0.2.0

    Simple set function If v has an index named _key it will use it as the key

    example
    const collection = new Collection<string>(String);
    collection.add("foo");
    // Collection {
    //     0 => 'foo'
    // }

    Parameters

    • v: T

      Value to add to the collection

    Returns void

addMany

  • addMany(x: T[] | Record<string | number | symbol, T> | Collection<T>): void
  • since

    0.3.3

    since

    0.4.3 Added addMany from Collection

    Add multiple items at once to the collection

    example
    const collection = new Collection<string>(String);
    collection.addMany(["foo", "bar", "baz", "123"]);
    collection.addMany({ "foo": "bar", "baz": "123" });
    // Collection {
    //     0 => 'foo',
    //     1 => 'bar',
    //     2 => 'baz',
    //     3 => '123',
    //     'foo' => 'bar',
    //     'baz' => '123'
    // }

    Parameters

    • x: T[] | Record<string | number | symbol, T> | Collection<T>

      The array with items

    Returns void

all

  • all(fn: Predicate<T>): boolean
  • since

    0.4.0

    Returns true if all elements satisfy predicate.

    example
    const collection = Collection.from<string>(["foo", "bar", "baz"]);
    const result = collection.all((value) => typeof value === "string");
    // => true, all the items in the collection are strings

    Parameters

    • fn: Predicate<T>

      A function that returns a result

    Returns boolean

any

  • any(fn: Predicate<T> | null): boolean
  • since

    0.4.0

    Returns true if atleast one element satisfies the predicate.

    example
    const collection = Collection.from<string>(["foo", "bar", "baz"]);
    const result = collection.any((value) => value.includes("a"));
    // => true, collection has atleast 1 item that includes an "a"

    Parameters

    • fn: Predicate<T> | null

      A function that returns a result

    Returns boolean

clear

  • clear(): void
  • Returns void

contains

  • contains(element: T): boolean
  • since

    0.4.0

    Checks if element is in the collection

    example
    const collection = Collection.from<string>(["foo", "bar", "baz"]);
    const result = collection.contains("baz");
    // => true, collection has an item that is "baz"

    Parameters

    • element: T

      Element you want to check

    Returns boolean

    true if element is in collection else false

containsAll

  • since

    0.4.0

    Checks if all values from given collection are in this collection

    example
    const collection = Collection.from<string>(["foo", "bar", "baz"]);
    const collection2 = Collection.from<string>(["foo", "bar", "baz", "123"]);
    const result = collection.containsAll(collection2);
    // => false, 123 does not exist in collection

    Parameters

    • collection: Collection<T>

      The collection with values you want to check

    Returns boolean

    true if all values are in this collection else false

count

  • count(fn?: Predicate<T> | null): number
  • since

    0.4.0

    Gives the number of items in the collection, if predicate is given the number of items that evaluated true

    example
    const collection = Collection.from<string>(["foo", "bar", "baz"]);
    const result = collection.count((value) => value.includes("a"));
    // => 2, only 2 items include an "a"

    Parameters

    • Optional fn: Predicate<T> | null

      A function that returns a result

    Returns number

delete

  • delete(key: string | number): boolean
  • Parameters

    • key: string | number

    Returns boolean

entries

  • entries(): IterableIterator<[string | number, T]>
  • Returns an iterable of key, value pairs for every entry in the map.

    Returns IterableIterator<[string | number, T]>

filter

  • filter(fn: Predicate<T>): Result<T>[]
  • since

    0.1.0

    Returns an Array with all the elements that make the function evaluate true

    example
    const collection = new Collection<string>(String, ["foo", "bar", "baz", "123"]);
    const results = collection.filter((item) => item.includes("a"));
    console.log(results);
    // [
    //     { "key": 1, "value": "bar" },
    //     { "key": 2, "value": "baz" }
    // ]

    Parameters

    • fn: Predicate<T>

      A function that returns true if it matches the given param

    Returns Result<T>[]

    An array with all the elements that evaluated true

find

  • find(fn: Predicate<T>): Result<T> | undefined
  • since

    0.1.0

    Returns first matching Object or undefined if no match

    example
    const collection = new Collection<string>(String, ["foo", "bar", "baz", "123"]);
    const result = collection.find((item) => item === "foo");
    console.log(result);
    // { "key": 0, "value": "foo" }

    Parameters

    • fn: Predicate<T>

      A function that returns true if it matches the given param

    Returns Result<T> | undefined

    The first matching object or undefined if none found

forEach

  • forEach(callbackfn: function, thisArg?: any): void
  • Parameters

    • callbackfn: function
        • (value: T, key: string | number, map: Map<string | number, T>): void
        • Parameters

          • value: T
          • key: string | number
          • map: Map<string | number, T>

          Returns void

    • Optional thisArg: any

    Returns void

get

  • get(key: string | number): T | undefined
  • Parameters

    • key: string | number

    Returns T | undefined

has

  • has(key: string | number): boolean
  • Parameters

    • key: string | number

    Returns boolean

keys

  • keys(): IterableIterator<string | number>
  • Returns an iterable of keys in the map

    Returns IterableIterator<string | number>

map

  • map<R>(fn: function): R[]
  • since

    0.1.0

    Returns an Array with the results of applying the given function to each element

    example
    const collection = new Collection<string>(String, ["foo", "bar", "baz", "123"]);
    collection.map((v) => v.replace("a", "o"));
    // [
    //     "foo",
    //     "bor",
    //     "boz",
    //     "123"
    // ]

    Type parameters

    • R

    Parameters

    • fn: function

      A function that returns a result

        • Parameters

          Returns R

    Returns R[]

    An array with the results

merge

  • since

    0.1.0

    Merge two collections

    example
    const col1 = Collection.from<string>(["foo", "bar"]);
    const col2 = Collection.from<string>(["baz"]);
    
    col1.merge(col2);
    // Collection {
    //     0 => 'foo',
    //     1 => 'bar',
    //     2 => 'baz'
    // }

    Parameters

    • x: Collection<T>

      A collection to merge together with this

    Returns Collection<T>

    The merged collection

random

  • random(): T | undefined
  • since

    0.1.0

    Returns a random Object from the Collection or undefined if the Collection is empty

    example
    const collection = Collection.from<string>(["foo", "bar", "baz"]);
    collection.random(); // => will return 1 of foo, bar or baz

    Returns T | undefined

    The random object or undefined if none exist

remove

  • remove(item: T): void
  • since

    0.5.0

    Remove an item from the collection

    Parameters

    • item: T

      The item to delete

    Returns void

set

  • set(key: string | number, value: T): this
  • Parameters

    • key: string | number
    • value: T

    Returns this

toArray

  • toArray(): T[]
  • since

    0.4.0

    Convert collection values to array

    example
    const collection = Collection.from<string>(["foo", "bar", "baz"]);
    collection.asArray // => ["foo", "bar", "baz"]

    Returns T[]

toObject

  • toObject(): Record<string | number | symbol, T>
  • since

    0.4.0

    Convert collection to object

    example
    const collection = Collection.from<string>(["foo", "bar", "baz"]);
    collection.asObject // => {0: "foo", 1: "bar", 2: "baz"}

    Returns Record<string | number | symbol, T>

toString

  • toString(): string
  • since

    0.1.0

    example
    const collection = new Collection<string>(String, ["foo", "bar", "baz"]);
    collection.toString(); // => [Collection<String>]

    Returns string

values

  • values(): IterableIterator<T>
  • Returns an iterable of values in the map

    Returns IterableIterator<T>

Static from

  • from<T>(x: T[] | Record<string | number | symbol, T>): Collection<T>
  • since

    0.2.0

    since

    0.4.8 Add generic type to static method

    Create a Collection from an Array or Object

    example
    const col = Collection.from<string>(["foo", "bar", "baz"]);
    // Collection {
    //     0 => 'foo',
    //     1 => 'bar',
    //     2 => 'baz'
    // }

    Type parameters

    • T

    Parameters

    • x: T[] | Record<string | number | symbol, T>

      The array you want to create a collection from

    Returns Collection<T>

    The created collection

Static merge

  • since

    0.3.4

    since

    0.4.8 Add generic type to static method

    Merge multiple collections together

    example
    const col1 = new Collection<string>(String, ["foo", "bar"]);
    const col2 = new Collection<string>(String, ["baz"]);
    
    const col3 = Collection.merge(col1, col2);
    // Collection {
    //     0 => 'foo',
    //     1 => 'bar',
    //     2 => 'baz'
    // }

    Type parameters

    • T

    Parameters

    • Rest ...collections: Collection<T>[]

      All the collections you want to merge together

    Returns Collection<T>

Generated using TypeDoc