├── .github
└── workflows
│ └── ci-module.yml
├── .gitignore
├── API.md
├── LICENSE.md
├── README.md
├── lib
├── index.d.ts
└── index.js
├── package.json
└── test
├── index.js
└── index.ts
/.github/workflows/ci-module.yml:
--------------------------------------------------------------------------------
1 | name: ci
2 |
3 | on:
4 | push:
5 | branches:
6 | - master
7 | pull_request:
8 | workflow_dispatch:
9 |
10 | jobs:
11 | test:
12 | uses: hapijs/.github/.github/workflows/ci-module.yml@master
13 | with:
14 | min-node-version: 14
15 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | **/node_modules
2 | **/package-lock.json
3 |
4 | coverage.*
5 |
6 | **/.DS_Store
7 | **/._*
8 |
9 | **/*.pem
10 |
11 | **/.vs
12 | **/.vscode
13 | **/.idea
14 |
--------------------------------------------------------------------------------
/API.md:
--------------------------------------------------------------------------------
1 | ## Introduction
2 |
3 | The `Topo` object is the container for topologically sorting a list of nodes with non-circular interdependencies.
4 |
5 | ## Example
6 |
7 | ```js
8 | const Topo = require('@hapi/topo');
9 |
10 | const morning = new Topo.Sorter();
11 |
12 | morning.add('Nap', { after: ['breakfast', 'prep'] });
13 |
14 | morning.add([
15 | 'Make toast',
16 | 'Pour juice'
17 | ], { before: 'breakfast', group: 'prep' });
18 |
19 | morning.add('Eat breakfast', { group: 'breakfast' });
20 |
21 | morning.nodes; // ['Make toast', 'Pour juice', 'Eat breakfast', 'Nap']
22 | ```
23 |
24 | ## Methods
25 |
26 | ### `new Sorter()`
27 |
28 | Creates a new `Sorter` object.
29 |
30 | ### `sorter.add(nodes, [options])`
31 |
32 | Specifies an additional node or list of nodes to be topologically sorted where:
33 | - `nodes` - a mixed value or array of mixed values to be added as nodes to the topologically sorted list.
34 | - `options` - optional sorting information about the `nodes`:
35 | - `group` - a string naming the group to which `nodes` should be assigned. The group name `'?'` is reserved.
36 | - `before` - a string or array of strings specifying the groups that `nodes` must precede in the topological sort.
37 | - `after` - a string or array of strings specifying the groups that `nodes` must succeed in the topological sort.
38 | - `sort` - a numerical value used to sort items when performing a `sorter.merge()`.
39 | - `manual` - if `true`, the array is not sorted automatically and `sorter.sort()` must be called when done adding items.
40 |
41 | Returns an array of the topologically sorted nodes (unless `manual` is used in which case the array is left unchanged).
42 |
43 | ### `sorter.nodes`
44 |
45 | An array of the topologically sorted nodes. This list is renewed upon each call to [`sorter.add()`](#topoaddnodes-options) unless
46 | `manual` is used.
47 |
48 | ### `sorter.merge(others)`
49 |
50 | Merges another `Sorter` object into the current object where:
51 | - `others` - the other object or array of objects to be merged into the current one. `null`
52 | values are ignored.
53 |
54 | Returns an array of the topologically sorted nodes. Will throw if a dependency error is found as a result of the
55 | combined items.
56 |
57 | If the order in which items have been added to each list matters, use the `sort` option in `sorter.add()` with an incrementing
58 | value providing an absolute sort order among all items added to either object.
59 |
60 | ### `sorter.sort()`
61 |
62 | Sorts the array. Only needed if the `manual` option is used when `add()` is called.
63 |
64 | Returns an array of the topologically sorted nodes. Will throw if a dependency error is found.
65 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | Copyright (c) 2012-2022, Project contributors
2 | Copyright (c) 2012-2020, Sideway Inc
3 | Copyright (c) 2012-2014, Walmart.
4 | All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9 | * The names of any contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
10 |
11 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS OFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | # @hapi/topo
4 |
5 | #### Topological sorting with grouping support.
6 |
7 | **topo** is part of the **hapi** ecosystem and was designed to work seamlessly with the [hapi web framework](https://hapi.dev) and its other components (but works great on its own or with other frameworks). If you are using a different web framework and find this module useful, check out [hapi](https://hapi.dev) – they work even better together.
8 |
9 | ### Visit the [hapi.dev](https://hapi.dev) Developer Portal for tutorials, documentation, and support
10 |
11 | ## Useful resources
12 |
13 | - [Documentation and API](https://hapi.dev/family/topo/)
14 | - [Version status](https://hapi.dev/resources/status/#topo) (builds, dependencies, node versions, licenses, eol)
15 | - [Changelog](https://hapi.dev/family/topo/changelog/)
16 | - [Project policies](https://hapi.dev/policies/)
17 | - [Free and commercial support options](https://hapi.dev/support/)
18 |
--------------------------------------------------------------------------------
/lib/index.d.ts:
--------------------------------------------------------------------------------
1 | export class Sorter {
2 |
3 | /**
4 | * An array of the topologically sorted nodes. This list is renewed upon each call to topo.add().
5 | */
6 | nodes: T[];
7 |
8 | /**
9 | * Adds a node or list of nodes to be added and topologically sorted
10 | *
11 | * @param nodes - A mixed value or array of mixed values to be added as nodes to the topologically sorted list.
12 | * @param options - Optional sorting information about the nodes.
13 | *
14 | * @returns Returns an array of the topologically sorted nodes.
15 | */
16 | add(nodes: T | T[], options?: Options): T[];
17 |
18 | /**
19 | * Merges another Sorter object into the current object.
20 | *
21 | * @param others - The other object or array of objects to be merged into the current one.
22 | *
23 | * @returns Returns an array of the topologically sorted nodes.
24 | */
25 | merge(others: Sorter | Sorter[]): T[];
26 |
27 | /**
28 | * Sorts the nodes array (only required if the manual option is used when adding items)
29 | */
30 | sort(): T[];
31 | }
32 |
33 |
34 | export interface Options {
35 |
36 | /**
37 | * The sorting group the added items belong to
38 | */
39 | readonly group?: string;
40 |
41 | /**
42 | * The group or groups the added items must come before
43 | */
44 | readonly before?: string | string[];
45 |
46 | /**
47 | * The group or groups the added items must come after
48 | */
49 | readonly after?: string | string[];
50 |
51 | /**
52 | * A number used to sort items with equal before/after requirements
53 | */
54 | readonly sort?: number;
55 |
56 | /**
57 | * If true, the array is not sorted automatically until sort() is called
58 | */
59 | readonly manual?: boolean;
60 | }
61 |
--------------------------------------------------------------------------------
/lib/index.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | const { assert } = require('@hapi/hoek');
4 |
5 |
6 | const internals = {};
7 |
8 |
9 | exports.Sorter = class {
10 |
11 | constructor() {
12 |
13 | this._items = [];
14 | this.nodes = [];
15 | }
16 |
17 | add(nodes, options) {
18 |
19 | options = options ?? {};
20 |
21 | // Validate rules
22 |
23 | const before = [].concat(options.before ?? []);
24 | const after = [].concat(options.after ?? []);
25 | const group = options.group ?? '?';
26 | const sort = options.sort ?? 0; // Used for merging only
27 |
28 | assert(!before.includes(group), `Item cannot come before itself: ${group}`);
29 | assert(!before.includes('?'), 'Item cannot come before unassociated items');
30 | assert(!after.includes(group), `Item cannot come after itself: ${group}`);
31 | assert(!after.includes('?'), 'Item cannot come after unassociated items');
32 |
33 | if (!Array.isArray(nodes)) {
34 | nodes = [nodes];
35 | }
36 |
37 | for (const node of nodes) {
38 | const item = {
39 | seq: this._items.length,
40 | sort,
41 | before,
42 | after,
43 | group,
44 | node
45 | };
46 |
47 | this._items.push(item);
48 | }
49 |
50 | // Insert event
51 |
52 | if (!options.manual) {
53 | const valid = this._sort();
54 | assert(valid, 'item', group !== '?' ? `added into group ${group}` : '', 'created a dependencies error');
55 | }
56 |
57 | return this.nodes;
58 | }
59 |
60 | merge(others) {
61 |
62 | if (!Array.isArray(others)) {
63 | others = [others];
64 | }
65 |
66 | for (const other of others) {
67 | if (other) {
68 | for (const item of other._items) {
69 | this._items.push(Object.assign({}, item)); // Shallow cloned
70 | }
71 | }
72 | }
73 |
74 | // Sort items
75 |
76 | this._items.sort(internals.mergeSort);
77 | for (let i = 0; i < this._items.length; ++i) {
78 | this._items[i].seq = i;
79 | }
80 |
81 | const valid = this._sort();
82 | assert(valid, 'merge created a dependencies error');
83 |
84 | return this.nodes;
85 | }
86 |
87 | sort() {
88 |
89 | const valid = this._sort();
90 | assert(valid, 'sort created a dependencies error');
91 |
92 | return this.nodes;
93 | }
94 |
95 | _sort() {
96 |
97 | // Construct graph
98 |
99 | const graph = {};
100 | const graphAfters = Object.create(null); // A prototype can bungle lookups w/ false positives
101 | const groups = Object.create(null);
102 |
103 | for (const item of this._items) {
104 | const seq = item.seq; // Unique across all items
105 | const group = item.group;
106 |
107 | // Determine Groups
108 |
109 | groups[group] = groups[group] ?? [];
110 | groups[group].push(seq);
111 |
112 | // Build intermediary graph using 'before'
113 |
114 | graph[seq] = item.before;
115 |
116 | // Build second intermediary graph with 'after'
117 |
118 | for (const after of item.after) {
119 | graphAfters[after] = graphAfters[after] ?? [];
120 | graphAfters[after].push(seq);
121 | }
122 | }
123 |
124 | // Expand intermediary graph
125 |
126 | for (const node in graph) {
127 | const expandedGroups = [];
128 |
129 | for (const graphNodeItem in graph[node]) {
130 | const group = graph[node][graphNodeItem];
131 | groups[group] = groups[group] ?? [];
132 | expandedGroups.push(...groups[group]);
133 | }
134 |
135 | graph[node] = expandedGroups;
136 | }
137 |
138 | // Merge intermediary graph using graphAfters into final graph
139 |
140 | for (const group in graphAfters) {
141 | if (groups[group]) {
142 | for (const node of groups[group]) {
143 | graph[node].push(...graphAfters[group]);
144 | }
145 | }
146 | }
147 |
148 | // Compile ancestors
149 |
150 | const ancestors = {};
151 | for (const node in graph) {
152 | const children = graph[node];
153 | for (const child of children) {
154 | ancestors[child] = ancestors[child] ?? [];
155 | ancestors[child].push(node);
156 | }
157 | }
158 |
159 | // Topo sort
160 |
161 | const visited = {};
162 | const sorted = [];
163 |
164 | for (let i = 0; i < this._items.length; ++i) { // Looping through item.seq values out of order
165 | let next = i;
166 |
167 | if (ancestors[i]) {
168 | next = null;
169 | for (let j = 0; j < this._items.length; ++j) { // As above, these are item.seq values
170 | if (visited[j] === true) {
171 | continue;
172 | }
173 |
174 | if (!ancestors[j]) {
175 | ancestors[j] = [];
176 | }
177 |
178 | const shouldSeeCount = ancestors[j].length;
179 | let seenCount = 0;
180 | for (let k = 0; k < shouldSeeCount; ++k) {
181 | if (visited[ancestors[j][k]]) {
182 | ++seenCount;
183 | }
184 | }
185 |
186 | if (seenCount === shouldSeeCount) {
187 | next = j;
188 | break;
189 | }
190 | }
191 | }
192 |
193 | if (next !== null) {
194 | visited[next] = true;
195 | sorted.push(next);
196 | }
197 | }
198 |
199 | if (sorted.length !== this._items.length) {
200 | return false;
201 | }
202 |
203 | const seqIndex = {};
204 | for (const item of this._items) {
205 | seqIndex[item.seq] = item;
206 | }
207 |
208 | this._items = [];
209 | this.nodes = [];
210 |
211 | for (const value of sorted) {
212 | const sortedItem = seqIndex[value];
213 | this.nodes.push(sortedItem.node);
214 | this._items.push(sortedItem);
215 | }
216 |
217 | return true;
218 | }
219 | };
220 |
221 |
222 | internals.mergeSort = (a, b) => {
223 |
224 | return a.sort === b.sort ? 0 : (a.sort < b.sort ? -1 : 1);
225 | };
226 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "@hapi/topo",
3 | "description": "Topological sorting with grouping support",
4 | "version": "6.0.2",
5 | "repository": "git://github.com/hapijs/topo",
6 | "main": "lib/index.js",
7 | "types": "lib/index.d.ts",
8 | "files": [
9 | "lib"
10 | ],
11 | "keywords": [
12 | "topological",
13 | "sort",
14 | "toposort",
15 | "topsort"
16 | ],
17 | "eslintConfig": {
18 | "extends": [
19 | "plugin:@hapi/module"
20 | ]
21 | },
22 | "dependencies": {
23 | "@hapi/hoek": "^11.0.2"
24 | },
25 | "devDependencies": {
26 | "@hapi/code": "^9.0.3",
27 | "@hapi/eslint-plugin": "^6.0.0",
28 | "@hapi/lab": "^25.1.2",
29 | "@types/node": "^17.0.31",
30 | "typescript": "~4.6.4"
31 | },
32 | "scripts": {
33 | "test": "lab -a @hapi/code -t 100 -L -Y",
34 | "test-cov-html": "lab -a @hapi/code -t 100 -L -r html -o coverage.html"
35 | },
36 | "license": "BSD-3-Clause"
37 | }
38 |
--------------------------------------------------------------------------------
/test/index.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | const Code = require('@hapi/code');
4 | const Hoek = require('@hapi/hoek');
5 | const Lab = require('@hapi/lab');
6 | const Topo = require('..');
7 |
8 |
9 | const internals = {};
10 |
11 |
12 | const { describe, it } = exports.lab = Lab.script();
13 | const expect = Code.expect;
14 |
15 |
16 | describe('Sorter', () => {
17 |
18 | const testDeps = function (scenario) {
19 |
20 | const topo = new Topo.Sorter();
21 | for (const record of scenario) {
22 | const options = record.before || record.after || record.group ? { before: record.before, after: record.after, group: record.group } : null;
23 | topo.add(record.id, options);
24 | }
25 |
26 | return topo.nodes.join('');
27 | };
28 |
29 | it('sorts dependencies', () => {
30 |
31 | const scenario = [
32 | { id: '0', before: 'a' },
33 | { id: '1', after: 'f', group: 'a' },
34 | { id: '2', before: 'a' },
35 | { id: '3', before: ['b', 'c'], group: 'a' },
36 | { id: '4', after: 'c', group: 'b' },
37 | { id: '5', group: 'c' },
38 | { id: '6', group: 'd' },
39 | { id: '7', group: 'e' },
40 | { id: '8', before: 'd' },
41 | { id: '9', after: 'c', group: 'a' }
42 | ];
43 |
44 | expect(testDeps(scenario)).to.equal('0213547869');
45 | });
46 |
47 | it('sorts dependencies (manual)', () => {
48 |
49 | const scenario = [
50 | { id: '0', before: 'a' },
51 | { id: '1', after: 'f', group: 'a' },
52 | { id: '2', before: 'a' },
53 | { id: '3', before: ['b', 'c'], group: 'a' },
54 | { id: '4', after: 'c', group: 'b' },
55 | { id: '5', group: 'c' },
56 | { id: '6', group: 'd' },
57 | { id: '7', group: 'e' },
58 | { id: '8', before: 'd' },
59 | { id: '9', after: 'c', group: 'a' }
60 | ];
61 |
62 | const topo = new Topo.Sorter();
63 | for (const record of scenario) {
64 | const options = record.before || record.after || record.group ? { before: record.before, after: record.after, group: record.group } : null;
65 | topo.add(record.id, { ...options, manual: true });
66 | }
67 |
68 | expect(topo.nodes.join('')).to.equal('');
69 | expect(topo.sort().join('')).to.equal('0213547869');
70 | expect(topo.nodes.join('')).to.equal('0213547869');
71 | });
72 |
73 | it('sorts dependencies (before as array)', () => {
74 |
75 | const scenario = [
76 | { id: '0', group: 'a' },
77 | { id: '1', group: 'b' },
78 | { id: '2', before: ['a', 'b'] }
79 | ];
80 |
81 | expect(testDeps(scenario)).to.equal('201');
82 | });
83 |
84 | it('sorts dependencies (after as array)', () => {
85 |
86 | const scenario = [
87 | { id: '0', after: ['a', 'b'] },
88 | { id: '1', group: 'a' },
89 | { id: '2', group: 'b' }
90 | ];
91 |
92 | expect(testDeps(scenario)).to.equal('120');
93 | });
94 |
95 | it('sorts dependencies (seq)', () => {
96 |
97 | const scenario = [
98 | { id: '0' },
99 | { id: '1' },
100 | { id: '2' },
101 | { id: '3' }
102 | ];
103 |
104 | expect(testDeps(scenario)).to.equal('0123');
105 | });
106 |
107 | it('sorts dependencies (explicitly using after or before)', () => {
108 |
109 | const set = '0123456789abcdefghijklmnopqrstuvwxyz';
110 | const groups = set.split('');
111 |
112 | // Use Fisher-Yates for shuffling
113 |
114 | const fisherYates = function (array) {
115 |
116 | let i = array.length;
117 | while (--i) {
118 | const j = Math.floor(Math.random() * (i + 1));
119 | const tempi = array[i];
120 | const tempj = array[j];
121 | array[i] = tempj;
122 | array[j] = tempi;
123 | }
124 | };
125 |
126 | const scenarioAfter = [];
127 | const scenarioBefore = [];
128 | for (let i = 0; i < groups.length; ++i) {
129 | const item = {
130 | id: groups[i],
131 | group: groups[i]
132 | };
133 | const afterMod = {
134 | after: i ? groups.slice(0, i) : []
135 | };
136 | const beforeMod = {
137 | before: groups.slice(i + 1)
138 | };
139 |
140 | scenarioAfter.push(Hoek.applyToDefaults(item, afterMod));
141 | scenarioBefore.push(Hoek.applyToDefaults(item, beforeMod));
142 | }
143 |
144 | fisherYates(scenarioAfter);
145 | expect(testDeps(scenarioAfter)).to.equal(set);
146 |
147 | fisherYates(scenarioBefore);
148 | expect(testDeps(scenarioBefore)).to.equal(set);
149 | });
150 |
151 | it('throws on circular dependency', () => {
152 |
153 | const scenario = [
154 | { id: '0', before: 'a', group: 'b' },
155 | { id: '1', before: 'c', group: 'a' },
156 | { id: '2', before: 'b', group: 'c' }
157 | ];
158 |
159 | expect(() => {
160 |
161 | testDeps(scenario);
162 | }).to.throw('item added into group c created a dependencies error');
163 | });
164 |
165 | it('can handle groups named after properties of Object.prototype', () => {
166 |
167 | const scenario = [
168 | { id: '0', after: ['valueOf', 'toString'] },
169 | { id: '1', group: 'valueOf' },
170 | { id: '2', group: 'toString' }
171 | ];
172 |
173 | expect(testDeps(scenario)).to.equal('120');
174 | });
175 |
176 | describe('merge()', () => {
177 |
178 | it('merges objects', () => {
179 |
180 | const topo = new Topo.Sorter();
181 | topo.add(['0'], { before: 'a' });
182 | topo.add('2', { before: 'a' });
183 | topo.add('4', { after: 'c', group: 'b' });
184 | topo.add('6', { group: 'd' });
185 | topo.add('8', { before: 'd' });
186 | expect(topo.nodes.join('')).to.equal('02486');
187 |
188 | const other = new Topo.Sorter();
189 | other.add('1', { after: 'f', group: 'a' });
190 | other.add('3', { before: ['b', 'c'], group: 'a' });
191 | other.add('5', { group: 'c' });
192 | other.add('7', { group: 'e' });
193 | other.add('9', { after: 'c', group: 'a' });
194 | expect(other.nodes.join('')).to.equal('13579');
195 |
196 | topo.merge(other);
197 | expect(topo.nodes.join('')).to.equal('0286135479');
198 | });
199 |
200 | it('merges objects (explicit sort)', () => {
201 |
202 | const topo = new Topo.Sorter();
203 | topo.add('0', { before: 'a', sort: 1 });
204 | topo.add('2', { before: 'a', sort: 2 });
205 | topo.add('4', { after: 'c', group: 'b', sort: 3 });
206 | topo.add('6', { group: 'd', sort: 4 });
207 | topo.add('8', { before: 'd', sort: 5 });
208 | expect(topo.nodes.join('')).to.equal('02486');
209 |
210 | const other = new Topo.Sorter();
211 | other.add('1', { after: 'f', group: 'a', sort: 6 });
212 | other.add('3', { before: ['b', 'c'], group: 'a', sort: 7 });
213 | other.add('5', { group: 'c', sort: 8 });
214 | other.add('7', { group: 'e', sort: 9 });
215 | other.add('9', { after: 'c', group: 'a', sort: 10 });
216 | expect(other.nodes.join('')).to.equal('13579');
217 |
218 | topo.merge(other);
219 | expect(topo.nodes.join('')).to.equal('0286135479');
220 | });
221 |
222 | it('merges objects (mixed sort)', () => {
223 |
224 | const topo = new Topo.Sorter();
225 | topo.add('0', { before: 'a', sort: 1 });
226 | topo.add('2', { before: 'a', sort: 3 });
227 | topo.add('4', { after: 'c', group: 'b', sort: 5 });
228 | topo.add('6', { group: 'd', sort: 7 });
229 | topo.add('8', { before: 'd', sort: 9 });
230 | expect(topo.nodes.join('')).to.equal('02486');
231 |
232 | const other = new Topo.Sorter();
233 | other.add('1', { after: 'f', group: 'a', sort: 2 });
234 | other.add('3', { before: ['b', 'c'], group: 'a', sort: 4 });
235 | other.add('5', { group: 'c', sort: 6 });
236 | other.add('7', { group: 'e', sort: 8 });
237 | other.add('9', { after: 'c', group: 'a', sort: 10 });
238 | expect(other.nodes.join('')).to.equal('13579');
239 |
240 | topo.merge(other);
241 | expect(topo.nodes.join('')).to.equal('0213547869');
242 | });
243 |
244 | it('merges objects (multiple)', () => {
245 |
246 | const topo1 = new Topo.Sorter();
247 | topo1.add('0', { before: 'a', sort: 1 });
248 | topo1.add('2', { before: 'a', sort: 3 });
249 | topo1.add('4', { after: 'c', group: 'b', sort: 5 });
250 |
251 | const topo2 = new Topo.Sorter();
252 | topo2.add('6', { group: 'd', sort: 7 });
253 | topo2.add('8', { before: 'd', sort: 9 });
254 |
255 | const other = new Topo.Sorter();
256 | other.add('1', { after: 'f', group: 'a', sort: 2 });
257 | other.add('3', { before: ['b', 'c'], group: 'a', sort: 4 });
258 | other.add('5', { group: 'c', sort: 6 });
259 | other.add('7', { group: 'e', sort: 8 });
260 | other.add('9', { after: 'c', group: 'a', sort: 10 });
261 | expect(other.nodes.join('')).to.equal('13579');
262 |
263 | topo1.merge([topo2, null, other]);
264 | expect(topo1.nodes.join('')).to.equal('0213547869');
265 | });
266 |
267 | it('throws on circular dependency', () => {
268 |
269 | const topo = new Topo.Sorter();
270 | topo.add('0', { before: 'a', group: 'b' });
271 | topo.add('1', { before: 'c', group: 'a' });
272 |
273 | const other = new Topo.Sorter();
274 | other.add('2', { before: 'b', group: 'c' });
275 |
276 | expect(() => {
277 |
278 | topo.merge(other);
279 | }).to.throw('merge created a dependencies error');
280 | });
281 | });
282 | });
283 |
--------------------------------------------------------------------------------
/test/index.ts:
--------------------------------------------------------------------------------
1 | import * as Topo from '..';
2 | import * as Lab from '@hapi/lab';
3 |
4 |
5 | const { expect } = Lab.types;
6 |
7 |
8 | // new Topo.Sorter()
9 |
10 | const morning = new Topo.Sorter();
11 | morning.add('Nap', { after: ['breakfast', 'prep'] })
12 | morning.add(['Make toast', 'Pour juice'], { before: 'breakfast', group: 'prep' });
13 | morning.add('Eat breakfast', { group: 'breakfast' });
14 |
15 | const afternoon = new Topo.Sorter();
16 | afternoon.add('Eat lunch', { after: ['afternoon', 'prep'], sort: 2 });
17 |
18 | expect.type