Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let use file with no extension #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var fs = require('fs'),
function checkFileInclusion(path, filename, options) {
return (
// verify file has valid extension
(new RegExp('\\.(' + options.extensions.join('|') + ')$', 'i').test(filename)) &&
(new RegExp('^(' + options.extensions.join('|') + ')$', 'i').test((filename.match(/\.(.+)/) || ['']).pop())) &&

// if options.include is a RegExp, evaluate it and make sure the path passes
!(options.include && options.include instanceof RegExp && !options.include.test(path)) &&
Expand Down Expand Up @@ -60,7 +60,8 @@ function requireDirectory(m, path, options) {
var joined = join(path, filename),
files,
key,
obj;
obj,
dotId;

if (fs.statSync(joined).isDirectory() && options.recurse) {
// this node is a directory; recurse
Expand All @@ -72,7 +73,8 @@ function requireDirectory(m, path, options) {
} else {
if (joined !== m.filename && checkFileInclusion(joined, filename, options)) {
// hash node key shouldn't include file extension
key = filename.substring(0, filename.lastIndexOf('.'));
dotId = filename.lastIndexOf('.');
key = filename.substring(0, dotId < 0 ? filename.length : dotId);
obj = m.require(joined);
retval[options.rename(key, joined, filename)] = options.visit(obj, joined, filename) || obj;
}
Expand Down
1 change: 1 addition & 0 deletions test/example/noext
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'noext!';
3 changes: 2 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@

test('should be able to specify supported extensions', function () {
//act
var test = reqdir(module, PATH_TO_EXAMPLE, {extensions: ['json']});
var test = reqdir(module, PATH_TO_EXAMPLE, {extensions: ['json', '']});

//assert
assert.equal(undefined, test.foo);
assert.equal('be', test.bun.should);
assert.equal('noext!', test.noext);
});

test('should be able to exclude path and still pass options', function () {
Expand Down