Skip to content

Commit 8189bc7

Browse files
committed
Attempt tsc fix
1 parent 0893012 commit 8189bc7

34 files changed

Lines changed: 780 additions & 7 deletions

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
uses: bahmutov/npm-install@v1
2424

2525
- name: Check types
26-
run: npm run type-check
26+
run: npm run tsc
2727

2828
- name: Test
2929
run: npm run test

.travis.yml

Lines changed: 0 additions & 3 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"test": "vitest --run",
3636
"prepublishOnly": "npm run build",
3737
"prettier": "prettier --write .",
38-
"tsc": "tsc",
38+
"tsc": "tsc --noEmit",
3939
"release": "release-it"
4040
},
4141
"devDependencies": {

src/CycleError.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"use strict";
2+
var __extends = (this && this.__extends) || (function () {
3+
var extendStatics = function (d, b) {
4+
extendStatics = Object.setPrototypeOf ||
5+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7+
return extendStatics(d, b);
8+
};
9+
return function (d, b) {
10+
if (typeof b !== "function" && b !== null)
11+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12+
extendStatics(d, b);
13+
function __() { this.constructor = d; }
14+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15+
};
16+
})();
17+
Object.defineProperty(exports, "__esModule", { value: true });
18+
exports.CycleError = void 0;
19+
var CycleError = /** @class */ (function (_super) {
20+
__extends(CycleError, _super);
21+
function CycleError(message) {
22+
var _this = _super.call(this, message) || this;
23+
Object.setPrototypeOf(_this, CycleError.prototype);
24+
return _this;
25+
}
26+
return CycleError;
27+
}(Error));
28+
exports.CycleError = CycleError;

src/Graph.js

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.Graph = void 0;
4+
var invariant_js_1 = require("./invariant.js");
5+
var Graph = /** @class */ (function () {
6+
function Graph() {
7+
/**
8+
* Contains all the nodes added to the graph.
9+
*/
10+
this.nodes = new Set();
11+
/**
12+
* The adjacency list of the graph.
13+
*/
14+
this.edges = new Map();
15+
/**
16+
* The weights of edges.
17+
*
18+
* Map<SourceNode, Map<TargetNode, EdgeWeight>>
19+
*/
20+
this.edgeWeights = new Map();
21+
/**
22+
* Arbitrary properties of edges.
23+
* Map<SourceNode, Map<TargetNode, EdgeProperties>>
24+
*/
25+
this.edgeProperties = new Map();
26+
}
27+
/**
28+
* Adds a node to the graph.
29+
* If node was already added, this function does nothing.
30+
* If node was not already added, this function sets up an empty adjacency list.
31+
*/
32+
Graph.prototype.addNode = function (node) {
33+
if (!this.nodes.has(node)) {
34+
this.nodes.add(node);
35+
}
36+
if (!this.edges.has(node)) {
37+
this.edges.set(node, new Set());
38+
}
39+
return this;
40+
};
41+
/**
42+
* Removes a node from the graph.
43+
* Also removes incoming and outgoing edges.
44+
*/
45+
Graph.prototype.removeNode = function (node) {
46+
// Remove outgoing edges (and signal that the node no longer exists).
47+
this.edges.delete(node);
48+
this.nodes.delete(node);
49+
// Remove ingoing edges
50+
for (var _i = 0, _a = this.edges.values(); _i < _a.length; _i++) {
51+
var adjacentNodes = _a[_i];
52+
adjacentNodes.delete(node);
53+
}
54+
return this;
55+
};
56+
/**
57+
* Gets the adjacent nodes set for the given node.
58+
*/
59+
Graph.prototype.adjacent = function (node) {
60+
return this.edges.get(node);
61+
};
62+
/**
63+
* Sets the weight of the given edge.
64+
*/
65+
Graph.prototype.setEdgeWeight = function (source, target, weight) {
66+
if (!this.edgeWeights.has(source)) {
67+
this.edgeWeights.set(source, new Map());
68+
}
69+
var weights = this.edgeWeights.get(source);
70+
(0, invariant_js_1.invariant)(weights);
71+
weights.set(target, weight);
72+
return this;
73+
};
74+
/**
75+
* Gets the weight of the given edge or `1` if not set.
76+
*/
77+
Graph.prototype.getEdgeWeight = function (source, target) {
78+
var _a, _b;
79+
return (_b = (_a = this.edgeWeights.get(source)) === null || _a === void 0 ? void 0 : _a.get(target)) !== null && _b !== void 0 ? _b : 1;
80+
};
81+
/**
82+
* Set the properties of the given edge.
83+
*/
84+
Graph.prototype.setEdgeProperties = function (source, target, props) {
85+
if (!this.edgeProperties.has(source)) {
86+
this.edgeProperties.set(source, new Map());
87+
}
88+
var propsHolder = this.edgeProperties.get(source);
89+
(0, invariant_js_1.invariant)(propsHolder);
90+
propsHolder.set(target, props);
91+
return this;
92+
};
93+
/**
94+
* Get the properties of the given edge or undefined if none are set.
95+
*/
96+
Graph.prototype.getEdgeProperties = function (source, target) {
97+
var _a;
98+
return (_a = this.edgeProperties.get(source)) === null || _a === void 0 ? void 0 : _a.get(target);
99+
};
100+
/**
101+
* Adds an edge from the `source` node to `target` node.
102+
* This method will create the nodes if they were not already added.
103+
*/
104+
Graph.prototype.addEdge = function (source, target) {
105+
var opts = [];
106+
for (var _i = 2; _i < arguments.length; _i++) {
107+
opts[_i - 2] = arguments[_i];
108+
}
109+
var weight = opts[0], linkProps = opts[1];
110+
this.addNode(source);
111+
this.addNode(target);
112+
var adjacentNodes = this.adjacent(source);
113+
(0, invariant_js_1.invariant)(adjacentNodes);
114+
adjacentNodes.add(target);
115+
if (weight !== undefined) {
116+
this.setEdgeWeight(source, target, weight);
117+
}
118+
if (linkProps !== undefined) {
119+
this.setEdgeProperties(source, target, linkProps);
120+
}
121+
return this;
122+
};
123+
/**
124+
* Removes the edge from the `source` node to `target` node.
125+
* Does not remove the nodes themselves.
126+
* Does nothing if the edge does not exist.
127+
*/
128+
Graph.prototype.removeEdge = function (source, target) {
129+
var _a, _b;
130+
(_a = this.edges.get(source)) === null || _a === void 0 ? void 0 : _a.delete(target);
131+
(_b = this.edgeProperties.get(source)) === null || _b === void 0 ? void 0 : _b.delete(target);
132+
return this;
133+
};
134+
/**
135+
* Returns true if there is an edge from the `source` node to `target` node..
136+
*/
137+
Graph.prototype.hasEdge = function (source, target) {
138+
var _a, _b;
139+
return (_b = (_a = this.edges.get(source)) === null || _a === void 0 ? void 0 : _a.has(target)) !== null && _b !== void 0 ? _b : false;
140+
};
141+
return Graph;
142+
}());
143+
exports.Graph = Graph;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.depthFirstSearch = depthFirstSearch;
4+
var depthFirstVisit_js_1 = require("./depthFirstVisit.js");
5+
/**
6+
* Depth First Search algorithm, inspired by
7+
* Cormen et al. "Introduction to Algorithms" 3rd Ed. p. 604
8+
*/
9+
function depthFirstSearch(graph, opts) {
10+
var _a;
11+
if (opts === void 0) { opts = {}; }
12+
var _b = opts.sourceNodes, sourceNodes = _b === void 0 ? Array.from(graph.nodes) : _b, _c = opts.includeSourceNodes, includeSourceNodes = _c === void 0 ? true : _c;
13+
var visited = new Set();
14+
var visiting = new Set();
15+
var nodeList = [];
16+
if (includeSourceNodes) {
17+
for (var i = 0; i < sourceNodes.length; i++) {
18+
var sourceNode = sourceNodes[i];
19+
if (!sourceNode)
20+
continue;
21+
(0, depthFirstVisit_js_1.depthFirstVisit)(graph, nodeList, visited, visiting, sourceNode, opts);
22+
}
23+
return nodeList;
24+
}
25+
for (var i = 0; i < sourceNodes.length; i++) {
26+
var sourceNode = sourceNodes[i];
27+
if (!sourceNode)
28+
continue;
29+
visited.add(sourceNode);
30+
}
31+
for (var i = 0; i < sourceNodes.length; i++) {
32+
var sourceNode = sourceNodes[i];
33+
if (!sourceNode)
34+
continue;
35+
(_a = graph
36+
.adjacent(sourceNode)) === null || _a === void 0 ? void 0 : _a.forEach(function (n) { return (0, depthFirstVisit_js_1.depthFirstVisit)(graph, nodeList, visited, visiting, n, opts); });
37+
}
38+
return nodeList;
39+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.depthFirstVisit = depthFirstVisit;
4+
var CycleError_js_1 = require("../../CycleError.js");
5+
function depthFirstVisit(graph, nodeList, visited, visiting, node, opts) {
6+
var _a;
7+
var _b = opts.errorOnCycle, errorOnCycle = _b === void 0 ? false : _b, shouldFollow = opts.shouldFollow;
8+
if (visiting.has(node) && errorOnCycle) {
9+
throw new CycleError_js_1.CycleError('Cycle found');
10+
}
11+
if (!visited.has(node)) {
12+
visited.add(node);
13+
visiting.add(node);
14+
(_a = graph.adjacent(node)) === null || _a === void 0 ? void 0 : _a.forEach(function (n) {
15+
var follow = shouldFollow === undefined || shouldFollow({ source: node, target: n, graph: graph });
16+
if (!follow)
17+
return;
18+
depthFirstVisit(graph, nodeList, visited, visiting, n, opts);
19+
});
20+
visiting.delete(node);
21+
nodeList.push(node);
22+
}
23+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.depthFirstSearch = void 0;
4+
var depthFirstSearch_js_1 = require("./depthFirstSearch.js");
5+
Object.defineProperty(exports, "depthFirstSearch", { enumerable: true, get: function () { return depthFirstSearch_js_1.depthFirstSearch; } });
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.lowestCommonAncestors = void 0;
4+
var lowestCommonAncestors_js_1 = require("./lowestCommonAncestors.js");
5+
Object.defineProperty(exports, "lowestCommonAncestors", { enumerable: true, get: function () { return lowestCommonAncestors_js_1.lowestCommonAncestors; } });

0 commit comments

Comments
 (0)