toposort-class
Advanced tools
Comparing version 0.1.2 to 0.1.3
63
index.js
@@ -26,19 +26,18 @@ /** | ||
*/ | ||
this.add = function(item, deps) { | ||
if (typeof item !== "string" || !item) { | ||
this.add = function( item, deps ) { | ||
if ( typeof item !== "string" || !item ) { | ||
throw new TypeError("Dependent name must be given as a not empty string"); | ||
} | ||
if (deps) { | ||
var tmp = Array.isArray(deps) ? deps : [deps]; | ||
tmp.forEach(function(dep) { | ||
if (typeof dep !== "string" || !dep) { | ||
if ( deps ) { | ||
var tmp = Array.isArray( deps ) ? deps.slice() : [ deps ]; | ||
tmp.forEach(function( dep ) { | ||
if ( typeof dep !== "string" || !dep ) { | ||
throw new TypeError("Dependency name must be given as a not empty string"); | ||
} | ||
edges.push( [ item, dep ] ); | ||
}); | ||
tmp.unshift(item); | ||
edges.push(tmp); | ||
} else { | ||
edges.push([item]); | ||
edges.push( [ item ] ); | ||
} | ||
@@ -59,6 +58,6 @@ | ||
edges.forEach(function(edge) { | ||
edge.forEach(function(n) { | ||
if (nodes.indexOf(n) === -1) { | ||
nodes.push(n); | ||
edges.forEach(function( edge ) { | ||
edge.forEach(function( n ) { | ||
if ( nodes.indexOf( n ) === -1 ) { | ||
nodes.push( n ); | ||
} | ||
@@ -68,34 +67,34 @@ }); | ||
function visit(node, predecessors, i) { | ||
if (!predecessors) { | ||
predecessors = []; | ||
} else if (predecessors.indexOf(node) > 0) { | ||
throw new Error(require('util').format("Cyclic dependency found. '%s' is dependent of itself.", node)); | ||
function visit( node, predecessors, i ) { | ||
predecessors = predecessors || []; | ||
if ( predecessors.indexOf( node ) > -1 ) { | ||
throw new Error( require("util").format( "Cyclic dependency found. '%s' is dependent of itself.", node ) ); | ||
} | ||
var index = nodes.indexOf(node); | ||
if (index === -1) { | ||
var index = nodes.indexOf( node ); | ||
if ( index === -1 ) { | ||
return i; | ||
} | ||
nodes.splice(index, 1); | ||
if (predecessors.length === 0) { | ||
nodes.splice( index, 1 ); | ||
if ( predecessors.length === 0 ) { | ||
i--; | ||
} | ||
var predsCopy = predecessors.slice(0); | ||
predsCopy.push(node); | ||
var predsCopy = predecessors.slice(); | ||
predsCopy.push( node ); | ||
edges.filter(function(e) { | ||
return e[0] === node; | ||
}).forEach(function(e) { | ||
i = visit(e[1], predsCopy, i); | ||
edges.filter(function( e ) { | ||
return e[ 0 ] === node; | ||
}).forEach(function( e ) { | ||
i = visit( e[ 1 ], predsCopy, i ); | ||
}); | ||
sorted.unshift(node); | ||
sorted.unshift( node ); | ||
return i; | ||
} | ||
for (var i = 0; i < nodes.length; i++) { | ||
i = visit(nodes[i], null, i); | ||
for ( var i = 0; i < nodes.length; i++ ) { | ||
i = visit( nodes[ i ], null, i ); | ||
} | ||
@@ -102,0 +101,0 @@ |
{ | ||
"name": "toposort-class", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Topological sort of directed acyclic graphs (like dependecy lists)", | ||
"main": "index.js", | ||
"devDependencies": { | ||
"vows": "*" | ||
}, | ||
"scripts": { | ||
"test": "vows --spec test.js" | ||
}, | ||
"repository": { | ||
@@ -7,0 +13,0 @@ "type": "git", |
@@ -23,3 +23,3 @@ # Toposort | ||
```javascript | ||
var Toposort = require('./index'), | ||
var Toposort = require('toposort-class'), | ||
t = new Toposort(); | ||
@@ -26,0 +26,0 @@ |
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
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
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
6229
6
125
1
1