forEach for trees
$ {package mananger} install jkroso/tree-each
then in your app:
var treach = require('tree-each')
iterate a tree breadth first along path
from node
iterate a tree depth first along path
from node
walk up path
from node
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 ]
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.