UNPKG

@kaylum.io/json-graph-ts

Version:

Json Graph implementation in TypeScript

72 lines 4.07 kB
import { randomUUID } from "crypto"; class JsonGraph { constructor() { this._nodeLabelIndex = []; this.store = { graph: { nodes: [], edges: [] } }; } addNode(label, metadata) { var _a, _b; if (this._nodeLabelIndex.includes(label)) return this.findNodesByLabel(label)[0]; const node = { id: randomUUID(), label, metadata }; (_b = (_a = this.store.graph) === null || _a === void 0 ? void 0 : _a.nodes) === null || _b === void 0 ? void 0 : _b.push(node); this._nodeLabelIndex.push(label); return node; } addEdge(sourceNodeId, targetNodeId, relation) { var _a, _b; 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; (_b = (_a = this.store.graph) === null || _a === void 0 ? void 0 : _a.edges) === null || _b === void 0 ? void 0 : _b.push(edge); return edge; } findEdgeBySourceAndTarget(source, target, relation) { var _a, _b; return ((_b = (_a = this.store.graph) === null || _a === void 0 ? void 0 : _a.edges) === null || _b === void 0 ? void 0 : _b.filter(edge => edge.source === source && edge.target === target && (relation ? edge.relation === relation : true))) || []; } findNodesByLabel(label) { var _a, _b; return ((_b = (_a = this.store.graph) === null || _a === void 0 ? void 0 : _a.nodes) === null || _b === void 0 ? void 0 : _b.filter(node => node.label === label)) || []; } findNodesByEdgeSource(source) { var _a, _b, _c, _d; const targets = (_b = (_a = this.store.graph) === null || _a === void 0 ? void 0 : _a.edges) === null || _b === void 0 ? void 0 : _b.filter(edge => edge.source === source).map(edge => edge.target); return ((_d = (_c = this.store.graph) === null || _c === void 0 ? void 0 : _c.nodes) === null || _d === void 0 ? void 0 : _d.filter(node => targets === null || targets === void 0 ? void 0 : targets.includes(node.id))) || []; } findNodesByEdgeTarget(target) { var _a, _b, _c, _d; const sources = (_b = (_a = this.store.graph) === null || _a === void 0 ? void 0 : _a.edges) === null || _b === void 0 ? void 0 : _b.filter(edge => edge.target === target).map(edge => edge.source); return ((_d = (_c = this.store.graph) === null || _c === void 0 ? void 0 : _c.nodes) === null || _d === void 0 ? void 0 : _d.filter(node => sources === null || sources === void 0 ? void 0 : sources.includes(node.id))) || []; } findNodesByRelation(relation) { var _a, _b; const related = (_b = (_a = this.store.graph) === null || _a === void 0 ? void 0 : _a.edges) === null || _b === void 0 ? void 0 : _b.filter(edge => edge.relation === relation); const sources = related === null || related === void 0 ? void 0 : related.map(edge => { var _a, _b; return (_b = (_a = this.store.graph) === null || _a === void 0 ? void 0 : _a.nodes) === null || _b === void 0 ? void 0 : _b.filter((node) => edge.source === node.id)[0]; }); const targets = related === null || related === void 0 ? void 0 : related.map(edge => { var _a, _b; return (_b = (_a = this.store.graph) === null || _a === void 0 ? void 0 : _a.nodes) === null || _b === void 0 ? void 0 : _b.filter((node) => edge.target === node.id)[0]; }); // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore return sources === null || sources === void 0 ? void 0 : 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