Skip to content

Commit

Permalink
Accept "filter" RegExps without any capture groups
Browse files Browse the repository at this point in the history
closes #43
fixes #46
  • Loading branch information
feliun authored and dougwilson committed Feb 19, 2017
1 parent 2589709 commit 8fc6316
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Changes.md
Expand Up @@ -4,6 +4,10 @@ This file is a manually maintained list of changes for each release. Feel free
to add your changes here when sending pull requests. Also send corrections if
you spot any mistakes.

## HEAD

* Accept "filter" RegExps without any capture groups #43 #46

## v2.1.0 (2016-12-09)

* Accept a function for "filter" option #27 #31
Expand Down
3 changes: 2 additions & 1 deletion Readme.md
Expand Up @@ -73,7 +73,8 @@ can be a regular expression. In the following example, the `filter` is set to
are required, and the resulting property name will be the name of the file
without the ".js" extension. For example, the file "MainController.js" will
match, and since the first capture group will contain "MainController", that
will be the property name used.
will be the property name used. If no capture group is used, then the entire
match will be used as the name.

```js
var controllers = require('require-all')({
Expand Down
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -26,7 +26,7 @@ module.exports = function requireAll(options) {
var match = filename.match(filter);
if (!match) return;

return match[1];
return match[1] || match[0];
}

var files = fs.readdirSync(dirname);
Expand Down
23 changes: 23 additions & 0 deletions test/test.js
Expand Up @@ -79,6 +79,29 @@ assert.deepEqual(controllersMap, {
}
});

var controllersMap = requireAll({
dirname: __dirname + '/controllers',
filter: /.+Controller\.js$/
});

assert.deepEqual(controllersMap, {
'main-Controller.js': {
index: 1,
show: 2,
add: 3,
edit: 4
},
'other-Controller.js': {
index: 1,
show: 'nothing'
},
'sub-dir': {
'other-Controller.js': {
index: 1,
show: 2
}
}
});

controllersMap = requireAll({
dirname: __dirname + '/controllers',
Expand Down

0 comments on commit 8fc6316

Please sign in to comment.