depth-first-map
Map a tree of arrays in pre-order depth-first
Last updated 2 years ago by emilbayes .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install depth-first-map 
SYNC missed versions from official npm registry.

depth-first-map

Map a tree of arrays in pre-order depth-first

Usage

var dfs = require('depth-first-map')

// Numbers refer to the order in which fn touches the elements
var tree = [
  0,
  1,
  // outer array: 2, inner array: 4
  [3, [5]],
  6
]

dfs(tree, x => x)

API

var newTree = dfs(tree, fn, context)

Map tree to newTree calling fn(subtree, depth, index), where context (this) will be to the current subtree, unless defined in which case it will stay constant through out the traversal. Please review the following control flow symbols as well for more powerful mapping possibilities.

return dfs.continue

Skip the current element, but continue processing siblings

return dfs.break

Break out of processing the current element, preserving previously maped siblings.

return dfs.drop

Drop the current subtree and return back to processing parent. Previously maped siblings will also be dropped.

return dfs.spread(elms)

Return an array of new elements to be spliced into position of the current subtree / leaf. The next iteration will process the first element of the spread elements:

dfs([5], x => x > 1 ? dfs.spread([1, x - 1]) : 1) // => [1, 1, 1, 1, 1]
/*
[5] => [1, 4] => [1, 1, 3] => [1, 1, 1, 2] => [1, 1, 1, 1, 1]
 */

Install

npm install depth-first-map

License

ISC

Current Tags

  • 1.0.0                                ...           latest (2 years ago)

1 Versions

  • 1.0.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (1)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |