topological-sort-group
Version:
Topological sorting and cycle detection. Optional grouping for parallel processing
89 lines • 3.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "default", {
enumerable: true,
get: function() {
return Graph;
}
});
var _deepGet = /*#__PURE__*/ _interop_require_default(require("./deepGet.js"));
function _class_call_check(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
function _type_of(obj) {
"@swc/helpers - typeof";
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
}
var isArray = Array.isArray || function(x) {
return Object.prototype.toString.call(x) === '[object Array]';
};
var Graph = /*#__PURE__*/ function() {
"use strict";
function Graph(options) {
_class_call_check(this, Graph);
this.path = options ? options.path || undefined : undefined;
this.nodeMap = {};
}
var _proto = Graph.prototype;
_proto.key = function key(keyOrValue) {
if (this.path) return (typeof keyOrValue === "undefined" ? "undefined" : _type_of(keyOrValue)) === 'object' ? (0, _deepGet.default)(keyOrValue, this.path) : keyOrValue;
return keyOrValue;
};
_proto.keys = function keys() {
var keys = [];
for(var key in this.nodeMap)keys.push(key);
return keys;
};
_proto.value = function value(key) {
return this.nodeMap[key].value;
};
_proto.edges = function edges(key) {
return this.nodeMap[key].edges;
};
_proto.add = function add(keyOrValue, toKeyOrValue) {
var key = this.key(keyOrValue);
var value = this.path ? (typeof keyOrValue === "undefined" ? "undefined" : _type_of(keyOrValue)) === 'object' ? keyOrValue : undefined : keyOrValue;
if (value !== undefined) {
if (this.nodeMap[key] === undefined) this.nodeMap[key] = {
value: value,
edges: []
};
else if (this.nodeMap[key].value !== value) throw new Error("Adding different node values to same graph. Key ".concat(key, ". Existing: ").concat(JSON.stringify(this.nodeMap[key].value), ". New: ").concat(JSON.stringify(value)));
}
// biome-ignore lint/style/noArguments: <explanation>
if (arguments.length === 1) return;
// add edge
this.add(toKeyOrValue);
var toKey = this.key(toKeyOrValue);
this.nodeMap[key].edges.push(toKey);
};
_proto.degrees = function degrees() {
var degrees = {};
for(var from in this.nodeMap){
if (degrees[from] === undefined) degrees[from] = 0;
this.nodeMap[from].edges.forEach(function(key) {
if (degrees[key] === undefined) degrees[key] = 0;
degrees[key]++;
});
}
return degrees;
};
Graph.from = function from(nodes, options) {
var graph = new Graph(options);
nodes.forEach(function(node) {
return isArray(node) ? graph.add.apply(graph, node) : graph.add(node);
});
return graph;
};
return Graph;
}();
/* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }