require-all
Advanced tools
Comparing version 2.0.0 to 2.1.0
17
index.js
@@ -21,2 +21,13 @@ var fs = require('fs'); | ||
function filterFile(filename) { | ||
if (typeof filter === 'function') { | ||
return filter(filename); | ||
} | ||
var match = filename.match(filter); | ||
if (!match) return; | ||
return match[1]; | ||
} | ||
var files = fs.readdirSync(dirname); | ||
@@ -39,6 +50,6 @@ | ||
} else { | ||
var match = file.match(filter); | ||
if (!match) return; | ||
var name = filterFile(file); | ||
if (!name) return; | ||
modules[map(match[1], filepath)] = resolve(require(filepath)); | ||
modules[map(name, filepath)] = resolve(require(filepath)); | ||
} | ||
@@ -45,0 +56,0 @@ }); |
{ | ||
"name": "require-all", | ||
"description": "An easy way to require all files within a directory.", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"author": "Felix Geisendörfer <felix@debuggable.com> (http://debuggable.com/)", | ||
"contributors": [ | ||
"Douglas Christopher Wilson <doug@somethingdoug.com>", | ||
"Prince Obiechine Onyenike <leewaygroups@gmail.com>", | ||
"Nuno Job <nunojobpinto@gmail.com> (http://nunojob.com)" | ||
@@ -16,2 +17,3 @@ ], | ||
"files": [ | ||
"Changes.md", | ||
"LICENSE", | ||
@@ -18,0 +20,0 @@ "index.js", |
@@ -5,2 +5,6 @@ # require-all | ||
[![NPM Version][npm-image]][npm-url] | ||
[![NPM Downloads][downloads-image]][downloads-url] | ||
[![Build Status][travis-image]][travis-url] | ||
## Usage | ||
@@ -58,1 +62,32 @@ | ||
``` | ||
### Filtering files | ||
If your directory contains files that you do not want to require, or that you want only a part of the file's name to be used as the property name, `filter` can be a regular expression. In the following example, the `filter` is set to `/^(.+Controller)\.js$/`, which means only files that end in "Conroller.js" 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. | ||
```js | ||
var controllers = require('require-all')({ | ||
dirname : __dirname + '/controllers', | ||
filter : /^(.+Controller)\.js$/ | ||
}); | ||
``` | ||
For even more advanced usage, the `filter` option also accepts a function that is invoked with the file name as the first argument. The filter function is expected to return a falsy value to ignore the file, otherwise a string to use as the property name. | ||
```js | ||
var controllers = requireAll({ | ||
dirname : __dirname + '/controllers', | ||
filter : function (fileName) { | ||
var parts = fileName.split('-'); | ||
if (parts[1] !== 'Controller.js') return; | ||
return parts[0]; | ||
} | ||
}); | ||
``` | ||
[npm-image]: https://img.shields.io/npm/v/require-all.svg | ||
[npm-url]: https://npmjs.org/package/require-all | ||
[downloads-image]: https://img.shields.io/npm/dm/require-all.svg | ||
[downloads-url]: https://npmjs.org/package/require-all | ||
[travis-image]: https://img.shields.io/travis/felixge/node-require-all/master.svg | ||
[travis-url]: https://travis-ci.org/felixge/node-require-all |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8067
5
47
92