npm: npm install @hapi/topo

yarn: yarn add @hapi/topo


The Topo object is the container for topologically sorting a list of nodes with non-circular interdependencies.


const Topo = require('@hapi/topo');

const morning = new Topo.Sorter();

morning.add('Nap', { after: ['breakfast', 'prep'] });

    'Make toast',
    'Pour juice'
], { before: 'breakfast', group: 'prep' });

morning.add('Eat breakfast', { group: 'breakfast' });

morning.nodes;        // ['Make toast', 'Pour juice', 'Eat breakfast', 'Nap']


new Sorter()

Creates a new Sorter object.

sorter.add(nodes, [options])

Specifies an additional node or list of nodes to be topologically sorted where:

  • nodes - a mixed value or array of mixed values to be added as nodes to the topologically sorted list.
  • options - optional sorting information about the nodes:
    • group - a string naming the group to which nodes should be assigned. The group name '?' is reserved.
    • before - a string or array of strings specifying the groups that nodes must precede in the topological sort.
    • after - a string or array of strings specifying the groups that nodes must succeed in the topological sort.
    • sort - a numerical value used to sort items when performing a sorter.merge().

Returns an array of the topologically sorted nodes.


An array of the topologically sorted nodes. This list is renewed upon each call to sorter.add().


Merges another Sorter object into the current object where:

  • others - the other object or array of objects to be merged into the current one. null values are ignored.

Returns an array of the topologically sorted nodes. Will throw if a dependency error is found as a result of the combined items.

If the order in which items have been added to each list matters, use the sort option in sorter.add() with an incrementing value providing an absolute sort order among all items added to either object.


Adjust hoek to `^8.3.x`
breaking changes
Move default export to Sorter
Drop node 8
Update hoek dep
Remove types
Added TS declarations
Direct hoek method require
Update deps
Update deps
Change module namespace
Remove engines
Update hoek v6
Update implementation to use classes. Update license and travis.
breaking changes
Node 8
Commercial version of v2 branch
Cleanup sort routine, clarify usage of seq vs index
Ensure groups can be named after props of Object.prototype
A problem with group names that are also property names of Object.prototype
Add .npmignore, update code to v3
Test on node v6, update dependencies
breaking changes
es6. Closes #18
ES6 style changes and node v4
Lab v6 and travis node versions
Update to lab v6
merge() support. Closes #14
Add merge() support
Linting and repo style updates.
`before` option as array misbehaves
Fix bug with usage of `before` as array.
Update to Lab 5.x.x and Code 1.x.x
lab 4.0
Don't die when user adds enumerable properties to Array.prototype