Class TrieMap<T>

Class for a fast trie map.

Type Parameters

  • T

Hierarchy

  • TrieMap

Constructors

Properties

root: Record<string, any>

The TrieMap data structure root.

Accessors

  • get count(): number
  • Returns the number of values in the TrieMap.

    Returns number

    Example

    const trie = new TrieMap();
    trie.
    .set(['some', 'path'], 'value')
    .count;
    //=> 1

Methods

  • Returns an Iterable that yields each entry ([prefix, value]) in the TrieMap with the given prefix.

    Parameters

    • Optional prefix: string[]

      A string array.

    Returns Iterable<[string[], T]>

    Example

    const trie = new TrieMap()
    .set(['src', 'classes'], 2)
    .set(['src', 'modules'], 2)
    .set(['docs'], 8);
    [...trie];
    //=> [
    // [['src', 'classes'], 2],
    // [['src', 'modules'], 2],
    // [['docs', 8]]
    // ]
  • Deletes the value at the given prefix. Returns whether the operation was successful.

    Parameters

    • prefix: string[]

      A string array.

    Returns boolean

    Example

    const trie = new TrieMap();
    trie.set(['some', 'path'], 'value');
    trie.has(['some', 'path']);
    //=> true
    trie.delete(['some', 'path']);
    //=> true (means operation was successful)
    trie.has(['some', 'path']);
    //=> false
  • Deletes the value at the given prefix or all values with the given prefix if ´prune´ is set to true.

    Parameters

    • prefix: string[]

      A string array.

    Returns void

    Throws

    if the operation was unsuccessful.

    Example

    const trie = new TrieMap();
    trie.delete(['nonexistent', 'path']);
    //=> false (operation unsuccessful)
    trie.deleteStrict(['nonexistent', 'path']);
    //=> throws Error
  • Returns an Iterable that yields each entry ([prefix, value]) in the TrieMap with the given prefix.

    Parameters

    • prefix: string[] = []

      A string array.

    Returns Iterable<[string[], T]>

    Example

    const trie = new TrieMap()
    .set(['src', 'classes'], 2)
    .set(['src', 'modules'], 2)
    .set(['docs'], 8);
    [...trie.entries()];
    //=> [
    // [['src', 'classes'], 2],
    // [['src', 'modules'], 2],
    // [['docs', 8]]
    // ]
  • Iterates all (value, prefix) where value === ´valueToFind´.

    Parameters

    • prefix: string[]

      A string array.

    • valueToFind: T

      The value to look for.

    • f: ((value, prefix) => void)

      A callback function.

        • (value, prefix): void
        • Parameters

          • value: T
          • prefix: string[]

          Returns void

    Returns TrieMap<T>

    Example

    const directoryFileCounts = new TrieMap()
    .set(['src', 'classes'], 2)
    .set(['src', 'modules'], 2)
    .set(['docs'], 8);
    const directoryPathsWithTwoFiles = [];
    directoryFileCounts.find([], (value, prefix) => {
    if(value === 2) {
    directoryPathsWithTwoFiles.push(prefix);
    }
    });
    // directoryPathsWithTwoFiles will now contain: [
    // ['src', 'classes'],
    // ['src', 'modules']
    // ]
  • Iterate each (value, prefix) with the given prefix.

    Parameters

    • prefix: string[]

      A string array.

    • f: ((value, prefix) => boolean | void)

      A callback function. Return true to terminate iteration.

        • (value, prefix): boolean | void
        • Parameters

          • value: T
          • prefix: string[]

          Returns boolean | void

    Returns TrieMap<T>

    Example

    const directoryFileCounts = new TrieMap()
    .set(['src', 'classes'], 2)
    .set(['src', 'modules'], 6)
    .set(['docs'], 8);
    let totalFiles = 0;
    directoryFileCounts.forEach([], (value, prefix) => {
    totalFiles += value;
    });
    // totalFiles (2 + 6 + 8) is now = 16
    let totalSourceFiles = 0;
    directoryFileCounts.forEach(['src'], (value, prefix) => {
    totalSourceFiles += value;
    });
    // totalSourceFiles (2 + 6) is now = 8
  • Returns the value at a given prefix or undefined if no node is found.

    Parameters

    • prefix: string[]

      A string array.

    Returns undefined | T

    Example

    const trie = new TrieMap();
    trie.set(['some', 'path'], 4);
    trie.get(['some', 'path']);
    //=> 4
  • Returns the value at a given prefix.

    Parameters

    • prefix: string[]

      A string array.

    Returns T

    Throws

    if there is no value at the given prefix.

    Example

    const trie = new TrieMap();
    trie.get(['nonexistent', 'path']);
    //=> undefined
    trie.getStrict(['nonexistent', 'path']);
    //=> throws Error
  • Returns an array of alle values that begin with a specified precix.

    Parameters

    • prefix: string[]

      A string array.

    Returns T[]

    Example

    const directoryFileCounts = new TrieMap()
    .set(['src', 'classes'], 2)
    .set(['src', 'modules'], 3)
    .set(['docs'], 8);
    directoryFileCounts.getAll(['src']);
    //=> [2, 3]
  • Returns whether a value exists at the given prefix.

    Parameters

    • prefix: string[]

      A string array.

    Returns boolean

    Example

    const trie = new TrieMap();
    trie.has(['some', 'path']);
    //=> false
    trie.set(['some', 'path'], 'value');
    trie.has(['some', 'path']);
    //=> true
  • Returns an Iterable that yields each prefix in the TrieMap with the given prefix.

    Parameters

    • prefix: string[] = []

      A string array.

    Returns Iterable<string[]>

    Example

    const trie = new TrieMap()
    .set(['src', 'classes'], 2)
    .set(['src', 'modules'], 2)
    .set(['docs'], 8);
    [...trie.keys()];
    //=> [
    // ['src', 'classes'],
    // ['src', 'modules'],
    // ['docs']
    // ]
  • Insert multiple entries into the TrieMap.

    Parameters

    • iterable: Iterable<[string[], T]>

      An array or other iterable that yields entries.

    Returns TrieMap<T>

    this/self (chainable)

    Example

    const trie = new TrieMap().load([
    [['some', 'path'], 'value1'],
    [['other', 'path'], 'value2']
    ]);
  • Insert a value into the TrieMap.

    Parameters

    • prefix: string[]

      A string array.

    • value: T

      The value to insert.

    Returns TrieMap<T>

    this/self (chainable)

    Example

    const trie = new TrieMap()
    .set(['some', 'path'], 'value1');
    .set(['other', 'path'], 'value2');
  • Returns an Iterable that yields each entry ([prefix, value]) in the TrieMap with the given prefix.

    Parameters

    • prefix: string[] = []

      A string array.

    Returns [string[], T][]

    Example

    const trie = new TrieMap()
    .set(['src', 'classes'], 2)
    .set(['src', 'modules'], 2)
    .set(['docs'], 8);
    trie.toArray();
    //=> [
    // [['src', 'classes'], 2],
    // [['src', 'modules'], 2],
    // [['docs', 8]]
    // ]
  • Returns the trie map data structure as pretty printed JSON.

    Parameters

    • pretty: boolean = false

      Whether to return a pretty formatted JSON string rather than a condensed machine readble string.

    Returns string

    Example

    const trie = new TrieMap()
    .set(['src', 'classes'], 2)
    .set(['src', 'modules'], 2)
    .set(['docs'], 8);
    trie.toJson();
    //=> "{root:{src:{classes:2,modules:2,},docs:8,}}"
    trie.toJson(true);
    //=> {
    // root: {
    // src: {
    // classes: 2,
    // modules: 2,
    // },
    // docs: 8,
    // },
    // }
  • Updates a value in the TrieMap.

    Parameters

    • prefix: string[]

      A string array.

    • f: ((value) => T)

      A function that when passed the current value, will return another replacement value.

        • (value): T
        • Parameters

          • value: T

          Returns T

    Returns TrieMap<T>

    this/self (chainable)

    Example

    const trie = new TrieMap();
    trie.set(['some', 'path'], 4);
    trie.get(['some', 'path']);
    //=> 4
    trie.update(['some', 'path'], (value) => {
    return value + 2
    });
    trie.get(['some', 'path']);
    //=> 6
  • Iterate each (value, prefix) with the given prefix and replace all elements using a callback function.

    Parameters

    • prefix: string[]

      A string array.

    • f: ((value, prefix?) => T)

      A callback function.

        • (value, prefix?): T
        • Parameters

          • value: T
          • Optional prefix: string[]

          Returns T

    Returns TrieMap<T>

    Example

    const trie = new TrieMap();
    trie.load([
    [['a'], 0],
    [['b'], 1],
    [['a', 'a'], 2],
    ]);
    trie.updateAll([], (value) => value + 1);
    trie.get(['a']) // => 1
    trie.get(['b']) // => 2
    trie.get(['a', 'a']) // => 3
  • Returns an Iterable that yields each value in the TrieMap with the given prefix.

    Parameters

    • prefix: string[] = []

      A string array.

    Returns Iterable<T>

    Example

    const trie = new TrieMap()
    .set(['src', 'classes'], 2)
    .set(['src', 'modules'], 2)
    .set(['docs'], 8);
    [...trie.values()];
    //=> [2, 2, 8]
  • Creates a new instance from existing data.

    Type Parameters

    • T

    Parameters

    • iterable: Iterable<[string[], T]>

      An interable that yields entries.

    Returns TrieMap<T>

    Example

    const trie = TrieMap.fromIterable([
    [['some', 'path'], 'value1'],
    [['other', 'path'], 'value2']
    ]);
  • Creates a new instance from existing data.

    Type Parameters

    • T

    Parameters

    • json: string

      A JSON-string (a previously stringified TrieMap instance).

    Returns TrieMap<T>

    Example

    const json = new TrieMap()
    .set(['some', 'path'], 'value')
    .toJson();
    const trie = TrieMap.fromJSON(json);

Generated using TypeDoc