Skip to content

Commit

Permalink
update test to asserts 4.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenhandley committed May 1, 2013
1 parent 7a60cca commit 3088432
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 65 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
language: node_js
node_js:
- 0.4
- 0.6
- 0.8
- 0.10
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ Write minimal node index.js files that require and export siblings by file basen

# Latest Version

1.0.0
1.0.1

# Installation
```
npm install requireindex
```

or in package.json
or in package.json

```json
{
...
"dependencies": {
"requireindex": "~1.0.0"
"requireindex": "1.0.x"
}
}
```
Expand All @@ -40,7 +40,7 @@ lib/
somemore.js
bam.js
_private.js
```

The index.js files in [test/lib/](https://github.com/stephenhandley/requireindex/tree/master/test/lib/index.js) and [test/lib/bar/](https://github.com/stephenhandley/requireindex/tree/master/test/lib/bar/index.js) contain:
Expand All @@ -66,23 +66,23 @@ require('lib');
is:

```js
{
bam: {
m: [Function],
n: [Function]
{
bam: {
m: [Function],
n: [Function]
},
bar: {
bar: {
f: [Function],
fed: {
again: [Function],
somemore: [Function]
fed: {
again: [Function],
somemore: [Function]
},
fing: [Function]
fing: [Function]
},
Foo: {
l: [Function],
ls: [Function]
}
Foo: {
l: [Function],
ls: [Function]
}
}
```

Expand Down
51 changes: 27 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
var fs = require('fs');
var path = require('path');
var FS = require('fs');
var Path = require('path');

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

if (arguments.length === 1) {

if (arguments.length === 2) {
// 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);
});

} else if (arguments.length === 1) {
// if basenames arguments isn't passed, require all javascript
// files (except for those prefixed with _) and all directories
var files = fs.readdirSync(dir);

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) {
Expand All @@ -23,30 +30,26 @@ module.exports = function (dir, basenames) {
return 0;
}
});

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);

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);

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

requires[basename] = require(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);
});
throw new Error("Must pass directory as first argument");
}

return requires;
};
28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
{
"name": "requireindex",
"description": "Write minimal node index.js files that require and export siblings by file basename",
"version": "1.0.0",
"version": "1.0.1",
"license" : "MIT",
"main": "index.js",

"repository": {
"type": "git",
"url": "git://github.com/stephenhandley/requireindex.git"
},

"scripts": {
"test": "node test/test.js"
},

"keywords": [
"require",
"index",
"require",
"index",
"index.js"
],

"directories" : {
"lib" : ".",
"test" : "test"
},
"bugs": {

"bugs": {
"url" : "http://github.com/stephenhandley/requireindex/issues"
},

"engines" : {
"node" : ">=0.4.0"
"node" : ">=0.10.5"
},

"devDependencies": {
"asserts": "~2.0.0"
"asserts": "4.0.x"
},

"author": {
"name": "Stephen Handley",
"email": "[email protected]",
Expand Down
14 changes: 7 additions & 7 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ var Asserts = require('asserts');

Asserts(function () {
var lib = require('./lib');

return {
"requireindex should": {
"properly include files parallel to index.js and maintain structure": function () {
Asserts.allEqual([
Asserts.all.equal([
[lib.bam.m, [], "ok"],
[lib.bar.f, [], "yea"],
[lib.bar.fing, [], 'definitely'],
Expand All @@ -18,22 +18,22 @@ Asserts(function () {
[lib.bar.fed.somemore, [], 'somemore']
]);
},

"ignore _ prefixed files": function () {
Assert.equal(('_private' in lib), false);
},

"not include files not mentioned when second array argument is used": function () {
Assert.equal(('ignored' in lib.bar.fed), false);
},

"ignore non javascript files": function () {
Assert.equal(('not_javascript' in lib), false);
},

"sort files by lowercase alpha of the filename": function () {
Assert.equal(Object.keys(lib)[0], 'bam');
}
},
}
};
});

0 comments on commit 3088432

Please sign in to comment.