es-stl - v3.0.2
    Preparing search index...

    Dictionary backed by a plain object.

    Keys are limited to numbers and strings because values are stored as object properties. Iteration keys are returned as strings, matching Object.keys(). All input objects are copied into a null-prototype object before storage.

    const dict = new Dictionary<string, number>();
    dict.set('apples', 2).set('oranges', 3);

    dict.get('apples');
    // => 2

    dict.keys();
    // => ['apples', 'oranges']

    Type Parameters

    • K extends NumOrStr

      Numeric or string key type accepted by dictionary methods.

    • V

      Value type stored in the dictionary.

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    _iterators: [Nullable<Iterator<V, string>>, Nullable<Iterator<V, string>>] = ...
    _obj: KeyValue<K, V> = ...
    _size: number = 0

    Accessors

    • get size(): number

      Number of values currently stored in the collection.

      Returns number

      The collection size.

    Methods

    • Removes key from the dictionary.

      Parameters

      • key: K

        Key to remove.

      Returns boolean

      true when a stored key was removed.

      const dict = new Dictionary({ stale: true });

      dict.delete('stale');
      // => true
    • Replaces the backing object with object.

      Parameters

      • object: KeyValue<K, V>

        Object to use as dictionary storage.

      Returns this

      This dictionary instance.

      The provided object is copied. Later changes to object do not mutate the dictionary.

      const source = { a: 1 };
      const dict = new Dictionary<string, number>().fromObject(source);

      source.a = 2;
      dict.get('a');
      // => 1
    • Reads the value stored for key.

      Parameters

      • key: K

        Key to read.

      Returns V | undefined

      The stored value, or undefined when the key is absent.

      const dict = new Dictionary({ count: 3 });

      dict.get('count');
      // => 3
    • Checks whether key exists as an own property.

      Parameters

      • key: K

        Key to check.

      Returns boolean

      true when the dictionary contains key.

      const dict = new Dictionary({ enabled: false });

      dict.has('enabled');
      // => true
    • Creates or reuses an iterator bound to this collection.

      Parameters

      • Optionalreverse: boolean

        Whether iteration should run in reverse order.

      Returns Iterator<V, string>

      An iterator bound to this collection.

      Calling iterator() repeatedly returns the same forward iterator instance. Calling iterator(true) repeatedly returns the same reverse iterator instance.

      const dict = new Dictionary({ a: 1, b: 2 });

      dict.iterator().map((value) => value * 2);
      // => [2, 4]
    • Lists dictionary keys.

      Returns string[]

      Own enumerable keys as strings.

      const dict = new Dictionary({ 1: 'one', 2: 'two' });

      dict.keys();
      // => ['1', '2']
    • Stores value at key.

      Parameters

      • key: K

        Key to write.

      • value: V

        Value to store.

      Returns this

      This dictionary instance.

      const dict = new Dictionary<string, number>();

      dict.set('count', 1).set('count', 2);
      dict.size;
      // => 1
    • Returns the backing object.

      Returns KeyValue<K, V>

      The backing object.

      The returned object is a shallow copy. Mutating it does not update the dictionary.

      const dict = new Dictionary({ a: 1 });
      const object = dict.toObject();

      object.a = 2;
      dict.get('a');
      // => 1
    • Serializes the backing object as JSON.

      Returns string

      JSON string representation of the dictionary.

      const dict = new Dictionary({ ok: true });

      dict.toString();
      // => '{"ok":true}'
    • Visits dictionary values in Object.keys() order.

      Parameters

      • breakFlag: boolean

        Callback result that should stop traversal early.

      • reverse: boolean

        Whether traversal should run from last key to first key.

      • callback: IterateCallback<V, string>

        Callback invoked for each value and string key.

      Returns boolean

      breakFlag when traversal stops early, otherwise !breakFlag.

      Reverse traversal visits the keys returned by keys from the end of the array to the beginning.

      const dict = new Dictionary({ a: 1, b: 2 });
      const seen: string[] = [];

      dict.traverse(false, false, (value, key) => {
      seen.push(`${key}:${value}`);
      });
      // seen === ['a:1', 'b:2']
    • Lists dictionary values in key iteration order.

      Parameters

      • Optionalvalues: V[]

        Optional array to clear and reuse for the result.

      Returns V[]

      Array containing the dictionary values.

      When values is provided, it is cleared and reused as the return value.

      const dict = new Dictionary({ a: 1, b: 2 });
      const output: number[] = [99];

      dict.values(output);
      // output === [1, 2]