Skip to content

Commit

Permalink
Support multiple edge types in Graph.hasEdge (#8550)
Browse files Browse the repository at this point in the history
* Support multiple edge types in Graph.hasEdge

* Update comment

* Pass known edge types into Graphs

* Remove unused type

* Remove unused type

* Fix lint

* Rollback ALL_EDGE_TYPE support

* Rollback experimental bundler

* Fix Graph hasEdge type def

* Add test back in

* Add test back in

* Rolback COntentGraph
  • Loading branch information
mattcompiles committed Oct 19, 2022
1 parent c1942c9 commit e168a78
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
14 changes: 11 additions & 3 deletions packages/core/graph/src/AdjacencyList.js
Expand Up @@ -329,10 +329,18 @@ export default class AdjacencyList<TEdgeType: number = 1> {
hasEdge(
from: NodeId,
to: NodeId,
type: TEdgeType | NullEdgeType = 1,
type: TEdgeType | NullEdgeType | Array<TEdgeType | NullEdgeType> = 1,
): boolean {
let hash = this.#edges.hash(from, to, type);
return this.#edges.addressOf(hash, from, to, type) !== null;
let hasEdge = (type: TEdgeType | NullEdgeType) => {
let hash = this.#edges.hash(from, to, type);
return this.#edges.addressOf(hash, from, to, type) !== null;
};

if (Array.isArray(type)) {
return type.some(hasEdge);
}

return hasEdge(type);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/core/graph/src/Graph.js
Expand Up @@ -104,7 +104,7 @@ export default class Graph<TNode, TEdgeType: number = 1> {
hasEdge(
from: NodeId,
to: NodeId,
type?: TEdgeType | NullEdgeType = 1,
type?: TEdgeType | NullEdgeType | Array<TEdgeType | NullEdgeType> = 1,
): boolean {
return this.adjacencyList.hasEdge(from, to, type);
}
Expand Down
15 changes: 15 additions & 0 deletions packages/core/graph/test/AdjacencyList.test.js
Expand Up @@ -243,6 +243,21 @@ describe('AdjacencyList', () => {
AdjacencyList.prototype.hash = originalHash;
});

it('hasEdge should accept an array of edge types', () => {
let graph = new AdjacencyList();
let a = graph.addNode();
let b = graph.addNode();
let c = graph.addNode();

graph.addEdge(a, b, 1);
graph.addEdge(b, c, 2);

assert.ok(!graph.hasEdge(a, b, [2, 3]));
assert.ok(graph.hasEdge(a, b, [1, 2]));
assert.ok(!graph.hasEdge(b, c, [1, 3]));
assert.ok(graph.hasEdge(b, c, [2, 3]));
});

describe('deserialize', function () {
this.timeout(10000);

Expand Down

0 comments on commit e168a78

Please sign in to comment.