You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
996 B
JavaScript
41 lines
996 B
JavaScript
var through = require('through');
|
|
|
|
module.exports = function (opts) {
|
|
if (!opts) opts = {};
|
|
|
|
var rows = [];
|
|
return through(write, end);
|
|
|
|
function write (row) { rows.push(row) }
|
|
|
|
function end () {
|
|
var tr = this;
|
|
rows.sort(cmp);
|
|
|
|
if (opts.index) {
|
|
var index = {};
|
|
rows.forEach(function (row, ix) {
|
|
row.index = ix + 1;
|
|
index[row.id] = ix + 1;
|
|
});
|
|
rows.forEach(function (row) {
|
|
row.indexDeps = {};
|
|
Object.keys(row.deps).forEach(function (key) {
|
|
row.indexDeps[key] = index[row.deps[key]];
|
|
});
|
|
tr.queue(row);
|
|
});
|
|
}
|
|
else {
|
|
rows.forEach(function (row) {
|
|
tr.queue(row);
|
|
});
|
|
}
|
|
tr.queue(null);
|
|
}
|
|
|
|
function cmp (a, b) {
|
|
return a.id + a.hash < b.id + b.hash ? -1 : 1;
|
|
}
|
|
};
|