diff --git a/lib/dep-graph.js b/lib/dep-graph.js index a93cf93..d3b2401 100644 --- a/lib/dep-graph.js +++ b/lib/dep-graph.js @@ -1,16 +1,16 @@ +// Generated by CoffeeScript 1.3.3 (function() { - var DepGraph, _; - var __indexOf = Array.prototype.indexOf || function(item) { - for (var i = 0, l = this.length; i < l; i++) { - if (this[i] === item) return i; - } - return -1; - }, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; + var DepGraph, _, + __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; + _ = require('underscore'); + DepGraph = (function() { + function DepGraph() { this.map = {}; } + DepGraph.prototype.add = function(id, depId) { var _base, _ref; if ((_ref = (_base = this.map)[id]) == null) { @@ -22,24 +22,28 @@ this.map[id].push(depId); return this.map[id]; }; + DepGraph.prototype.getChain = function(id) { - var chain, deps, leafNode, visit, visited, _i, _len, _ref; + var chain, deps, leafNode, visit, visited, _i, _len, _ref, + _this = this; deps = this.descendantsOf(id); chain = []; visited = {}; - visit = __bind(function(node) { + visit = function(node) { var parent, _i, _len, _ref; if (visited[node] || node === id) { return; } visited[node] = true; - _ref = this.parentsOf(node); + _ref = _this.parentsOf(node); for (_i = 0, _len = _ref.length; _i < _len; _i++) { parent = _ref[_i]; - visit(parent); + if (__indexOf.call(deps, parent) >= 0) { + visit(parent); + } } return chain.unshift(node); - }, this); + }; _ref = _.intersection(deps, this.leafNodes()).reverse(); for (_i = 0, _len = _ref.length; _i < _len; _i++) { leafNode = _ref[_i]; @@ -47,6 +51,7 @@ } return chain; }; + DepGraph.prototype.leafNodes = function() { var allNodes, node, _i, _len, _ref, _results; allNodes = _.uniq(_.flatten(_.values(this.map))); @@ -59,6 +64,7 @@ } return _results; }; + DepGraph.prototype.parentsOf = function(child) { var node, _i, _len, _ref, _results; _ref = _.keys(this.map); @@ -71,8 +77,9 @@ } return _results; }; + DepGraph.prototype.descendantsOf = function(parent, descendants, branch) { - var child, _i, _len, _ref, _ref2; + var child, _i, _len, _ref, _ref1; if (descendants == null) { descendants = []; } @@ -81,9 +88,9 @@ } descendants.push(parent); branch.push(parent); - _ref2 = (_ref = this.map[parent]) != null ? _ref : []; - for (_i = 0, _len = _ref2.length; _i < _len; _i++) { - child = _ref2[_i]; + _ref1 = (_ref = this.map[parent]) != null ? _ref : []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + child = _ref1[_i]; if (__indexOf.call(branch, child) >= 0) { throw new Error("Cyclic dependency from " + parent + " to " + child); } @@ -94,11 +101,15 @@ } return descendants.slice(1); }; + return DepGraph; + })(); + if ((typeof module !== "undefined" && module !== null ? module.exports : void 0) != null) { module.exports = DepGraph; } else { this.DepGraph = DepGraph; } + }).call(this); diff --git a/src/dep-graph.coffee b/src/dep-graph.coffee index 2363396..fcff3a1 100644 --- a/src/dep-graph.coffee +++ b/src/dep-graph.coffee @@ -27,8 +27,7 @@ class DepGraph visit = (node) => return if visited[node] or node is id visited[node] = true - for parent in @parentsOf(node) - visit parent + visit parent for parent in @parentsOf(node) when parent in deps chain.unshift node for leafNode in _.intersection(deps, @leafNodes()).reverse() @@ -57,4 +56,4 @@ class DepGraph if module?.exports? module.exports = DepGraph else - @DepGraph = DepGraph \ No newline at end of file + @DepGraph = DepGraph