dag-visitor

A generic DAG visitor for use in JavaScript

Usage no npm install needed!

<script type="module">
  import dagVisitor from 'https://cdn.skypack.dev/dag-visitor';
</script>

README

dag-visitor

A generic visitor library for directed-acyclic graphs in JavaScript. The purpose of this library is to use best practices for traversing a graph. It avoids using recursion to enable processing of large graphs.

Example usage

// Setup a DAG
const a = { id: 'a', children: []};
const b = { id: 'b', children: []};
a.children.push(b);

const dfsVisitor = new DfsVisitor(
    (node) => { 
        console.log(`previsit: ${node.id}`); 
        return node.children; 
    },
    (node) => { console.log(`postvisit: ${node.id}`); });

dfsVisitor.visit([a, b]);

// previsit: a
// previsit: b
// postvisit: b
// postvisit: a