0.0.4 • Published 2 years ago

@sha1n/dagraph v0.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

CI Coverage GitHub npm type definitions npm

DAGraph

A direct acyclic graph implementation

Features

  • Hosts your data in the graph structure
  • Topological sort traversal generator
  • Traverse root nodes generator
  • Reverse graph API

Usage

Basic

import createDAG from '@sha1n/dagraph';

const dag = createDAG();

dag.addNode({id : 'a'});
dag.addEdge({id : 'b'}, {id : 'a'});
dag.addEdge({id : 'c'}, {id : 'a'});
dag.addEdge({id : 'd'}, {id : 'b'});

for (const node of dap.topologicalSort()) {
  ...
}

Custom Objects

type MyThing = {
  id: string; // <-- implicitly implements the 'Identifiable' interface
  name: string;
  doSomething(): void;
};

const myThing = {
    id: 'a',
    name: 'my thing',
    doSomething: () => {
      console.log('A');
    }
  };

const myOtherThing = {
    id: 'b',
    name: 'my other thing',
    doSomething: () => {
      console.log('B');
    }
  };

const myDAG = createDAG<MyThing>();

myDAG.addEdge(myThing, myOtherThing);

Install

yarn install @sha1n/dagraph
npm i @sha1n/dagraph