Skip to content

Latest commit

 

History

History
77 lines (49 loc) · 1.51 KB

Documentation.md

File metadata and controls

77 lines (49 loc) · 1.51 KB

Documenation

Introduction

Topaz is a library for topological sorting.

Installation

Topaz is available on npm:

npm install @botsocket/topaz

Usage

const Topaz = require('@botsocket/topaz');

const sorter = Topaz.sorter();

sorter
    .add('a', 'b')
    .add('b', 'c');

sorter.sort();  // [ c, b, a ]

API

sorter()

Creates a sorter.

const sorter = Topaz.sorter();

Back to top

sorter.add(node, [...deps])

Adds a node and its dependencies where:

  • node: The node to add.
  • deps: Optional dependencies to the node. Can be an array of values or multiple values passed as arguments.

Note that calling the method multiple times with the same node will concatenate its dependencies.

const sorter = Topaz.sorter();

sorter.add('x', 'y'); // Single dependency
sorter.add('x', ['y', 'z']); // Multiple dependencies
sorter.add('x', 'y').add('x', 'z', 't'); // Multiple calls
sorter.add('x'); // No dependencies

Back to top

sorter.sort()

Sorts the given nodes topologically. If a circular dependency is detected, an error will be thrown with the dependency chain for debugging purposes. Note that objects are sorted by their references.

const sorter = Topaz.sorter();

sorter.add('x', 'y');
sorter.add('y', 'z');
sorter.sort(); // [ z, y, x ]

Back to top