Skip to content

Commit

Permalink
Add edge only once (#4)
Browse files Browse the repository at this point in the history
* package: improve format

* graph: addEdge does nothing if edges already exists

* package: bump version to 0.1.4
  • Loading branch information
hbbio committed Apr 2, 2024
1 parent 1d112a4 commit 33c159f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@okcontract/graph",
"version": "0.1.3",
"version": "0.1.4",
"description": "Directed graph library",
"private": false,
"main": "dist/graph.umd.cjs",
Expand Down Expand Up @@ -32,7 +32,7 @@
"definitions": "tsc --project tsconfig.build.json",
"prepublishOnly": "npm run check && npm test && npm run build && npm run definitions",
"check": "npx @biomejs/biome check src",
"format": "npx @biomejs/biome format src --write",
"format": "npx @biomejs/biome format src --write && npx @biomejs/biome check src --apply",
"formatReadme": "prettier README.md --prose-wrap always --print-width 78 -w"
},
"repository": {
Expand All @@ -41,4 +41,4 @@
},
"author": "Henri Binsztok",
"license": "MIT"
}
}
9 changes: 9 additions & 0 deletions src/graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ test("should add edges correctly", () => {
expect(graph.get("a")).toEqual(["b"]);
});

test("does not add the same edge twice", () => {
const graph = new Graph<string>();
graph.addNode("a");
graph.addNode("b");
graph.addEdge("a", "b");
graph.addEdge("a", "b");
expect(graph.get("a")).toEqual(["b"]);
});

test("should return topological sort correctly", () => {
const graph = new Graph<string>();
graph.addNode("a");
Expand Down
1 change: 1 addition & 0 deletions src/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class Graph<T extends NodeId> {
}

addEdge(node1: T, node2: T): void {
if (this._adjacencyList.get(node1)?.includes(node2)) return;
this._check(node1);
this._check(node2);
this._adjacencyList.get(node1)?.push(node2);
Expand Down

0 comments on commit 33c159f

Please sign in to comment.