Skip to content

Commit

Permalink
Add ContentGraph::getContentKeyByNodeId
Browse files Browse the repository at this point in the history
Adds a method to retrieve content keys associated with node IDs.

Test Plan: Run unit-tests (yarn test)

Reviewers: MonicaOlejniczak

Reviewed By: MonicaOlejniczak

Pull Request: #327
  • Loading branch information
yamadapc authored Feb 5, 2025
1 parent cc4ac39 commit a7506e7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/core/graph/src/ContentGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ export default class ContentGraph<TNode, TEdgeType: number = 1> extends Graph<
}
}

getContentKeyByNodeId(nodeId: NodeId): ContentKey {
return nullthrows(
this._nodeIdToContentKey.get(nodeId),
`Expected node id ${nodeId} to exist`,
);
}

getNodeIdByContentKey(contentKey: ContentKey): NodeId {
return nullthrows(
this._contentKeyToNodeId.get(contentKey),
Expand Down
18 changes: 18 additions & 0 deletions packages/core/graph/test/ContentGraph.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,22 @@ describe('ContentGraph', () => {

assert(!graph.hasContentKey('contentKey'));
});

describe('getContentKeyByNodeId', () => {
it('returns the content key for a node', () => {
const graph = new ContentGraph();

const node1 = graph.addNodeByContentKey('node1', {});
assert.equal(graph.getContentKeyByNodeId(node1), 'node1');
const node2 = graph.addNodeByContentKey('node2', {});
assert.equal(graph.getContentKeyByNodeId(node2), 'node2');
});

it('throws if the node does not have a content key', () => {
const graph = new ContentGraph();

const node1 = graph.addNode({});
assert.throws(() => graph.getContentKeyByNodeId(node1));
});
});
});

0 comments on commit a7506e7

Please sign in to comment.