Skip to content

Commit

Permalink
Merge pull request #95 from JesusTheHun/feat-shouldFollow-edge-props
Browse files Browse the repository at this point in the history
feat: add `props` to the `shouldFollow` opts, which contain the edge properties
  • Loading branch information
curran authored Oct 23, 2024
2 parents 3f8fb2c + 8bd9a4d commit ae0bea7
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Graph.spec-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('graph types', () => {
const g = new Graph<string, { type: 'foo' | 'bar' }>();
const props = g.getEdgeProperties('a', 'b');

expectTypeOf(props).toEqualTypeOf<{ type: 'foo' | 'bar' }>();
expectTypeOf(props).toEqualTypeOf<{ type: 'foo' | 'bar' } | undefined>();
});

it('should only accept nodes of the given type', () => {
Expand Down
3 changes: 1 addition & 2 deletions src/algorithms/depthFirstSearch/depthFirstSearch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ describe('depthFirstSearch', () => {
graph.addEdge('b', 'e', { props: { type: 'foo' } });

const nodes = depthFirstSearch(graph, {
shouldFollow: ({ source, target, graph }) =>
graph.getEdgeProperties(source, target).type === 'foo',
shouldFollow: ({ props }) => props.type === 'foo',
});

expect(nodes).toContain('a');
Expand Down
6 changes: 2 additions & 4 deletions src/algorithms/depthFirstSearch/depthFirstSearch.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { invariant } from '../../invariant.js';
import type { NoInfer } from '../../types.js';
import type { DepthFirstSearchOptions } from './types.js';

import { Graph } from '../../Graph.js';
import type { NoInfer } from '../../types.js';
import { depthFirstVisit } from './depthFirstVisit.js';
import type { DepthFirstSearchOptions } from './types.js';

/**
* Depth First Search algorithm, inspired by
Expand Down
8 changes: 7 additions & 1 deletion src/algorithms/depthFirstSearch/depthFirstVisit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ export function depthFirstVisit<Node, LinkProps>(

graph.adjacent(node)?.forEach((n) => {
const follow =
shouldFollow === undefined || shouldFollow({ source: node, target: n, graph });
shouldFollow === undefined ||
shouldFollow({
source: node,
target: n,
graph,
props: graph.getEdgeProperties(node, n)!,
});
if (!follow) return;

depthFirstVisit(graph, nodeList, visited, visiting, n, opts);
Expand Down
6 changes: 3 additions & 3 deletions src/algorithms/depthFirstSearch/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Graph } from '../../Graph.js';
import type { NoInfer } from '../../types.js';

export type DepthFirstSearchOptions<Node, LinkProps> = {
/**
Expand Down Expand Up @@ -28,8 +27,9 @@ export type DepthFirstSearchOptions<Node, LinkProps> = {
* @returns boolean
*/
shouldFollow?: (args: {
source: NoInfer<Node>;
target: NoInfer<Node>;
source: Node;
target: Node;
props: LinkProps;
graph: Graph<Node, LinkProps>;
}) => boolean;
};
4 changes: 2 additions & 2 deletions src/algorithms/shortestPath/shortestPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function shortestPaths<Node, LinkProps>(
u,
v,
weight: graph.getEdgeWeight(u, v),
props: graph.getEdgeProperties(u, v),
props: graph.getEdgeProperties(u, v)!,
});
graph.removeEdge(u, v);
}
Expand All @@ -34,7 +34,7 @@ export function shortestPaths<Node, LinkProps>(
u: v,
v: u,
weight: graph.getEdgeWeight(v, u),
props: graph.getEdgeProperties(u, v),
props: graph.getEdgeProperties(v, u)!,
});
graph.removeEdge(v, u);
}
Expand Down
3 changes: 1 addition & 2 deletions src/algorithms/topologicalSort/topologicalSort.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ describe('topologicalSort', () => {
const sorted = topologicalSort(graph, {
sourceNodes: ['a'],
includeSourceNodes: true,
shouldFollow: ({ source, target }) =>
graph.getEdgeProperties(source, target).type === 'foo',
shouldFollow: ({ props }) => props.type === 'foo',
});

expect(sorted.length).toEqual(3);
Expand Down
5 changes: 3 additions & 2 deletions src/algorithms/topologicalSort/topologicalSort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ export type TopologicalSortOptions<Node, LinkProps> = {
* @returns boolean
*/
shouldFollow?: (args: {
source: NoInfer<Node>;
target: NoInfer<Node>;
source: Node;
target: Node;
props: LinkProps;
graph: Graph<Node, LinkProps>;
}) => boolean;
};
Expand Down

0 comments on commit ae0bea7

Please sign in to comment.