UNPKG

@kaylum.io/json-graph-ts

Version:

Json Graph implementation in TypeScript

79 lines 4.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonGraph = void 0; var crypto_1 = require("crypto"); var JsonGraph = /** @class */ (function () { function JsonGraph() { this._nodeLabelIndex = []; this.store = { graph: { nodes: [], edges: [] } }; } JsonGraph.prototype.addNode = function (label, metadata) { var _a, _b; if (this._nodeLabelIndex.includes(label)) return this.findNodesByLabel(label)[0]; var node = { id: (0, crypto_1.randomUUID)(), label: label, metadata: 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; }; JsonGraph.prototype.addEdge = function (sourceNodeId, targetNodeId, relation) { var _a, _b; var matches = this.findEdgeBySourceAndTarget(sourceNodeId, targetNodeId, relation); if (matches.length > 0) return matches[0]; var edge = { id: (0, crypto_1.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; }; JsonGraph.prototype.findEdgeBySourceAndTarget = function (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(function (edge) { return edge.source === source && edge.target === target && (relation ? edge.relation === relation : true); })) || []; }; JsonGraph.prototype.findNodesByLabel = function (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(function (node) { return node.label === label; })) || []; }; JsonGraph.prototype.findNodesByEdgeSource = function (source) { var _a, _b, _c, _d; var targets = (_b = (_a = this.store.graph) === null || _a === void 0 ? void 0 : _a.edges) === null || _b === void 0 ? void 0 : _b.filter(function (edge) { return edge.source === source; }).map(function (edge) { return edge.target; }); return ((_d = (_c = this.store.graph) === null || _c === void 0 ? void 0 : _c.nodes) === null || _d === void 0 ? void 0 : _d.filter(function (node) { return targets === null || targets === void 0 ? void 0 : targets.includes(node.id); })) || []; }; JsonGraph.prototype.findNodesByEdgeTarget = function (target) { var _a, _b, _c, _d; var sources = (_b = (_a = this.store.graph) === null || _a === void 0 ? void 0 : _a.edges) === null || _b === void 0 ? void 0 : _b.filter(function (edge) { return edge.target === target; }).map(function (edge) { return edge.source; }); return ((_d = (_c = this.store.graph) === null || _c === void 0 ? void 0 : _c.nodes) === null || _d === void 0 ? void 0 : _d.filter(function (node) { return sources === null || sources === void 0 ? void 0 : sources.includes(node.id); })) || []; }; JsonGraph.prototype.findNodesByRelation = function (relation) { var _this = this; var _a, _b; var related = (_b = (_a = this.store.graph) === null || _a === void 0 ? void 0 : _a.edges) === null || _b === void 0 ? void 0 : _b.filter(function (edge) { return edge.relation === relation; }); var sources = related === null || related === void 0 ? void 0 : related.map(function (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(function (node) { return edge.source === node.id; })[0]; }); var targets = related === null || related === void 0 ? void 0 : related.map(function (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(function (node) { return 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(function (source, i) { return [source, relation, targets[i]]; }); }; JsonGraph.prototype.toString = function (spacer, indent) { if (spacer === void 0) { spacer = null; } if (indent === void 0) { indent = 2; } return JSON.stringify(this.store, spacer, indent); }; return JsonGraph; }()); exports.JsonGraph = JsonGraph; //# sourceMappingURL=jsongraph.js.map