0.0.3 • Published 6 years ago

@bhurlow/tree.walk v0.0.3

Weekly downloads
109
License
MIT
Repository
-
Last release
6 years ago

🌲

tree.walk

An implementation of clojure's walk functions for javascript. Use this for generic tree traversal operations over nested objects and arrays.

example

postwalk performs a depth-first post-order traversal of the provided (possibly nested) data structure. Uses provided function's return value in place of the original

const { postwalk } = require('./index')

const input = [[1, 2], [3, [4, 5]]]

const res = postwalk(
  val => {
    if (typeof val == 'number') {
      return val * 5
    }
    return val
  },
  input
)

=> [ [ 5, 10 ], [ 15, [ 20, 25 ] ] ]

API

walk(inner, outer, form)

applies inner to the innermost items in the collection, and outer to the wrapping whole

postwalk(f, form)

depth-first post-order

prewalk(f, form)

depth-first pre-order