0.2.0 • Published 12 years ago
tree-each v0.2.0
tree-each
forEach for trees
Getting Started
$ {package mananger} install jkroso/tree-eachthen 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.
0.2.0
12 years ago