JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 20502
  • Score
    100M100P100Q133979F
  • License MIT

Agnostic tree traversal library.

Package Exports

  • tree-crawl

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (tree-crawl) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

tree-crawl

Agnostic tree traversal library.

travis codecov

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'

// traverse the tree in pre-order
crawl(tree, console.log)
crawl(tree, console.log, { order: 'pre' })

// traverse the tree in post-order
crawl(tree, console.log, { order: 'post' })

// traverse the tree using `childNodes` as the children key
crawl(tree, console.log, { childrenKey: 'childNodes' }

// skip a node and its children
crawl(tree, (node, context) => {
  if ('foo' === node.type) {
    context.skip()
  }
  console.log(node)
})

// break the walk
crawl(tree, (node, context) => {
  if ('foo' === node.type) {
    console.log(node)
    context.break()
  }
})

// remove a node
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.

License

MIT © Nicolas Gryman