We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5a14bc4 commit ab91e6fCopy full SHA for ab91e6f
README.md
@@ -14,7 +14,10 @@ sorted(["ab", "bc"]); // abc
14
15
// Add a comparison function to break ties.
16
import {increasing} from '@aureooms/js-compare';
17
-sorted(["ab", "cd"], increasing); // acbd
+sorted(["ab", "cd"], increasing); // abcd
18
+
19
+import {decreasing} from '@aureooms/js-compare';
20
+sorted(["ab", "cd"], decreasing); // cdab
21
```
22
23
[](https://raw.githubusercontent.com/aureooms/js-topological-sorting/main/LICENSE)
src/sorted.js
@@ -10,10 +10,10 @@ import kahn from './kahn.js';
10
* @param {(a: any, b: any) => Number} breakTies - The function to break ties.
11
* @returns {Iterable<any>} The vertices sorted in topological order.
12
*/
13
-export default function sorted(edges, breakTies = (_a, _b) => -1) {
+export default function sorted(edges, breakTies = undefined) {
const graph = Pairs.from(edges);
- const queue = new Heap(breakTies);
+ const queue = breakTies ? new Heap(breakTies) : [];
const freeVertices = new Set();
for (const [u] of graph) freeVertices.add(u);
for (const [, v] of graph) freeVertices.delete(v);
test/src/api.js
@@ -124,3 +124,21 @@ test('Triangle after edge', (t) => {
124
const edges = ['xa', 'ab', 'bc', 'ca'];
125
t.throws(() => [...sorted(edges)], {message: /cycle/});
126
});
127
128
+test('README example #1', (t) => {
129
+ const edges = ['ab', 'bc'];
130
+ const expected = ['a', 'b', 'c'];
131
+ t.deepEqual([...sorted(edges)], expected);
132
+});
133
134
+test('README example #2', (t) => {
135
+ const edges = ['ab', 'cd'];
136
+ const expected = ['a', 'b', 'c', 'd'];
137
+ t.deepEqual([...sorted(edges, increasing)], expected);
138
139
140
+test('README example #3', (t) => {
141
142
+ const expected = ['c', 'd', 'a', 'b'];
143
+ t.deepEqual([...sorted(edges, decreasing)], expected);
144
0 commit comments