Tree

Helper functions for working with asynchronous trees

async assign(target, source)

  • target: AsyncMutableTree
  • source: AsyncTree

Returns: unknown

Apply the key/values pairs from the source tree to the target tree.

If a key exists in both trees, and the values in both trees are subtrees, then the subtrees will be merged recursively. Otherwise, the value from the source tree will overwrite the value in the target tree.

async clear(tree)

  • tree: AsyncMutableTree

Returns: any

Removes all entries from the tree.

async entries(tree)

  • tree: AsyncTree

Returns: unknown

Returns a new Iterator object that contains a two-member array of [key, value] for each element in the specific node of the tree.

async forEach(tree, callbackFn)

  • tree: AsyncTree
  • callbackFn: Function

Returns: any

Calls callbackFn once for each key-value pair present in the specific node of the tree.

from(obj)

  • obj: Treelike | Object

Returns: any

Attempts to cast the indicated object to an async tree.

async has(tree, key)

  • tree: AsyncTree
  • key: any

Returns: unknown

Returns a boolean indicating whether the specific node of the tree has a value for the given key.

isAsyncTree(object)

  • object: any

Returns: boolean

Return true if the indicated object is an async tree.

isAsyncMutableTree(object)

  • object: any

Returns: boolean

Return true if the indicated object is an async mutable tree.

async isKeyForSubtree(tree, key)

  • tree: any
  • key: any

Returns: unknown

Return true if the indicated key produces or is expected to produce an async tree.

This defers to the tree’s own isKeyForSubtree method. If not found, this gets the value of that key and returns true if the value is an async tree.

isTraversable(object)

  • object: any

Returns: boolean

Return true if the object can be traversed via the traverse() method. The object must be either treelike or a packed object with an unpack() method.

isTreelike(object)

  • object: any

Returns: boolean

Returns true if the indicated object can be directly treated as an asynchronous tree. This includes:

  • An object that implements the AsyncTree interface (including AsyncTree instances)
  • A function
  • An Array instance
  • A Map instance
  • A Set instance
  • A plain object

Note: the from() method accepts any JavaScript object, but isTreelike returns false for an object that isn’t one of the above types.

map(treelike, valueFn)

  • treelike: Treelike
  • valueFn: ValueKeyFn

Returns: any

Return a new tree with deeply-mapped values of the original tree.

async mapReduce(treelike, valueFn, reduceFn)

  • treelike: Treelike
  • valueFn: ValueKeyFn | null
  • reduceFn: ReduceFn

Returns: unknown

Map and reduce a tree.

This is done in as parallel fashion as possible. Each of the tree’s values will be requested in an async call, then those results will be awaited collectively. If a mapFn is provided, it will be invoked to convert each value to a mapped value; otherwise, values will be used as is. When the values have been obtained, all the values and keys will be passed to the reduceFn, which should consolidate those into a single result.

async plain(treelike)

  • treelike: Treelike

Returns: Promise

Converts an asynchronous tree into a synchronous plain JavaScript object.

The result’s keys will be the tree’s keys cast to strings. Any tree value that is itself a tree will be similarly converted to a plain object.

async remove(tree, key)

  • tree: AsyncMutableTree
  • key: any

Returns: unknown

Removes the value for the given key from the specific node of the tree.

Note: The corresponding Map method is delete, not remove. However, delete is a reserved word in JavaScript, so this uses remove instead.

toFunction(treelike)

  • treelike: Treelike

Returns: unknown

Returns a function that invokes the tree’s get method.

async traverse(treelike, keys)

  • treelike: Treelike
  • keys: any

Returns: unknown

Return the value at the corresponding path of keys.

async traverseOrThrow(treelike, keys)

  • treelike: Treelike
  • keys: any

Returns: unknown

Return the value at the corresponding path of keys. Throw if any interior step of the path doesn’t lead to a result.

async traversePath(tree, path)

  • tree: Treelike
  • path: string

Returns: unknown

Given a slash-separated path like “foo/bar”, traverse the keys “foo” and “bar” and return the resulting value.

async values(tree)

  • tree: AsyncTree

Returns: unknown

Return the values in the specific node of the tree.