Skip to content

Commit

Permalink
returning array of arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Dec 11, 2012
1 parent 288acce commit 388f449
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
25 changes: 19 additions & 6 deletions req-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,53 @@ var path = require('path');
var Module = require('module');
var detective = require('detective');

var out = {};
var out = [];

// returns an array of immediate reqs, even for a single argument
function outbound(moduleName) {
// console.log('computing outbound links starting with', moduleName);
if (!Array.isArray(moduleName)) {
moduleName = [moduleName];
}

out = {};
visit(moduleName);
console.assert(Array.isArray(moduleName), moduleName, 'should be an array');
out = [];
moduleName.forEach(function(item) {
var reqs = visit(item);
console.assert(typeof reqs === 'object', 'return should be an object for', item);
out.push(Object.keys(reqs));
})
return out;
}

// only immediate requires are returned
function visit(request, parent) {
var reqs = {};

var fn;
try {
fn = require.resolve(request);
} catch (err) {
fn = Module._resolveFilename(request, parent);
}
if (!fs.existsSync(fn)) {
return;
return {};
}
// console.log(fn);
var src = fs.readFileSync(fn);
var requires = detective(src);

requires.forEach(function(item) {
out[item] = item;
reqs[item] = item;
/*
visit(item, {
id: request,
filename: fn
});
*/
})
});

return reqs;
};

module.exports = {
Expand Down
23 changes: 13 additions & 10 deletions test/basic/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var sure = require('../../node_modules/gt/sure.js');
var req = require('../../req-count');
var check = require('../../node_modules/check-types');

sure.init();

Expand All @@ -11,32 +12,34 @@ gt.test('basic', function () {

gt.test('foo outbound', function () {
var out = req.outbound('./foo');
gt.equal(typeof out, 'object', 'returns an object');
gt.equal(Object.keys(out).length, 1, 'there should be single outbound, not ' + JSON.stringify(out));
gt.ok(Array.isArray(out), 'returns an array');
gt.equal(out.length, 1, 'there should be single outbound, not ' + JSON.stringify(out));
});

gt.test('bar outbound', function () {
var out = req.outbound('./bar');
gt.equal(typeof out, 'object', 'returns an object');
gt.equal(Object.keys(out).length, 0, 'there should be nothing outbound, not ' + JSON.stringify(out));
gt.ok(Array.isArray(out), 'returns an array');
gt.equal(out.length, 1, 'there should be nothing outbound, not ' + JSON.stringify(out));
gt.equal(out[0].length, 0, "should be single empty array, not " + JSON.stringify(out));
});

gt.test('zoo outbound', function () {
var out = req.outbound('./zoo');
gt.equal(typeof out, 'object', 'returns an object');
gt.equal(Object.keys(out).length, 1, 'there should be single outbound, not ' + JSON.stringify(out));
gt.ok(Array.isArray(out), 'returns an array');
gt.equal(out.length, 1, 'there should be single outbound, not ' + JSON.stringify(out));
});

gt.test('two outbound', function () {
var out = req.outbound('./two');
gt.equal(typeof out, 'object', 'returns an object');
gt.equal(Object.keys(out).length, 2, 'there should be 2 outbound, not ' + JSON.stringify(out));
gt.ok(Array.isArray(out), 'returns an array');
gt.equal(out.length, 1, 'there should be 2 outbound, not ' + JSON.stringify(out));
gt.equal(out[0].length, 2, 'there should be 2 outbound, not ' + JSON.stringify(out));
});

gt.test('multiples do not count', function () {
var out = req.outbound('./multiples');
gt.equal(typeof out, 'object', 'returns an object');
gt.equal(Object.keys(out).length, 2, 'there should be 2 outbound, not ' + JSON.stringify(out));
gt.ok(Array.isArray(out), 'returns an array');
gt.equal(out.length, 1, 'there should be 1 outbound, not ' + JSON.stringify(out));
});

sure.run();

0 comments on commit 388f449

Please sign in to comment.