tree-each

forEach for trees

npm install tree-each
4 downloads in the last month

tree-each

forEach for trees

Getting Started

With component, packin or npm

$ {package mananger} install jkroso/tree-each

then in your app:

var treach = require('tree-each')

API

depth(path:String, node:Object, fn:Function, [context]:Any)

iterate a tree breadth first along path from node

breadth(path:String, node:Object, fn:Function, [context]:Any)

iterate a tree depth first along path from node

up(path:String, node:Object, fn:Function, [context]:Any)

walk up path from node

example

each can be used to define other higher order functions that operate over trees. Here we define filter and use it to emulate document.body.querySelectorAll. Note the use of currying to keep noise to a minimum inside filter. All functions in this package have been made curryable.

var each = depthFirst('children')
function filter(el, fn, ctx){
  var res = []
  each(el, function(el){
    if (fn(el)) res.push(el)
  }, ctx)
  return res
}
filter(document.body, function(el){
  return el.matchesSelector('.hidden')
}) // => [ Element ]

Running the tests

Just run make. It will install and start a development server. Then just point your browser to localhost:3000/test. Likewise to run the examples.

npm loves you