UNPKG

@kaylum.io/json-graph-ts

Version:

Json Graph implementation in TypeScript

68 lines 2.63 kB
import { randomUUID } from "crypto"; class JsonGraph { store; _nodeLabelIndex = []; constructor() { this.store = { graph: { nodes: [], edges: [] } }; } addNode(label, metadata) { if (this._nodeLabelIndex.includes(label)) return this.findNodesByLabel(label)[0]; const node = { id: randomUUID(), label, metadata }; this.store.graph?.nodes?.push(node); this._nodeLabelIndex.push(label); return node; } addEdge(sourceNodeId, targetNodeId, relation) { const matches = this.findEdgeBySourceAndTarget(sourceNodeId, targetNodeId, relation); if (matches.length > 0) return matches[0]; const edge = { id: randomUUID(), source: sourceNodeId, target: targetNodeId }; if (relation) edge.relation = relation; this.store.graph?.edges?.push(edge); return edge; } findEdgeBySourceAndTarget(source, target, relation) { return this.store.graph?.edges?.filter(edge => edge.source === source && edge.target === target && (relation ? edge.relation === relation : true)) || []; } findNodesByLabel(label) { return this.store.graph?.nodes?.filter(node => node.label === label) || []; } findNodesByEdgeSource(source) { const targets = this.store.graph?.edges?.filter(edge => edge.source === source) .map(edge => edge.target); return this.store.graph?.nodes?.filter(node => targets?.includes(node.id)) || []; } findNodesByEdgeTarget(target) { const sources = this.store.graph?.edges?.filter(edge => edge.target === target) .map(edge => edge.source); return this.store.graph?.nodes?.filter(node => sources?.includes(node.id)) || []; } findNodesByRelation(relation) { const related = this.store.graph?.edges?.filter(edge => edge.relation === relation); const sources = related?.map(edge => this.store.graph?.nodes?.filter((node) => edge.source === node.id)[0]); const targets = related?.map(edge => this.store.graph?.nodes?.filter((node) => edge.target === node.id)[0]); // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore return sources?.map((source, i) => [source, relation, targets[i]]); } toString(spacer = null, indent = 2) { return JSON.stringify(this.store, spacer, indent); } } export { JsonGraph }; //# sourceMappingURL=jsongraph.js.map