Skip to content

Commit bdd9b12

Browse files
authored
Merge pull request #87 from JesusTheHun/new-major-wrap-up
Wrap up details for new major version release
2 parents 07b00b1 + 70e7bc8 commit bdd9b12

38 files changed

+87
-814
lines changed

package-lock.json

Lines changed: 26 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
"version": "3.5.0",
44
"description": "A graph data structure with topological sort.",
55
"author": "Curran Kelleher",
6+
"contributors": [{
7+
"name": "Jonathan MASSUCHETTI",
8+
"email": "jonathan.massuchetti@dappit.fr",
9+
"url": "https://github.com/JesusTheHun"
10+
}],
611
"license": "MIT",
712
"bugs": {
813
"url": "https://github.com/datavis-tech/graph-data-structure/issues"
@@ -35,7 +40,7 @@
3540
"test": "vitest --run",
3641
"prepublishOnly": "npm run build",
3742
"prettier": "prettier --write .",
38-
"tsc": "tsc --noEmit",
43+
"tsc": "tsc --noEmit --noEmitOnError",
3944
"release": "release-it"
4045
},
4146
"devDependencies": {

src/CycleError.js

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

src/Graph.js

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

src/Graph.spec-d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,14 @@ describe('graph types', () => {
3838
it('should require edge properties if LinkProps is defined', () => {
3939
const g = new Graph<string, { type: string }>();
4040

41-
g.addEdge('a', 'b', undefined, { type: 'foo' });
42-
g.addEdge('a', 'b', 1, { type: 'foo' });
41+
g.addEdge('a', 'b', { props: { type: 'foo' } });
42+
g.addEdge('a', 'b', { weight: 1, props: { type: 'foo' } });
4343

4444
// @ts-expect-error
4545
g.addEdge('a', 'b', 1);
46+
47+
// @ts-expect-error
48+
g.addEdge('a', 'b', { weight: 1 });
4649
});
4750

4851
it('should not allow edge properties if LinkProps is never', () => {

src/Graph.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,25 @@ export class Graph<Node = string, LinkProps = never> {
116116
* Adds an edge from the `source` node to `target` node.
117117
* This method will create the nodes if they were not already added.
118118
*/
119-
addEdge(
120-
source: Node,
121-
target: Node,
122-
...opts: [LinkProps] extends [never]
123-
? [weight?: EdgeWeight]
124-
: [weight: EdgeWeight | undefined, linkProps: LinkProps]
125-
): this {
126-
const [weight, linkProps] = opts;
119+
addEdge(source: Node, target: Node, ...args: AddEdgeArgs<LinkProps>): this {
120+
let weight: number | undefined;
121+
let linkProps: LinkProps | undefined;
122+
123+
const firstArg = args[0];
124+
125+
if (typeof firstArg === 'number') {
126+
weight = firstArg;
127+
}
128+
129+
if (typeof firstArg === 'object') {
130+
weight = firstArg.weight;
131+
132+
if (firstArg)
133+
linkProps = Object.prototype.hasOwnProperty.call(firstArg, 'props')
134+
? (firstArg as { props: LinkProps }).props
135+
: undefined;
136+
}
137+
127138
this.addNode(source);
128139
this.addNode(target);
129140
const adjacentNodes = this.adjacent(source);
@@ -162,3 +173,7 @@ export class Graph<Node = string, LinkProps = never> {
162173
return this.edges.get(source)?.has(target) ?? false;
163174
}
164175
}
176+
177+
type AddEdgeArgs<LinkProps> = [LinkProps] extends [never]
178+
? [weight?: EdgeWeight] | [opts?: { weight?: EdgeWeight }]
179+
: [opts: { weight?: EdgeWeight; props: LinkProps }];

src/algorithms/depthFirstSearch/depthFirstSearch.js

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

src/algorithms/depthFirstSearch/depthFirstSearch.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ describe('depthFirstSearch', () => {
66
it('Should return the nodes connected to the source node with the correct type of edge.', function () {
77
const graph = new Graph<string, { type: 'foo' | 'bar' }>();
88

9-
graph.addEdge('a', 'b', undefined, { type: 'foo' });
10-
graph.addEdge('b', 'c', undefined, { type: 'bar' });
11-
graph.addEdge('b', 'd', undefined, { type: 'bar' });
12-
graph.addEdge('b', 'e', undefined, { type: 'foo' });
9+
graph.addEdge('a', 'b', { props: { type: 'foo' } });
10+
graph.addEdge('b', 'c', { props: { type: 'bar' } });
11+
graph.addEdge('b', 'd', { props: { type: 'bar' } });
12+
graph.addEdge('b', 'e', { props: { type: 'foo' } });
1313

1414
const nodes = depthFirstSearch(graph, {
1515
shouldFollow: ({ source, target, graph }) =>

0 commit comments

Comments
 (0)