Skip to content

Commit

Permalink
add test and proper check for ignoring non javascript files
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenhandley committed Sep 5, 2012
1 parent 0e1be56 commit 6ab36b4
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 50 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Write minimal node index.js files that require and export siblings by file basen

# Latest Version

0.1.4
0.1.8

# Installation
```
Expand All @@ -17,7 +17,7 @@ or in package.json
{
...
"dependencies": {
"requireindex": "~0.1.4"
"requireindex": "~0.1.8"
}
}
```
Expand Down
87 changes: 46 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,52 @@
(function() {
var fs = require('fs'),
path = require('path');
var fs = require('fs');
var path = require('path');

module.exports = function(dir, basenames) {
var requires = {};
module.exports = function (dir, basenames) {
var requires = {};

if (arguments.length === 1) {
var files = fs.readdirSync(dir);

// sort files in lowercase alpha for linux
files.sort(function(a,b) {
a = a.toLowerCase();
b = b.toLowerCase();

if (a < b) {
return -1;
} else if (b < a) {
return 1;
} else {
return 0;
}
});

files.forEach(function(filename) {

if ((filename === 'index.js') || (filename[0] === '_')) { return; }

var ext = path.extname(filename);
if (!(ext in require.extensions)) { return; }
if (arguments.length === 1) {
// if basenames arguments isn't passed, require all javascript
// files (except for those prefixed with _) and all directories

filename = path.basename(filename, ext);
var filepath = path.join(dir, filename);
var files = fs.readdirSync(dir);

// sort files in lowercase alpha for linux
files.sort(function (a,b) {
a = a.toLowerCase();
b = b.toLowerCase();

if (a < b) {
return -1;
} else if (b < a) {
return 1;
} else {
return 0;
}
});

requires[filename] = require(path.resolve(filepath));
});
files.forEach(function (filename) {
// ignore index.js and files prefixed with underscore
if ((filename === 'index.js') || (filename[0] === '_')) { return; }

var filepath = path.resolve(path.join(dir, filename));
var ext = path.extname(filename);
var stats = fs.statSync(filepath);

// don't require non-javascript files (.txt .md etc.)
if (stats.isFile() && !(ext in require.extensions)) { return; }

var basename = path.basename(filename, ext);

requires[basename] = require(filepath);
});

} else {
basenames.forEach(function(basename) {
var filepath = path.join(dir, basename);
requires[filename] = require(path.resolve(filepath));
});
}
} else {
// if basenames argument is passed, explicitly include those files
basenames.forEach(function (basename) {
var filepath = path.resolve(path.join(dir, basename));
requires[basename] = require(filepath);
});
}

return requires;
};
})();
return requires;
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"description": "Write minimal node index.js files that require and export siblings by file basename",

"version": "0.1.7",
"version": "0.1.8",

"license" : "MIT",

Expand Down
1 change: 1 addition & 0 deletions test/lib/not_javascript.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
asdf 1 2 / @ 123
14 changes: 8 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var assert = require('assert')
var assert = require('assert');

var lib = require('./lib')
var lib = require('./lib');

try {
var expectations = {
Expand All @@ -12,15 +12,16 @@ try {
"ack": lib.bam.n,
"again": lib.bar.fed.again,
"somemore": lib.bar.fed.somemore
}
};

var keys = Object.keys(expectations)
keys.forEach(function(expectation) {
var keys = Object.keys(expectations);
keys.forEach(function (expectation) {
assert.equal(expectations[expectation](), expectation);
})
});

assert.equal(('_private' in lib), false);
assert.equal(('ignored' in lib.bar.fed), false);
assert.equal(('not_javascript' in lib), false);

assert.equal(Object.keys(lib)[0], 'bam');

Expand All @@ -30,4 +31,5 @@ try {
console.log("Test Failed.");
console.log(" Expected: " + error.expected);
console.log(" Actual: " + error.actual);
console.log(error);
}

0 comments on commit 6ab36b4

Please sign in to comment.