tree-crawl
Agnostic tree traversal library.
tree-crawl is a lightweight tree crawler, well, technically walker.
It lets your easily walk any tree in pre-order or post-order and supports tree mutations, it does not 💥 when you move nodes around.
Install
npm install --save tree-crawl
Usage
import crawl from 'tree-crawl'
crawl(tree, console.log)
crawl(tree, console.log, { order: 'pre' })
crawl(tree, console.log, { order: 'post' })
crawl(tree, console.log, { childrenKey: 'childNodes' }
crawl(tree, (node, context) => {
if ('foo' === node.type) {
context.skip()
}
console.log(node)
})
crawl(tree, (node, context) => {
if ('foo' === node.type) {
console.log(node)
context.break()
}
})
crawl(tree, (node, context) => {
if ('foo' === node.type) {
const parentChildren = node.parent.children
parentChildren.splice(parentChildren.indexOf(node))
context.remove()
}
})
API
See the api documentation.
Related
License
MIT © Nicolas Gryman