topo
Installation:

npm: npm install @hapi/topo

yarn: yarn add @hapi/topo

Introduction

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

Example

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

const morning = new Topo.Sorter();

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

morning.add([
    'Make toast',
    'Pour juice'
], { before: 'breakfast', group: 'prep' });

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

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

Methods

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.

sorter.nodes

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

sorter.merge(others)

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.

Changelog

#57
Adjust hoek to `^8.3.x`
4.0.0
breaking changes
#55
Move default export to Sorter
#54
Drop node 8
#58
Update hoek dep
#56
Remove types
#46
Added TS declarations
#43
Direct hoek method require
#42
Update deps
#41
Update deps
#40
Change module namespace
#35
Remove engines
#33
Update hoek v6
#31
Cleanup
#30
Update implementation to use classes. Update license and travis.
3.0.0
breaking changes
#28
Node 8
#39
Commercial version of v2 branch
#26
Cleanup sort routine, clarify usage of seq vs index
#25
Ensure groups can be named after props of Object.prototype
#23
A problem with group names that are also property names of Object.prototype
#22
Add .npmignore, update code to v3
#21
Test on node v6, update dependencies
2.0.0
breaking changes
#19
es6. Closes #18
#18
ES6 style changes and node v4
#17
Lab v6 and travis node versions
#16
Update to lab v6
#15
merge() support. Closes #14
#14
Add merge() support
#10
Linting and repo style updates.
#9
`before` option as array misbehaves
#7
Fix bug with usage of `before` as array.
#6
Update to Lab 5.x.x and Code 1.x.x
#4
lab 4.0
#1
Don't die when user adds enumerable properties to Array.prototype
clipboard