@bhurlow/tree.walk

clojure's walk functions for javascript

Usage no npm install needed!

<script type="module">
  import bhurlowTreeWalk from 'https://cdn.skypack.dev/@bhurlow/tree.walk';
</script>

README

🌲

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