toposort.js
3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
!function() {
"use strict";
/**
* Topological sort class.
* Original by Marcel Klehr, contributed by Gustavo Henke.
*
* @class
* @since 0.1.0
* @see https://github.com/marcelklehr/toposort
* @author Marcel Klehr <mklehr@gmx.net>
*
* @see https://github.com/gustavohenke/toposort
* @author Gustavo Henke <gustavo@injoin.com.br>
*/
function Toposort() {
var self = this;
var edges = [];
/**
* Adds dependency edges.
*
* @since 0.1.0
* @param {String} item An dependent name. Must be an string and not empty
* @param {String[]|String} [deps] An dependency or array of dependencies
* @returns {Toposort} The Toposort instance
*/
self.add = function( item, deps ) {
if( typeof item !== "string" || !item ) {
throw new TypeError( "Dependent name must be given as a not empty string" );
}
deps = Array.isArray( deps ) ? deps.slice() : [deps];
if( deps.length ) {
deps.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] );
} );
} else {
edges.push( [item] );
}
return self;
};
/**
* Runs the toposorting and return an ordered array of strings
*
* @since 0.1.0
* @returns {String[]} The list of items topologically sorted.
*/
self.sort = function() {
var nodes = [];
var sorted = [];
edges.forEach( function( edge ) {
edge.forEach( function( n ) {
if( nodes.indexOf( n ) === -1 ) {
nodes.push( n );
}
} );
} );
function visit( node, predecessors, i ) {
var index, predsCopy;
predecessors = predecessors || [];
if( predecessors.indexOf( node ) > -1 ) {
throw new Error(
"Cyclic dependency found. '" + node + "' is dependent of itself.\n" +
"Dependency Chain: " + predecessors.join( " -> " ) + " => " + node
);
}
index = nodes.indexOf( node );
if( index === -1 ) {
return i;
}
nodes.splice( index, 1 );
if( predecessors.length === 0 ) {
i--;
}
predsCopy = predecessors.slice();
predsCopy.push( node );
edges.filter( function( e ) {
return e[0] === node;
} ).forEach( function( e ) {
i = visit( e[1], predsCopy, i );
} );
sorted.unshift( node );
return i;
}
for( var i = 0; i < nodes.length; i++ ) {
i = visit( nodes[i], null, i );
}
return sorted;
};
}
if( typeof module === "object" && module && typeof module.exports === "object" ) {
// Expose toposort to CommonJS loaders (aka Node)
module.exports = exports.Toposort = Toposort;
} else {
// Expose toposort to AMD loaders (aka Require.js)
if( typeof define === "function" && define.amd ) {
define( function() {
return Toposort;
} );
}
// Expose toposort as a browser global
if( typeof window === "object" ) {
window.Toposort = Toposort;
}
}
}();