Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ class Graph {
return this._label;
}


/* === Node functions ========== */

/**
Expand Down Expand Up @@ -165,7 +164,14 @@ class Graph {
*/
sinks() {
var self = this;
return this.nodes().filter(v => Object.keys(self._out[v]).length === 0);
return this.nodes().filter(v => {
let isEmpty = Object.keys(self._out[v]).length === 0;
if (self._isDirected) {
isEmpty = isEmpty && Object.keys(self._in[v]).length === 0;
}

return isEmpty;
});
}

/**
Expand Down
12 changes: 11 additions & 1 deletion test/graph-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

describe("Graph", function() {
var g;
var gUndirected;

beforeEach(function() {
g = new Graph();
gUndirected = new Graph({

Check failure on line 9 in test/graph-test.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

Mixed spaces and tabs
directed: false

Check failure on line 10 in test/graph-test.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

Expected indentation of 6 spaces but found 2 tabs
});

Check failure on line 11 in test/graph-test.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

Expected indentation of 4 spaces but found 1 tab
});

describe("initial state", function() {
Expand Down Expand Up @@ -80,11 +84,17 @@
});

describe("sinks", function() {
it("returns nodes in the graph that have no out-edges", function() {
it("returns nodes in the graph that have no out-edges (directed graph)", function() {
g.setPath(["a", "b", "c"]);
g.setNode("d");
expect(g.sinks().sort()).toEqual(["c", "d"]);
});

it("returns nodes in the graph that have no edges (undirected graph)", function() {
gUndirected.setPath(["a", "b", "c", "d"]);
gUndirected.removeNode("c");
expect(_.sortBy(gUndirected.sinks())).to.eql(["d"]);

Check failure on line 96 in test/graph-test.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

'_' is not defined
});
});

describe("filterNodes", function() {
Expand Down
Loading