Skip to content

Tree

Module Collections.Trees

The Trees.Mutable sub-module is a collection of functions for working with trees.

For more object-oriented access, use rootWrapped , which gives a WrappedNode instance.

import { Trees } from "https://unpkg.com/@ixfx/collections/bundle"
const root = Trees.Mutable.rootWrapped(`root`);
// Add 'a' as the child of the root node
let a = root.addValue(`a`);
// Add `aa` as the child of `a`
let b = a.addValue(`aa`);
b.hasParent(a); // True

The FromObject sub-module allows tree-like access to objects via asDynamicTraversable and create .

asDynamicTraversable will present a dynamic tree structure based on the live object.

It returns a TraversableTree which can be used with functions in the Trees.Traverse sub-module.

import { Trees } from "https://unpkg.com/@ixfx/collections/bundle"
const myObj = { name: `Pedro`, size: 45, colour: `orange` };
const root = Trees.FromObject.asDynamicTraversable(myObj);
for (const v of Trees.Traverse.breadthFirst(root)) {
// v.getValue() yields:
// { name: 'name', sourceValue: 'Pedro' ...},
// { name: 'size', sourceValue: 45 ... }
// ...
}

create in contrast will take a snapshot of the object, returning a Type TreeNode representation.

// Sub module
import { Stacks } from "@ixfx/collections.js"
// Eg: Stacks.immutable();
// Whole module
import * as Collections from "@ixfx/collections.js"
// Eg: Collections.Stacks.immutable();
// From bundle
import { Collections } from "@ixfx"
// Eg: Collections.Stacks.immutable();
// And within your HTML's <HEAD> </HEAD> block:
<script type="importmap">{ "imports": { "@ixfx":"/ixfx/index.js", "@ixfx/": "/ixfx/" } }</script>