unist utility to add references to parents on nodes in a tree.
Instead of modifying the original syntax tree, this module returns a wrapper that makes it easier to traverse that tree.
Algorithms that work on regular unist trees are (mostly) guaranteed to work on wrapped trees, and each wrapped node maintains a reference to the node from which it originated.
npm install unist-util-parents
var u =var parents =var tree =var wrapped =// Leaf 4var node = wrappedchildren1children2children1children0var chain =while nodechainnode = nodeparentconsole
'root' 'node' 'node' 'node' 'leaf'
Returns a wrapped
tree with a proxy that imposes two additional properties on
all of its nodes:
parent— parent link (or
nullfor the root node)
node— link to the original node
None of these properties are enumerable, and the original tree is not changed.
This means you can
JSON.stringify the wrapped tree and it is just the same.
wrapped.children returns array of wrapped child nodes, so that any
recursive algorithm will work on a wrapped tree just as well.
Remember to access
.node before you commit any changes to a node.
Node — A wrapped node: shallow copy of the given node with
non-enumerable references to
parent, and if
tree had children,
they are wrapped as well.
unist-util-visit-parents— Recursively walk over unist nodes, with ancestral information
This project has a Code of Conduct. By interacting with this repository, organisation, or community you agree to abide by its terms.
MIT © Eugene Sharygin