Comparing version 1.2.0 to 1.3.0
40
index.js
@@ -6,9 +6,18 @@ var fs = require('fs') | ||
module.exports = load | ||
/** | ||
* If options is function, it will be used as options.init | ||
* | ||
* @param {Function} options.init Initialize target module | ||
* @param {string} options.naming Exported module name conversion, support camelcase and pascalcase | ||
* @param {boolean} options.lazy Whether to lazy load module | ||
* @param {Function} options.filter Filter unwanted modules | ||
* @param {Function} options.exports Load modules into custom object | ||
*/ | ||
module.exports = function load(dirname, options) { | ||
options = options || {} | ||
function load(dir, options) { | ||
options = options || {} | ||
if (typeof options === 'function') { | ||
options = {init: options} | ||
} | ||
switch (options.naming) { | ||
@@ -21,21 +30,21 @@ case 'camel': | ||
break | ||
default: | ||
options.naming = identify | ||
break | ||
} | ||
// default options | ||
options = merge(options, { | ||
init: identify, | ||
naming: identify, | ||
lazy: true, | ||
init: identify, | ||
filter: function(modulePath) { return false }, | ||
}) | ||
var modules = options.exports || {} | ||
fs.readdirSync(dir).forEach(function(filename) { | ||
fs.readdirSync(dirname).forEach(function(filename) { | ||
// filter index and dotfiles | ||
var stat = fs.statSync(path.join(dir, filename)) | ||
var isModule = stat.isFile() && path.extname(filename) === '.js' && | ||
filename !== 'index.js' && filename[0] !== '.' | ||
if (stat.isDirectory() || isModule) { | ||
var moduleName = path.basename(filename, path.extname(filename)) | ||
var modulePath = path.join(dir, moduleName) | ||
var exportName = options.naming(moduleName) | ||
var stat = fs.statSync(path.join(dirname, filename)) | ||
var isModule = stat.isFile() && path.extname(filename) === '.js' && filename !== 'index.js' && filename[0] !== '.' | ||
var moduleName = path.basename(filename, path.extname(filename)) | ||
var modulePath = path.join(dirname, moduleName) | ||
var exportName = options.naming(moduleName) | ||
if ((stat.isDirectory() || isModule) && !options.filter(moduleName, modulePath, exportName)) { | ||
// lazy load | ||
@@ -53,3 +62,2 @@ if (options.lazy) { | ||
}) | ||
return modules | ||
@@ -56,0 +64,0 @@ } |
{ | ||
"name": "moder", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Module loader", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -67,2 +67,13 @@ var path = require('path'), | ||
}) | ||
it('should allow custom filter', function() { | ||
var case10 = moder(__dirname + '/modules/case10', { | ||
lazy: false, | ||
filter: function(moduleName) { | ||
return moduleName.indexOf('-test') !== -1 | ||
}, | ||
}) | ||
assert.deepEqual(case10.foo, 'hello world') | ||
assert.deepEqual(case10['foo-test'], undefined) | ||
}) | ||
}) |
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
8619
147
26