HashArray is a data structure that combines the best feature of a hash (O(1) retrieval) and an array (length and ordering). Think of it as a super-lightweight, extensible, self-indexing set database in memory.

Type Parameters

  • T extends object

Constructors

  • Type Parameters

    • T extends object

    Parameters

    • OptionalkeyFields: string | KeyFields

      A single string or an array of strings / arrays representing what fields on added objects are to be used as keys for the trie search / HashArray.

    • Optionaloptions: HashArrayOptions<T>

      Options.

    Returns HashArray<T>

Accessors

  • get size(): number
  • Returns number

    The mapped size; number of keys in HashArray.

  • get sizeFlat(): number
  • Returns number

    The flattened size; number of items in HashArray.

Methods

  • Add items or list of items to the HashArray instance.

    Parameters

    • ...items: (T | Iterable<T, any, any>)[]

      Items to add.

    Returns this

    This instance.

  • Clears all items.

    Returns this

    This instance.

  • Clones this HashArray. By default, returning an empty HashArray with cloned KeyFields. Set items in options to CloneOps.SHALLOW to copy the items. To fully clone all items set CloneOps.DEEP.

    Parameters

    • Optionalopts: { items?: CloneOps; options?: HashArrayOptions<T> }

      Optional parameters.

      • Optionalitems?: CloneOps

        Clone operation for items. By default, no items are included in the clone. Supply SHALLOW and items are copied. Supply DEEP and items are cloned as well.

      • Optionaloptions?: HashArrayOptions<T>

        Optional change to options for the clone that is merged with current HashArray options.

    Returns HashArray<T>

  • Detects if the given item collides with an existing key / item pair.

    Parameters

    • item: Partial<T>

      A partial item to check for collision.

    Returns boolean

    Is there a collision?

  • Filters this HashArray returning a new HashArray with the items that pass the given filter test.

    Parameters

    • key: Key

      The Key to retrieve item(s) to iterate.

    • callbackOrIndex: Key | (item: T) => boolean

      A Key to lookup for filter inclusion or a callback function returning the filter result for the item.

    Returns HashArray<T>

  • Iterates over all items retrieved by the given key invoking the callback function for each item.

    Parameters

    • key: Key

      The Key to retrieve items to iterate.

    • callback: (item: T) => void

      A callback invoked for each item.

    Returns this

    This instance.

  • Iterates over all items retrieved by the given key invoking the callback function for each item with the value found by the index Key and the item itself.

    Parameters

    • key: Key

      The Key to retrieve item(s) to iterate.

    • index: Key

      A specific Key in each item to lookup.

    • callback: (value: any, item: T) => void

      A callback invoked for each item with value of index and item.

    Returns this

    This instance.

  • Gets item(s) by the given key.

    Parameters

    • key: string

      The key for an item to retrieve.

    Returns T | T[]

    All items stored by the given key.

  • Gets all items stored by the given Key. You may pass * as a wildcard for all items.

    Parameters

    • keys: Key

      The Key for item(s) to retrieve.

    Returns T[]

    All item(s) for the given Key.

  • Gets item(s) by the given key always returning an array including an empty array when key is not in the HashArray.

    Parameters

    • key: string

      The key for item(s) to retrieve.

    Returns T[]

    All items for key or empty array.

  • Gets the item stored in the flat list of all items at the given index.

    Parameters

    • index: number

      The index to retrieve.

    Returns T

  • Verifies if this HashArray has this key.

    Parameters

    • key: string

      The key to check.

    Returns boolean

    Whether this HashArray already has the given key.

  • Returns the intersection of this HashArray and a target HashArray.

    Parameters

    Returns HashArray<T>

    Returns a new HashArray that contains the intersection between this (A) and the HashArray passed in (B). Returns A ^ B.

  • Removes all item(s) given.

    Parameters

    • ...items: T[]

      Items to remove.

    Returns this

    This instance.

  • Remove item(s) associated with the given keys from the HashArray.

    Parameters

    • ...keys: string[]

      Keys associated with the item(s) to be removed.

    Returns this

    This instance.

  • When treating HashArray as a cache removing the first item removes the oldest item.

    Returns this

    This instance.

  • When treating HashArray as a cache removing the last item removes the newest item.

    Returns this

    This instance.

Properties

CloneOps: typeof CloneOps

An enum used in HashArray.clone determining how items are handled.