Tree namespace

Work with trees

This is a collection of functions for working with tree structures built from maps: instances of the standard Map class, the asynchronous AsyncMap variation, or map-like objects.

Most of the Tree operations accept such a map as their first argument. If the argument is not already a Map or AsyncMap, it will be wrapped in one.

Some operations like Tree.values are inherently shallow and only work on the input as if it were a flat map. Other operations like Tree.deepValues treat their input as the root node of a map-based tree and generally walk through that tree structure.

$ ori Tree.values { a: 1, b: { c: 2 }}
- 1
- c: 2
$ ori Tree.deepValues { a: 1, b: { c: 2 }}
- 1
- 2

Here Tree.values returns the values of the map it is given, one of which is the object { c: 2 }. The Tree.deepValues function walks the tree to returns the leaf values. To reflect this, Tree.values is documented as taking a map parameter, while Tree.deepValues is documented as taking a tree parameter.

Commands by name #

Tree.addNextPrevious(map)
Add next/previous fields to the map’s values
Tree.assign(target, source)
Apply key/values from source to target
Tree.cache(tree, [cache])
Caches values from the tree
Tree.calendar(options)
Return a tree structure for years/months/days
Tree.clear(map)
Remove all values from the map
Tree.constant(value)
Return a deep tree with a single constant value
Tree.deepMap(tree, options)
Map the keys and values of a deep tree
Tree.deepMerge(…trees)
Return a deeply-merged tree
Tree.deepReverse(tree)
Reverse order of keys at all levels of the tree
Tree.deepTake(tree, n)
The first n values from the deep tree
Tree.deepText(tree)
The text values of the deep tree
Tree.deepValues(tree)
The in-order leaf values of the tree
Tree.delete(map, key)
Delete the value for the key from map
Tree.entries(map)
The map’s [key, value] pairs
Tree.filter(tree, options)
Filter a tree by a condition
Tree.first(map)
The first value in the map
Tree.forEach(map, fn)
Apply fn to each (value, key)
Tree.from(object, options)
Create a map from an object
Tree.globKeys(patterns)
A tree whose keys can include glob wildcard patterns
Tree.groupBy(map, fn)
A new map with values grouped by the function
Tree.has(map, key)
True if key exists in map
Tree.indent`…`
Tagged template literal for normalizing indentation
Tree.inners(tree)
The tree’s interior nodes
Tree.isMap(object)
True if object is a map
Tree.isMaplike(object)
True if object can be coerced to a tree
Tree.isReadOnlyMap(object)
True if object is a read-only map
Tree.isTraversable(object)
True if object is traversable
Tree.json(tree)
Render the tree in JSON format
Tree.keys(map)
The keys of the map
Tree.map(source, options)
Create a new map by transforming keys and/or values
Tree.mapExtension(source, ext, options)
Create a new map by transforming extensions
Tree.mapReduce(tree, mapFn, reduceFn)
Map the keys and/or values in a tree and reduce them
Tree.mask(source, mask)
Return the source tree with only the keys in the mask
Tree.match(pattern, fn, [keys])
Matches simple patterns or regular expressions
Tree.merge(…maps)
Return a new tree merging the given maps
Tree.paginate(map, [n])
Group the map’s values into fixed-size sets
Tree.parent(tree)
The parent of the given tree node
Tree.paths(tree)
Slash-separated paths for the tree’s values
Tree.plain(tree)
Render the tree as a plain JavaScript object
Tree.regExpKeys(tree)
A tree whose keys are regular expression strings
Tree.reverse(map)
Reverse the order of the map’s keys
Tree.root(tree)
The root node of the given tree
Tree.scope(tree)
A merged view of the tree and its ancestors
Tree.shuffle(map)
Shuffle the keys of the map
Tree.size(map)
The map’s size (number of keys)
Tree.sort(map, options)
A new map with its keys sorted
Tree.sync(tree)
Awaits all asynchronous values in the tree
Tree.take(map, n)
The first n values in the map
Tree.text`…`
Tagged template literal for rendering trees
Tree.traverse(tree, …keys)
Return the value at the path of keys
Tree.traverseOrThrow(tree, …keys)
Return the value at the path of keys or throw
Tree.traversePath(tree, path)
Traverse a slash-separated path
Tree.values(map)
The map’s values
Tree.withKeys(map, keys)
Use the given keys for the map