Class LRUCollection<K, V>

A LRUCache with additional utility methods. Inspired and built with Collection, the data structure from discord.js.

Type Parameters

  • K extends NonNullable<unknown>

    The key type the cache holds

  • V extends NonNullable<unknown>

    The value type the cache holds

Hierarchy

Constructors

  • Type Parameters

    • K extends {}

    • V extends {}

    Parameters

    • Optional entries: null | readonly (readonly [K, V])[]

    Returns LRUCollection<K, V>

  • Type Parameters

    • K extends {}

    • V extends {}

    Parameters

    • Optional iterable: null | Iterable<readonly [K, V]>

    Returns LRUCollection<K, V>

Properties

[toStringTag]: string
size: number

Returns

the number of elements in the Map.

[species]: MapConstructor

Accessors

  • get max(): number
  • Returns number

  • set max(max): void
  • Parameters

    • max: number

      The maximum number of entries the lru cache should hold

    Returns void

Methods

  • Returns an iterable of entries in the map.

    Returns IterableIterator<[K, V]>

  • Identical to Array.at(). Returns the item at a given index, allowing for positive and negative integers. Negative integers count back from the last item in the collection.

    Parameters

    • index: number

      The index of the element to obtain

    Returns undefined | V

  • Returns void

  • Creates an identical shallow copy of this collection.

    Example

    const newColl = someColl.clone();
    

    Returns DCollection<K, V>

  • Combines this collection with others into a new collection. None of the source collections are modified.

    Example

    const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
    

    Parameters

    Returns DCollection<K, V>

  • Parameters

    • key: K

    Returns boolean

    true if an element in the Map existed and has been removed, or false if the element does not exist.

  • The difference method returns a new structure containing items where the key is present in one of the original structures but not the other.

    Type Parameters

    • T

    Parameters

    Returns DCollection<K, V | T>

  • Identical to Map.forEach(), but returns the collection instead of undefined.

    Example

    collection
    .each(user => console.log(user.username))
    .filter(user => user.bot)
    .each(user => console.log(user.username));

    Parameters

    • fn: ((value, key, collection) => void)

      Function to execute for each element

        • (value, key, collection): void
        • Parameters

          Returns void

    Returns LRUCollection<K, V>

  • Type Parameters

    • T

    Parameters

    • fn: ((this, value, key, collection) => void)
        • (this, value, key, collection): void
        • Parameters

          Returns void

    • thisArg: T

    Returns LRUCollection<K, V>

  • Obtains the value of the given key if it exists, otherwise sets and returns the value provided by the default value generator.

    Example

    collection.ensure(guildId, () => defaultGuildConfig);
    

    Parameters

    • key: K

      The key to get if it exists, or set otherwise

    • defaultValueGenerator: ((key, collection) => V)

      A function that generates the default value

        • (key, collection): V
        • Parameters

          Returns V

    Returns V

  • Returns an iterable of key, value pairs for every entry in the map.

    Returns IterableIterator<[K, V]>

  • Checks if this collection shares identical items with another. This is different to checking for equality using equal-signs, because the collections may be different objects, but contain the same data.

    Parameters

    Returns boolean

    Whether the collections have identical contents

  • Checks if all items passes a test. Identical in behavior to Array.every().

    Example

    collection.every(user => !user.bot);
    

    Type Parameters

    • K2 extends {}

    Parameters

    • fn: ((value, key, collection) => key is K2)

      Function used to test (should return a boolean)

        • (value, key, collection): key is K2
        • Parameters

          Returns key is K2

    Returns this is DCollection<K2, V>

  • Type Parameters

    • V2 extends {}

    Parameters

    • fn: ((value, key, collection) => value is V2)
        • (value, key, collection): value is V2
        • Parameters

          Returns value is V2

    Returns this is DCollection<K, V2>

  • Parameters

    • fn: ((value, key, collection) => unknown)
        • (value, key, collection): unknown
        • Parameters

          Returns unknown

    Returns boolean

  • Type Parameters

    • This

    • K2 extends {}

    Parameters

    • fn: ((this, value, key, collection) => key is K2)
        • (this, value, key, collection): key is K2
        • Parameters

          Returns key is K2

    • thisArg: This

    Returns this is DCollection<K2, V>

  • Type Parameters

    • This

    • V2 extends {}

    Parameters

    • fn: ((this, value, key, collection) => value is V2)
        • (this, value, key, collection): value is V2
        • Parameters

          Returns value is V2

    • thisArg: This

    Returns this is DCollection<K, V2>

  • Type Parameters

    • This

    Parameters

    • fn: ((this, value, key, collection) => unknown)
        • (this, value, key, collection): unknown
        • Parameters

          Returns unknown

    • thisArg: This

    Returns boolean

  • Identical to Array.filter(), but returns a Collection instead of an Array.

    Example

    collection.filter(user => user.username === 'Bob');
    

    Type Parameters

    • K2 extends {}

    Parameters

    • fn: ((value, key, collection) => key is K2)

      The function to test with (should return boolean)

        • (value, key, collection): key is K2
        • Parameters

          Returns key is K2

    Returns DCollection<K2, V>

  • Type Parameters

    • V2 extends {}

    Parameters

    • fn: ((value, key, collection) => value is V2)
        • (value, key, collection): value is V2
        • Parameters

          Returns value is V2

    Returns DCollection<K, V2>

  • Parameters

    • fn: ((value, key, collection) => unknown)
        • (value, key, collection): unknown
        • Parameters

          Returns unknown

    Returns DCollection<K, V>

  • Type Parameters

    • This

    • K2 extends {}

    Parameters

    • fn: ((this, value, key, collection) => key is K2)
        • (this, value, key, collection): key is K2
        • Parameters

          Returns key is K2

    • thisArg: This

    Returns DCollection<K2, V>

  • Type Parameters

    • This

    • V2 extends {}

    Parameters

    • fn: ((this, value, key, collection) => value is V2)
        • (this, value, key, collection): value is V2
        • Parameters

          Returns value is V2

    • thisArg: This

    Returns DCollection<K, V2>

  • Type Parameters

    • This

    Parameters

    • fn: ((this, value, key, collection) => unknown)
        • (this, value, key, collection): unknown
        • Parameters

          Returns unknown

    • thisArg: This

    Returns DCollection<K, V>

  • Searches for a single item where the given function returns a truthy value. This behaves like Array.find(). All collections used in Discord.js are mapped using their id property, and if you want to find by id you should use the get method. See MDN for details.

    Example

    collection.find(user => user.username === 'Bob');
    

    Type Parameters

    • V2 extends {}

    Parameters

    • fn: ((value, key, collection) => value is V2)

      The function to test with (should return boolean)

        • (value, key, collection): value is V2
        • Parameters

          Returns value is V2

    Returns undefined | V2

  • Parameters

    • fn: ((value, key, collection) => unknown)
        • (value, key, collection): unknown
        • Parameters

          Returns unknown

    Returns undefined | V

  • Type Parameters

    • This

    • V2 extends {}

    Parameters

    • fn: ((this, value, key, collection) => value is V2)
        • (this, value, key, collection): value is V2
        • Parameters

          Returns value is V2

    • thisArg: This

    Returns undefined | V2

  • Type Parameters

    • This

    Parameters

    • fn: ((this, value, key, collection) => unknown)
        • (this, value, key, collection): unknown
        • Parameters

          Returns unknown

    • thisArg: This

    Returns undefined | V

  • Searches for the key of a single item where the given function returns a truthy value. This behaves like Array.findIndex(), but returns the key rather than the positional index.

    Example

    collection.findKey(user => user.username === 'Bob');
    

    Type Parameters

    • K2 extends {}

    Parameters

    • fn: ((value, key, collection) => key is K2)

      The function to test with (should return boolean)

        • (value, key, collection): key is K2
        • Parameters

          Returns key is K2

    Returns undefined | K2

  • Parameters

    • fn: ((value, key, collection) => unknown)
        • (value, key, collection): unknown
        • Parameters

          Returns unknown

    Returns undefined | K

  • Type Parameters

    • This

    • K2 extends {}

    Parameters

    • fn: ((this, value, key, collection) => key is K2)
        • (this, value, key, collection): key is K2
        • Parameters

          Returns key is K2

    • thisArg: This

    Returns undefined | K2

  • Type Parameters

    • This

    Parameters

    • fn: ((this, value, key, collection) => unknown)
        • (this, value, key, collection): unknown
        • Parameters

          Returns unknown

    • thisArg: This

    Returns undefined | K

  • Obtains the first value(s) in this collection.

    Returns undefined | V

    A single value if no amount is provided or an array of values, starting from the end if amount is negative

  • Parameters

    • amount: number

    Returns V[]

  • Obtains the first key(s) in this collection.

    Returns undefined | K

    A single key if no amount is provided or an array of keys, starting from the end if amount is negative

  • Parameters

    • amount: number

    Returns K[]

  • Maps each item into a Collection, then joins the results into a single Collection. Identical in behavior to Array.flatMap().

    Example

    collection.flatMap(guild => guild.members.cache);
    

    Type Parameters

    • T

    Parameters

    Returns DCollection<K, T>

  • Type Parameters

    • T

    • This

    Parameters

    Returns DCollection<K, T>

  • Executes a provided function once per each key/value pair in the Map, in insertion order.

    Parameters

    • callbackfn: ((value, key, map) => void)
        • (value, key, map): void
        • Parameters

          • value: V
          • key: K
          • map: Map<K, V>

          Returns void

    • Optional thisArg: any

    Returns void

  • Returns a specified element from the Map object. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Map.

    Parameters

    • key: K

    Returns undefined | V

    Returns the element associated with the specified key. If no element is associated with the specified key, undefined is returned.

  • Parameters

    • key: K

    Returns boolean

    boolean indicating whether an element with the specified key exists or not.

  • Checks if all of the elements exist in the collection.

    Parameters

    • Rest ...keys: K[]

      The keys of the elements to check for

    Returns boolean

    true if all of the elements exist, false if at least one does not exist.

  • Checks if any of the elements exist in the collection.

    Parameters

    • Rest ...keys: K[]

      The keys of the elements to check for

    Returns boolean

    true if any of the elements exist, false if none exist.

  • The intersect method returns a new structure containing items where the keys and values are present in both original structures.

    Type Parameters

    • T

    Parameters

    Returns DCollection<K, T>

  • Identical to Array.at(). Returns the key at a given index, allowing for positive and negative integers. Negative integers count back from the last item in the collection.

    Parameters

    • index: number

      The index of the key to obtain

    Returns undefined | K

  • Returns an iterable of keys in the map

    Returns IterableIterator<K>

  • Obtains the last value(s) in this collection.

    Returns undefined | V

    A single value if no amount is provided or an array of values, starting from the start if amount is negative

  • Parameters

    • amount: number

    Returns V[]

  • Obtains the last key(s) in this collection.

    Returns undefined | K

    A single key if no amount is provided or an array of keys, starting from the start if amount is negative

  • Parameters

    • amount: number

    Returns K[]

  • Maps each item to another value into an array. Identical in behavior to Array.map().

    Example

    collection.map(user => user.tag);
    

    Type Parameters

    • T

    Parameters

    • fn: ((value, key, collection) => T)

      Function that produces an element of the new array, taking three arguments

        • (value, key, collection): T
        • Parameters

          Returns T

    Returns T[]

  • Type Parameters

    • This

    • T

    Parameters

    • fn: ((this, value, key, collection) => T)
        • (this, value, key, collection): T
        • Parameters

          Returns T

    • thisArg: This

    Returns T[]

  • Maps each item to another value into a collection. Identical in behavior to Array.map().

    Example

    collection.mapValues(user => user.tag);
    

    Type Parameters

    • T

    Parameters

    • fn: ((value, key, collection) => T)

      Function that produces an element of the new collection, taking three arguments

        • (value, key, collection): T
        • Parameters

          Returns T

    Returns DCollection<K, T>

  • Type Parameters

    • This

    • T

    Parameters

    • fn: ((this, value, key, collection) => T)
        • (this, value, key, collection): T
        • Parameters

          Returns T

    • thisArg: This

    Returns DCollection<K, T>

  • Merges two Collections together into a new Collection.

    Example

    // Sums up the entries in two collections.
    coll.merge(
    other,
    x => ({ keep: true, value: x }),
    y => ({ keep: true, value: y }),
    (x, y) => ({ keep: true, value: x + y }),
    );

    Example

    // Intersects two collections in a left-biased manner.
    coll.merge(
    other,
    x => ({ keep: false }),
    y => ({ keep: false }),
    (x, _) => ({ keep: true, value: x }),
    );

    Type Parameters

    • T

    • R

    Parameters

    • other: DReadonlyCollection<K, T>

      The other Collection to merge with

    • whenInSelf: ((value, key) => Keep<R>)

      Function getting the result if the entry only exists in this Collection

        • (value, key): Keep<R>
        • Parameters

          • value: V
          • key: K

          Returns Keep<R>

    • whenInOther: ((valueOther, key) => Keep<R>)

      Function getting the result if the entry only exists in the other Collection

        • (valueOther, key): Keep<R>
        • Parameters

          • valueOther: T
          • key: K

          Returns Keep<R>

    • whenInBoth: ((value, valueOther, key) => Keep<R>)

      Function getting the result if the entry exists in both Collections

        • (value, valueOther, key): Keep<R>
        • Parameters

          • value: V
          • valueOther: T
          • key: K

          Returns Keep<R>

    Returns DCollection<K, R>

  • Partitions the collection into two collections where the first collection contains the items that passed and the second contains the items that failed.

    Example

    const [big, small] = collection.partition(guild => guild.memberCount > 250);
    

    Type Parameters

    • K2 extends {}

    Parameters

    • fn: ((value, key, collection) => key is K2)

      Function used to test (should return a boolean)

        • (value, key, collection): key is K2
        • Parameters

          Returns key is K2

    Returns [DCollection<K2, V>, DCollection<Exclude<K, K2>, V>]

  • Type Parameters

    • V2 extends {}

    Parameters

    • fn: ((value, key, collection) => value is V2)
        • (value, key, collection): value is V2
        • Parameters

          Returns value is V2

    Returns [DCollection<K, V2>, DCollection<K, Exclude<V, V2>>]

  • Parameters

    • fn: ((value, key, collection) => unknown)
        • (value, key, collection): unknown
        • Parameters

          Returns unknown

    Returns [DCollection<K, V>, DCollection<K, V>]

  • Type Parameters

    • This

    • K2 extends {}

    Parameters

    • fn: ((this, value, key, collection) => key is K2)
        • (this, value, key, collection): key is K2
        • Parameters

          Returns key is K2

    • thisArg: This

    Returns [DCollection<K2, V>, DCollection<Exclude<K, K2>, V>]

  • Type Parameters

    • This

    • V2 extends {}

    Parameters

    • fn: ((this, value, key, collection) => value is V2)
        • (this, value, key, collection): value is V2
        • Parameters

          Returns value is V2

    • thisArg: This

    Returns [DCollection<K, V2>, DCollection<K, Exclude<V, V2>>]

  • Type Parameters

    • This

    Parameters

    • fn: ((this, value, key, collection) => unknown)
        • (this, value, key, collection): unknown
        • Parameters

          Returns unknown

    • thisArg: This

    Returns [DCollection<K, V>, DCollection<K, V>]

  • Obtains unique random value(s) from this collection.

    Returns undefined | V

    A single value if no amount is provided or an array of values

  • Parameters

    • amount: number

    Returns V[]

  • Obtains unique random key(s) from this collection.

    Returns undefined | K

    A single key if no amount is provided or an array

  • Parameters

    • amount: number

    Returns K[]

  • Applies a function to produce a single value. Identical in behavior to Array.reduce().

    Example

    collection.reduce((acc, guild) => acc + guild.memberCount, 0);
    

    Type Parameters

    • T

    Parameters

    • fn: ((accumulator, value, key, collection) => T)

      Function used to reduce, taking four arguments; accumulator, currentValue, currentKey, and collection

        • (accumulator, value, key, collection): T
        • Parameters

          Returns T

    • Optional initialValue: T

      Starting value for the accumulator

    Returns T

  • Adds a new element with a specified key and value to the Map. If an element with the same key already exists, the element will be updated.

    Parameters

    • key: K
    • value: V

    Returns LRUCollection<K, V>

  • Checks if there exists an item that passes a test. Identical in behavior to Array.some().

    Example

    collection.some(user => user.discriminator === '0000');
    

    Parameters

    • fn: ((value, key, collection) => unknown)

      Function used to test (should return a boolean)

        • (value, key, collection): unknown
        • Parameters

          Returns unknown

    Returns boolean

  • Type Parameters

    • T

    Parameters

    • fn: ((this, value, key, collection) => unknown)
        • (this, value, key, collection): unknown
        • Parameters

          Returns unknown

    • thisArg: T

    Returns boolean

  • The sort method sorts the items of a collection in place and returns it. The sort is not necessarily stable in Node 10 or older. The default sort order is according to string Unicode code points.

    Example

    collection.sort((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
    

    Parameters

    • Optional compareFunction: Comparator<K, V>

      Specifies a function that defines the sort order. If omitted, the collection is sorted according to each character's Unicode code point value, according to the string conversion of each element.

    Returns LRUCollection<K, V>

  • The sorted method sorts the items of a collection and returns it. The sort is not necessarily stable in Node 10 or older. The default sort order is according to string Unicode code points.

    Example

    collection.sorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
    

    Parameters

    • Optional compareFunction: Comparator<K, V>

      Specifies a function that defines the sort order. If omitted, the collection is sorted according to each character's Unicode code point value, according to the string conversion of each element.

    Returns DCollection<K, V>

  • The subtract method returns a new structure containing items where the keys and values of the original structure are not present in the other.

    Type Parameters

    • T

    Parameters

    Returns DCollection<K, V>

  • Removes items that satisfy the provided filter function.

    Parameters

    • fn: ((value, key, collection) => unknown)

      Function used to test (should return a boolean)

        • (value, key, collection): unknown
        • Parameters

          Returns unknown

    Returns number

    The number of removed entries

  • Type Parameters

    • T

    Parameters

    • fn: ((this, value, key, collection) => unknown)
        • (this, value, key, collection): unknown
        • Parameters

          Returns unknown

    • thisArg: T

    Returns number

  • Runs a function on the collection and returns the collection.

    Example

    collection
    .tap(coll => console.log(coll.size))
    .filter(user => user.bot)
    .tap(coll => console.log(coll.size))

    Parameters

    • fn: ((collection) => void)

      Function to execute

        • (collection): void
        • Parameters

          Returns void

    Returns LRUCollection<K, V>

  • Type Parameters

    • T

    Parameters

    • fn: ((this, collection) => void)
        • (this, collection): void
        • Parameters

          Returns void

    • thisArg: T

    Returns LRUCollection<K, V>

  • Returns V[]

  • Returns an iterable of values in the map

    Returns IterableIterator<V>

  • Creates a Collection from a list of entries.

    Example

    Collection.combineEntries([["a", 1], ["b", 2], ["a", 2]], (x, y) => x + y);
    // returns Collection { "a" => 3, "b" => 2 }

    Type Parameters

    • K

    • V

    Parameters

    • entries: Iterable<[K, V]>

      The list of entries

    • combine: ((firstValue, secondValue, key) => V)

      Function to combine an existing entry with a new one

        • (firstValue, secondValue, key): V
        • Parameters

          • firstValue: V
          • secondValue: V
          • key: K

          Returns V

    Returns DCollection<K, V>

Generated using TypeDoc