Comparing version 0.2.5 to 0.2.7
58
index.js
@@ -45,30 +45,32 @@ /*! | ||
for (var i=0; i < nodes.length; i++) { | ||
(function visit(node, predecessors) { | ||
if (!predecessors) predecessors = [] | ||
else // The node is a dependency of itself?! I'd say, we're free to throw | ||
if(predecessors.indexOf(node) > 0) | ||
throw new Error('Cyclic dependency! The following node is a dependency of itself: '+JSON.stringify(node)) | ||
// if it's not in nodes[] anymore, we've already had this node | ||
if (nodes.indexOf(node) < 0) return; | ||
// remove this node from nodes[] | ||
nodes.splice(nodes.indexOf(node), 1) | ||
if (predecessors.length == 0) i--; | ||
var predsCopy = predecessors.map(function(n) {return n}) | ||
predsCopy.push(node) | ||
edges | ||
.filter(function(e) { return e[0] === node }) | ||
.forEach(function(e) { | ||
// visit all dependencies of this node | ||
// and provide them with a *copy* of their predecessors (including *this* node) | ||
visit(e[1], predsCopy) | ||
}) | ||
sorted.unshift(node) | ||
})(nodes[i]) | ||
} | ||
while (nodes.length > 0) | ||
visit(nodes[0], null, edges, nodes, sorted) | ||
return sorted; | ||
} | ||
} | ||
function visit(node, predecessors, edges, nodes, sorted) { | ||
if (!predecessors) predecessors = [] | ||
else // The node is a dependency of itself?! I'd say, we're free to throw | ||
if(predecessors.indexOf(node) > 0) | ||
throw new Error('Cyclic dependency! The following node is a dependency of itself: '+JSON.stringify(node)) | ||
// if it's not in nodes[] anymore, we've already had this node | ||
if (nodes.indexOf(node) < 0) return; | ||
// remove this node from nodes[] | ||
nodes.splice(nodes.indexOf(node), 1) | ||
var predsCopy = predecessors.map(function(n) {return n}) | ||
predsCopy.push(node) | ||
edges | ||
.filter(function(e) { return e[0] === node }) | ||
.forEach(function(e) { | ||
// visit all dependencies of this node | ||
// and provide them with a *copy* of their predecessors (including *this* node) | ||
visit(e[1], predsCopy, edges, nodes, sorted) | ||
}) | ||
sorted.unshift(node) | ||
} |
{ | ||
"name": "toposort", | ||
"version": "0.2.5", | ||
"version": "0.2.7", | ||
"description": "Topological sort of directed ascyclic graphs (like dependecy lists)", | ||
@@ -11,3 +11,3 @@ "main": "index.js", | ||
"type": "git", | ||
"url": "https://github.com/marcelklehr/node-toposort.git" | ||
"url": "https://github.com/marcelklehr/toposort.git" | ||
}, | ||
@@ -14,0 +14,0 @@ "devDependencies": { |
# Sorting directed acyclic graphs | ||
[![Build Status](https://travis-ci.org/marcelklehr/node-toposort.png)](https://travis-ci.org/marcelklehr/node-toposort) | ||
[![Build Status](https://travis-ci.org/marcelklehr/toposort.png)](https://travis-ci.org/marcelklehr/toposort) | ||
## Installation | ||
`npm install toposort` | ||
`npm install toposort` or `component install toposort` | ||
@@ -7,0 +7,0 @@ ## Example |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7519
8
157