Skip to content

Commit 8bd9a4d

Browse files
committed
feat: add props to the shouldFollow opts, which contain the edge properties.
(cherry picked from commit 49423cc)
1 parent d9785de commit 8bd9a4d

File tree

8 files changed

+20
-17
lines changed

8 files changed

+20
-17
lines changed

src/Graph.spec-d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('graph types', () => {
1616
const g = new Graph<string, { type: 'foo' | 'bar' }>();
1717
const props = g.getEdgeProperties('a', 'b');
1818

19-
expectTypeOf(props).toEqualTypeOf<{ type: 'foo' | 'bar' }>();
19+
expectTypeOf(props).toEqualTypeOf<{ type: 'foo' | 'bar' } | undefined>();
2020
});
2121

2222
it('should only accept nodes of the given type', () => {

src/algorithms/depthFirstSearch/depthFirstSearch.spec.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ describe('depthFirstSearch', () => {
1212
graph.addEdge('b', 'e', { props: { type: 'foo' } });
1313

1414
const nodes = depthFirstSearch(graph, {
15-
shouldFollow: ({ source, target, graph }) =>
16-
graph.getEdgeProperties(source, target).type === 'foo',
15+
shouldFollow: ({ props }) => props.type === 'foo',
1716
});
1817

1918
expect(nodes).toContain('a');

src/algorithms/depthFirstSearch/depthFirstSearch.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { invariant } from '../../invariant.js';
2-
import type { NoInfer } from '../../types.js';
3-
import type { DepthFirstSearchOptions } from './types.js';
4-
51
import { Graph } from '../../Graph.js';
2+
import type { NoInfer } from '../../types.js';
63
import { depthFirstVisit } from './depthFirstVisit.js';
4+
import type { DepthFirstSearchOptions } from './types.js';
75

86
/**
97
* Depth First Search algorithm, inspired by

src/algorithms/depthFirstSearch/depthFirstVisit.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ export function depthFirstVisit<Node, LinkProps>(
2323

2424
graph.adjacent(node)?.forEach((n) => {
2525
const follow =
26-
shouldFollow === undefined || shouldFollow({ source: node, target: n, graph });
26+
shouldFollow === undefined ||
27+
shouldFollow({
28+
source: node,
29+
target: n,
30+
graph,
31+
props: graph.getEdgeProperties(node, n)!,
32+
});
2733
if (!follow) return;
2834

2935
depthFirstVisit(graph, nodeList, visited, visiting, n, opts);

src/algorithms/depthFirstSearch/types.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { Graph } from '../../Graph.js';
2-
import type { NoInfer } from '../../types.js';
32

43
export type DepthFirstSearchOptions<Node, LinkProps> = {
54
/**
@@ -28,8 +27,9 @@ export type DepthFirstSearchOptions<Node, LinkProps> = {
2827
* @returns boolean
2928
*/
3029
shouldFollow?: (args: {
31-
source: NoInfer<Node>;
32-
target: NoInfer<Node>;
30+
source: Node;
31+
target: Node;
32+
props: LinkProps;
3333
graph: Graph<Node, LinkProps>;
3434
}) => boolean;
3535
};

src/algorithms/shortestPath/shortestPaths.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function shortestPaths<Node, LinkProps>(
2424
u,
2525
v,
2626
weight: graph.getEdgeWeight(u, v),
27-
props: graph.getEdgeProperties(u, v),
27+
props: graph.getEdgeProperties(u, v)!,
2828
});
2929
graph.removeEdge(u, v);
3030
}
@@ -34,7 +34,7 @@ export function shortestPaths<Node, LinkProps>(
3434
u: v,
3535
v: u,
3636
weight: graph.getEdgeWeight(v, u),
37-
props: graph.getEdgeProperties(u, v),
37+
props: graph.getEdgeProperties(v, u)!,
3838
});
3939
graph.removeEdge(v, u);
4040
}

src/algorithms/topologicalSort/topologicalSort.spec.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ describe('topologicalSort', () => {
8989
const sorted = topologicalSort(graph, {
9090
sourceNodes: ['a'],
9191
includeSourceNodes: true,
92-
shouldFollow: ({ source, target }) =>
93-
graph.getEdgeProperties(source, target).type === 'foo',
92+
shouldFollow: ({ props }) => props.type === 'foo',
9493
});
9594

9695
expect(sorted.length).toEqual(3);

src/algorithms/topologicalSort/topologicalSort.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ export type TopologicalSortOptions<Node, LinkProps> = {
2929
* @returns boolean
3030
*/
3131
shouldFollow?: (args: {
32-
source: NoInfer<Node>;
33-
target: NoInfer<Node>;
32+
source: Node;
33+
target: Node;
34+
props: LinkProps;
3435
graph: Graph<Node, LinkProps>;
3536
}) => boolean;
3637
};

0 commit comments

Comments
 (0)