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

Add ContentGraph::getContentKeyByNodeId #327

Merged
merged 1 commit into from
Feb 5, 2025
Merged
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
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));
});
});
});