UNPKG

archunit

Version:

ArchUnit TypeScript is an architecture testing library, to specify and assert architecture rules in your TypeScript app

75 lines 2.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.projectCycles = exports.projectInternalCycles = void 0; const project_edges_1 = require("./project-edges"); const cycles_1 = require("./cycles/cycles"); const edge_projections_1 = require("./edge-projections"); function projectInternalCycles(graph) { const edges = (0, project_edges_1.projectEdges)(graph, (0, edge_projections_1.perInternalEdge)()); return new CycleProcessor().findCycles(edges); } exports.projectInternalCycles = projectInternalCycles; function projectCycles(graph) { return new CycleProcessor().findCycles(graph); } exports.projectCycles = projectCycles; class CycleProcessor { constructor() { this.labelToId = new Map(); this.idToLabel = new Map(); this.sourceEdges = []; } findCycles(edges) { const domainEdges = this.toDomain(edges); const cycles = (0, cycles_1.calculateCycles)(domainEdges); const result = this.fromDomain(cycles); this.clear(); return result; } clear() { this.labelToId = new Map(); this.idToLabel = new Map(); this.sourceEdges = []; } toDomain(filteredEdges) { this.sourceEdges = filteredEdges; this.labelToId = new Map(); this.idToLabel = new Map(); let index = 0; filteredEdges.forEach((e) => { if (!this.labelToId.has(e.sourceLabel)) { this.labelToId.set(e.sourceLabel, index); this.idToLabel.set(index++, e.sourceLabel); } if (!this.labelToId.has(e.targetLabel)) { this.labelToId.set(e.targetLabel, index); this.idToLabel.set(index++, e.targetLabel); } }); return filteredEdges.map((e) => { const fromId = this.labelToId.get(e.sourceLabel); const toId = this.labelToId.get(e.targetLabel); if (fromId === undefined || toId === undefined) { throw new Error('Label IDs should be defined at this point'); } return { from: fromId, to: toId, }; }); } fromDomain(cycles) { return cycles.map((c) => { return c.map((e) => { const sourceLabel = this.idToLabel.get(e.from); const targetLabel = this.idToLabel.get(e.to); const foundEdge = this.sourceEdges.find((e) => e.sourceLabel === sourceLabel && e.targetLabel === targetLabel); if (!foundEdge) { throw new Error('Edge should be found at this point'); } return foundEdge; }); }); } } //# sourceMappingURL=project-cycles.js.map