Class ArrayStringMap<K, V>

A Map that uses arrays as keys by encoding them as strings.

This class implements the Map interface. All methods that can be used on a normal Map are available on the ArrayStringMap.

Example

const map = new ArrayStringMap<string[], number>();
map.set(['a', 'b', 'c'], 123);
map.get(['a', 'b', 'c']); // 123
map.get(['a', 'b']); // undefined

Type Parameters

  • K extends any[]

    The type of the keys. Has to be an array type

  • V

    The type of the values.

Hierarchy

  • ArrayStringMap

Implements

  • Map<K, V>

Constructors

  • Creates a new ArrayStringMap.

    Example

    const map = new ArrayStringMap<string[], number>();
    

    Type Parameters

    • K extends any[]

    • V

    Parameters

    • sep: string = '\u200b'

      The seperator that is being used to seperate list items. By default, this is the zero-width space character, which is unlikely to be used in most trusted strings. However, if the data used can be arbritary strings that are provided as user input, it is recommended to validate given strings to remove the zero-width space character or set the seperator to another string that cannot be in the input due to validation checks.

    Returns ArrayStringMap<K, V>

Properties

_converterInfo: Map<string, K> = ...

A map storing the string versions and the original array versions.

_map: Map<string, V> = ...

The map that stores the string and values.

_sep: string

The seperator that is being used to seperate list items.

Accessors

  • get [toStringTag](): string
  • The name of the class.

    Returns string

  • get sep(): string
  • The seperator that is being used to seperate list items.

    Example

    const map = new ArrayStringMap<string[], number>();
    map.sep; // '\u200b'

    Returns string

  • get size(): number
  • The number of items in the map.

    Returns number

Methods

  • Returns

    An iterator over the entries in the map.

    See

    Returns IterableIterator<[K, V]>

  • Removes all entries from the map.

    Returns void

  • Deletes an entry from the map.

    Returns

    Whether the entry was deleted (true) or never existed (false)

    Example

    const map = new ArrayStringMap<string[], number>();
    map.set(['a', 'b', 'c'], 123);
    map.delete(['a', 'b', 'c']); // true
    map.delete(['a', 'b', 'c']); // false

    Parameters

    • key: K

      The key to delete

    Returns boolean

  • Encode an array into its string representation.

    Returns

    The string representation of the array

    Parameters

    • arr: K

      The array to encode

    Returns string

  • Returns

    An iterator over the entries in the map.

    Example

    const map = new ArrayStringMap<string[], number>();
    map.set(['a', 'b', 'c'], 123);
    map.set(['a', 'b', 'd'], 456);
    for (const [key, value] of map.entries()) {
    console.log(key, value);
    }
    // ['a', 'b', 'c'] 123
    // ['a', 'b', 'd'] 456

    Returns IterableIterator<[K, V]>

  • Executes a function for each entry in the map.

    Example

    const map = new ArrayStringMap<string[], number>();
    map.set(['a', 'b', 'c'], 123);
    map.set(['a', 'b', 'd'], 456);
    map.forEach((value, key) => {
    console.log(key, value);
    });
    // ['a', 'b', 'c'] 123
    // ['a', 'b', 'd'] 456

    Parameters

    • callbackfn: ((value: V, key: K, map: ArrayStringMap<K, V>) => void)

      The function to execute for each entry.

    • Optional thisArg: any

      The value to use as this when executing the function.

    Returns void

  • Gets the value for a given key.

    Returns

    The value for the given key, or undefined if the key does not exist.

    Example

    const map = new ArrayStringMap<string[], number>();
    map.set(['a', 'b', 'c'], 123);
    map.get(['a', 'b', 'c']); // 123
    map.get(['a', 'b']); // undefined

    Parameters

    • key: K

      The key to get the value for

    Returns undefined | V

  • Checks whether the map has a given key.

    Returns

    Whether the map has the given key.

    Example

    const map = new ArrayStringMap<string[], number>();
    map.set(['a', 'b', 'c'], 123);
    map.has(['a', 'b', 'c']); // true
    map.has(['a', 'b']); // false

    Parameters

    • key: K

      The key to check for.

    Returns boolean

  • Get the keys in the map in array form.

    Returns

    An iterator over the keys in the map.

    Example

    const map = new ArrayStringMap<string[], number>();
    map.set(['a', 'b', 'c'], 123);
    map.set(['a', 'b', 'd'], 456);
    for (const key of map.keys()) {
    console.log(key);
    }
    // ['a', 'b', 'c']
    // ['a', 'b', 'd']

    Returns IterableIterator<K>

  • Sets a value for a given key.

    Returns

    The map itself.

    Example

    const map = new ArrayStringMap<string[], number>();
    map.set(['a', 'b', 'c'], 123);

    Parameters

    • key: K

      The key to set the value for.

    • value: V

      The value to set.

    Returns ArrayStringMap<K, V>

  • Returns

    An iterator over the values in the map.

    Example

    const map = new ArrayStringMap<string[], number>();
    map.set(['a', 'b', 'c'], 123);
    map.set(['a', 'b', 'd'], 456);
    for (const value of map.values()) {
    console.log(value);
    }
    // 123
    // 456

    Returns IterableIterator<V>

Generated using TypeDoc