Skip to content

Commit 773fdbe

Browse files
authored
Merge pull request #53 from fecorreiabr/links
Add method to find link by its id
2 parents 0ff8af1 + 89db670 commit 773fdbe

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ declare module "ngraph.graph" {
120120
*/
121121
getLink: (fromNodeId: NodeId, toNodeId: NodeId) => Link<LinkData> | undefined
122122

123+
/**
124+
* Returns a link by its id
125+
*/
126+
getLinkById: (linkId: LinkId) => Link<LinkData> | undefined;
127+
123128
/**
124129
* Checks if link is present in the graph
125130
*/

index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,16 @@ function createGraph(options) {
246246
*
247247
* @returns link if there is one; undefined otherwise.
248248
*/
249-
getLink: getLink
249+
getLink: getLink,
250+
251+
/**
252+
* Gets a link by its id.
253+
*
254+
* @param {string} linkId link identifier
255+
*
256+
* @returns link if there is one; undefined otherwise.
257+
*/
258+
getLinkById: getLinkById
250259
};
251260

252261
// this will add `on()` and `fire()` methods.
@@ -445,6 +454,11 @@ function createGraph(options) {
445454
return links.get(makeLinkId(fromNodeId, toNodeId));
446455
}
447456

457+
function getLinkById(linkId) {
458+
if (linkId === undefined) return undefined;
459+
return links.get(linkId);
460+
}
461+
448462
function clear() {
449463
enterModification();
450464
forEachNode(function (node) {

test/construction.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ test('hasLink is the same as getLink', function () {
4545
expect(graph.getLink).toBe(graph.hasLink);
4646
});
4747

48+
test('getLinkById gets link by id', function () {
49+
var graph = createGraph();
50+
var link = graph.addLink(1, 2);
51+
var fetchedLink = graph.getLinkById(link.id);
52+
expect(fetchedLink).toBe(link);
53+
});
54+
4855
test('it considers uniqueLinkId as multigraph', function () {
4956
var options = {uniqueLinkId: true};
5057
createGraph(options);

0 commit comments

Comments
 (0)