Skip to content

Commit

Permalink
refactor: type Edge
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielrufino committed Dec 31, 2024
1 parent a322b85 commit da06431
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Graph/Edge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Edge = [string, string]
9 changes: 5 additions & 4 deletions src/Graph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import IGraphOptions from './IGraphOptions';
import Dictionary from '../Dictionary';
import Queue from '../Queue';
import Set from '../Set';
import { Edge } from './Edge';

export default class Graph implements IGraph {
private readonly _isDirected: boolean;
Expand Down Expand Up @@ -47,16 +48,16 @@ export default class Graph implements IGraph {
return [...this._data.keys];
}

get edges(): [string, string][] {
get edges(): Edge[] {
if (this.isDirected) {
return Object.entries(this.data)
.flatMap((
[node, links]) => links.map<[string, string]>(link => [node, link]),
[node, links]) => links.map<Edge>(link => [node, link]),
);
}

return Object.entries(this.data)
.reduce<[string, string][]>((accumulator, [node, links]) => {
.reduce<Edge[]>((accumulator, [node, links]) => {
const edges = links
.map<[string, string]>(link => [node, link])
.filter(edge => !accumulator.find(item => item.includes(edge[0]) && item.includes(edge[1])));
Expand All @@ -78,7 +79,7 @@ export default class Graph implements IGraph {
return node;
}

public connect(node1: string, node2: string): [string, string] {
public connect(node1: string, node2: string): Edge {
if (!this._data.hasKey(node1)) {
throw new GraphNodeNotFoundError(node1);
}
Expand Down

0 comments on commit da06431

Please sign in to comment.