Tree

Helper functions for working with asynchronous trees

async assign(target, source)

  • target: any
  • source: any

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: any

Returns: any

Removes all entries from the tree.

async entries(tree)

  • tree: any

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: any
  • callbackFn: unknown

Returns: any

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

from(object, [options])

  • object: any
  • options: { deep?: boolean; parent?: any; }

Returns: any

Attempts to cast the indicated object to an async tree.

If the object is a plain object, it will be converted to an ObjectTree. The optional deep option can be set to true to convert a plain object to a DeepObjectTree. The optional parent parameter will be used as the default parent of the new tree.

async has(tree, key)

  • tree: any
  • key: any

Returns: unknown

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

isAsyncTree(obj)

  • obj: any

Returns: boolean

Return true if the indicated object is an async tree.

isAsyncMutableTree(obj)

  • obj: any

Returns: boolean

Return true if the indicated object is an async mutable 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(obj)

  • obj: 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.

async mapReduce(treelike, valueFn, reduceFn)

  • treelike: any
  • valueFn: any
  • reduceFn: any

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 paths(treelike, [base])

  • treelike: any
  • base: string

Returns: any

Returns slash-separated paths for all values in the tree.

async plain(treelike)

  • treelike: any

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.

Any trailing slashes in keys will be removed.

async remove(tree, key)

  • tree: any
  • 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: any

Returns: unknown

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

async traverse(treelike, keys)

  • treelike: any
  • keys: unknown

Returns: unknown

Return the value at the corresponding path of keys.

async traverseOrThrow(treelike, keys)

  • treelike: any
  • keys: unknown

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: any
  • 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: any

Returns: unknown

Return the values in the specific node of the tree.