From 8bd9a4df589711d9424b1cd6096a2e0677fb0233 Mon Sep 17 00:00:00 2001 From: Jonathan MASSUCHETTI Date: Wed, 23 Oct 2024 14:10:58 +0200 Subject: [PATCH] feat: add `props` to the `shouldFollow` opts, which contain the edge properties. (cherry picked from commit 49423ccda0a1fe2e2e7910d5561d617d7f06f122) --- src/Graph.spec-d.ts | 2 +- src/algorithms/depthFirstSearch/depthFirstSearch.spec.ts | 3 +-- src/algorithms/depthFirstSearch/depthFirstSearch.ts | 6 ++---- src/algorithms/depthFirstSearch/depthFirstVisit.ts | 8 +++++++- src/algorithms/depthFirstSearch/types.ts | 6 +++--- src/algorithms/shortestPath/shortestPaths.ts | 4 ++-- src/algorithms/topologicalSort/topologicalSort.spec.ts | 3 +-- src/algorithms/topologicalSort/topologicalSort.ts | 5 +++-- 8 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/Graph.spec-d.ts b/src/Graph.spec-d.ts index 5d39b68..76a9088 100644 --- a/src/Graph.spec-d.ts +++ b/src/Graph.spec-d.ts @@ -16,7 +16,7 @@ describe('graph types', () => { const g = new Graph(); 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', () => { diff --git a/src/algorithms/depthFirstSearch/depthFirstSearch.spec.ts b/src/algorithms/depthFirstSearch/depthFirstSearch.spec.ts index 8a734c5..755e1a4 100644 --- a/src/algorithms/depthFirstSearch/depthFirstSearch.spec.ts +++ b/src/algorithms/depthFirstSearch/depthFirstSearch.spec.ts @@ -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'); diff --git a/src/algorithms/depthFirstSearch/depthFirstSearch.ts b/src/algorithms/depthFirstSearch/depthFirstSearch.ts index 0c1ca1e..ce459d6 100644 --- a/src/algorithms/depthFirstSearch/depthFirstSearch.ts +++ b/src/algorithms/depthFirstSearch/depthFirstSearch.ts @@ -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 diff --git a/src/algorithms/depthFirstSearch/depthFirstVisit.ts b/src/algorithms/depthFirstSearch/depthFirstVisit.ts index e68b5f4..e34f789 100644 --- a/src/algorithms/depthFirstSearch/depthFirstVisit.ts +++ b/src/algorithms/depthFirstSearch/depthFirstVisit.ts @@ -23,7 +23,13 @@ export function depthFirstVisit( 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); diff --git a/src/algorithms/depthFirstSearch/types.ts b/src/algorithms/depthFirstSearch/types.ts index 675a155..72d0686 100644 --- a/src/algorithms/depthFirstSearch/types.ts +++ b/src/algorithms/depthFirstSearch/types.ts @@ -1,5 +1,4 @@ import type { Graph } from '../../Graph.js'; -import type { NoInfer } from '../../types.js'; export type DepthFirstSearchOptions = { /** @@ -28,8 +27,9 @@ export type DepthFirstSearchOptions = { * @returns boolean */ shouldFollow?: (args: { - source: NoInfer; - target: NoInfer; + source: Node; + target: Node; + props: LinkProps; graph: Graph; }) => boolean; }; diff --git a/src/algorithms/shortestPath/shortestPaths.ts b/src/algorithms/shortestPath/shortestPaths.ts index 697fef7..928b382 100644 --- a/src/algorithms/shortestPath/shortestPaths.ts +++ b/src/algorithms/shortestPath/shortestPaths.ts @@ -24,7 +24,7 @@ export function shortestPaths( u, v, weight: graph.getEdgeWeight(u, v), - props: graph.getEdgeProperties(u, v), + props: graph.getEdgeProperties(u, v)!, }); graph.removeEdge(u, v); } @@ -34,7 +34,7 @@ export function shortestPaths( u: v, v: u, weight: graph.getEdgeWeight(v, u), - props: graph.getEdgeProperties(u, v), + props: graph.getEdgeProperties(v, u)!, }); graph.removeEdge(v, u); } diff --git a/src/algorithms/topologicalSort/topologicalSort.spec.ts b/src/algorithms/topologicalSort/topologicalSort.spec.ts index e04408e..a975c74 100644 --- a/src/algorithms/topologicalSort/topologicalSort.spec.ts +++ b/src/algorithms/topologicalSort/topologicalSort.spec.ts @@ -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); diff --git a/src/algorithms/topologicalSort/topologicalSort.ts b/src/algorithms/topologicalSort/topologicalSort.ts index dca8674..e274eb6 100644 --- a/src/algorithms/topologicalSort/topologicalSort.ts +++ b/src/algorithms/topologicalSort/topologicalSort.ts @@ -29,8 +29,9 @@ export type TopologicalSortOptions = { * @returns boolean */ shouldFollow?: (args: { - source: NoInfer; - target: NoInfer; + source: Node; + target: Node; + props: LinkProps; graph: Graph; }) => boolean; };