Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change getLink/hasLink to also find link by id #49

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,32 @@ declare module "ngraph.graph" {
hasNode: (nodeId: NodeId) => Node<NodeData> | undefined

/**
* Returns a link between two nodes
* Returns a link between two nodes or by its id
*/
getLink: (fromNodeId: NodeId, toNodeId: NodeId) => Link<LinkData> | undefined
getLink: {
/**
* Gets an edge between two nodes
*/
(fromNodeId: NodeId, toNodeId: NodeId): Link<LinkData> | undefined;
/**
* Gets an edge by its id
*/
(linkId: LinkId): Link<LinkData> | undefined;
}

/**
* Checks if link is present in the graph
*/
hasLink: (fromNodeId: NodeId, toNodeId: NodeId) => Link<LinkData> | undefined
hasLink: {
/**
* Checks an edge between two nodes
*/
(fromNodeId: NodeId, toNodeId: NodeId): Link<LinkData> | undefined;
/**
* Checks an edge by its id
*/
(linkId: LinkId): Link<LinkData> | undefined;
}

/**
* Returns number of nodes in the graph
Expand Down
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,11 @@ function createGraph(options) {
/**
* Gets an edge between two nodes.
* Operation complexity is O(n) where n - number of links of a node.
*
* If only the first arg is provided, gets an edge by its id.
*
* @param {string} fromId link start identifier
* @param {string} toId link end identifier
* @param {string} fromId link start identifier or link id
* @param {string} [toId] link end identifier
*
* @returns link if there is one; undefined otherwise.
*/
Expand Down Expand Up @@ -444,6 +446,9 @@ function createGraph(options) {
}

function getLink(fromNodeId, toNodeId) {
if (fromNodeId !== undefined && toNodeId === undefined) { // if only 1st arg, then search by linkId
return links.get(fromNodeId);
}
if (fromNodeId === undefined || toNodeId === undefined) return undefined;
return links.get(makeLinkId(fromNodeId, toNodeId));
}
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions test/construction.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ test('hasLink is the same as getLink', function (t) {
t.end();
});

test('hasLink checks links by id', function (t) {
var graph = createGraph();
var linkId = graph.addLink(1, 2).id;
var link12 = graph.hasLink(linkId);
t.ok(link12.fromId === 1 && link12.toId === 2, 'link is found');
t.end();
});

test('it considers uniqueLinkId as multigraph', function (t) {
var options = {uniqueLinkId: true};
createGraph(options);
Expand Down