modularity
Advanced tools
+15
-19
@@ -56,3 +56,3 @@ var EventEmitter = require('events').EventEmitter | ||
| forEach(dependencies, function (dependency, next) { | ||
| if (dependency === 'callback') { | ||
| if (dependency === 'callback' || typeof dependency === 'function') { | ||
| return next(); | ||
@@ -74,5 +74,5 @@ } | ||
| var module_deps; | ||
| if (Array.isArray(module)) { | ||
| if (Array.isArray(module) && typeof module[module.length - 1] === 'function') { | ||
| module_deps = module; | ||
| module = module_deps.pop(); | ||
| module = module[module.length - 1]; | ||
| } else if (typeof module === 'function') { | ||
@@ -113,20 +113,16 @@ module_deps = parseArgs(module); | ||
| Modularity.prototype.require = function (parent, dependency, callback) { | ||
| var attempts = [], require_path, module, module_path, module_name; | ||
| var attempts = [], module, module_path; | ||
| for (var i = 0, len = this.paths.length; i < len; i++) { | ||
| module_name = dependency; | ||
| require_path = this.paths[i]; | ||
| do { | ||
| module_path = path.join(require_path, module_name); | ||
| try { | ||
| module = require(module_path); | ||
| return callback(null, module, module_path); | ||
| } catch (e) { | ||
| if (typeof e !== 'object' || | ||
| e.code !== 'MODULE_NOT_FOUND' || | ||
| e.message.indexOf(module_path) === -1) { | ||
| return callback(e); | ||
| } | ||
| attempts.push(module_path); | ||
| module_path = path.join(this.paths[i], dependency); | ||
| try { | ||
| module = require(module_path); | ||
| return callback(null, module, module_path); | ||
| } catch (e) { | ||
| if (typeof e !== 'object' || | ||
| e.code !== 'MODULE_NOT_FOUND' || | ||
| e.message.indexOf(module_path) === -1) { | ||
| return callback(e); | ||
| } | ||
| } while (module_name !== (module_name = module_name.replace('_', path.sep))); | ||
| attempts.push(module_path); | ||
| } | ||
| } | ||
@@ -133,0 +129,0 @@ var requires = attempts.map(function (module_path) { |
+1
-1
| { | ||
| "name": "modularity", | ||
| "version": "1.1.0", | ||
| "version": "2.0.0", | ||
| "description": "Module loader", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+20
-7
@@ -12,9 +12,8 @@ ## Modularity | ||
| //Load the server and config module from ./lib | ||
| modularity.load(__dirname + '/lib', function (server, config) { | ||
| server.listen(config.port); | ||
| console.log('Listening on %s', config.port); | ||
| }); | ||
| modularity | ||
| .include(__dirname + '/lib') | ||
| .load(function (server, config) { | ||
| server.listen(config.port); | ||
| console.log('Listening on %s', config.port); | ||
| }); | ||
| ``` | ||
@@ -63,2 +62,16 @@ | ||
| ## Dependency injection | ||
| ```javascript | ||
| var modularity = require('modularity'); | ||
| modularity | ||
| .include(__dirname + '/lib') | ||
| .inject({ config: test_config }) | ||
| .load(function (server, config) { | ||
| server.listen(config.port); | ||
| console.log('Listening on %s', config.port); | ||
| }); | ||
| ``` | ||
| ## License (MIT) | ||
@@ -65,0 +78,0 @@ |
@@ -1,1 +0,1 @@ | ||
| module.exports = 'qux'; | ||
| module.exports = ['q', 'u', 'x']; |
| module.exports = ['bar/baz', 'bar/baz_foo', 'bar/baz/qux', function (baz, foo, qux) { | ||
| return baz + foo + qux; | ||
| return baz + foo + qux.join(''); | ||
| }]; | ||
+1
-15
@@ -108,17 +108,3 @@ /*jshint unused:false */ | ||
| it('should load dependencies from subdirectories', function (done) { | ||
| loadTest(7, function (foo) { | ||
| assert.equal(foo, 'bar'); | ||
| done(); | ||
| }); | ||
| }); | ||
| it('should replace underscores with path separators when resolving dependencies', function (done) { | ||
| loadTest(8, function (foo) { | ||
| assert.equal(foo, 'bazfooqux'); | ||
| done(); | ||
| }); | ||
| }); | ||
| it('should support angular.js style array syntax', function (done) { | ||
| it('should support angular.js style array syntax for subdirectories', function (done) { | ||
| loadTest(9, function (foo) { | ||
@@ -125,0 +111,0 @@ assert.equal(foo, 'bazfooqux'); |
| module.exports = function (foo_bar) { | ||
| return foo_bar; | ||
| }; | ||
| module.exports = function () { | ||
| return 'bar'; | ||
| }; | ||
| module.exports = 'foo'; |
| module.exports = 'baz'; |
| module.exports = 'qux'; |
| module.exports = function (bar_baz, bar_baz_foo, bar_baz_qux) { | ||
| return bar_baz + bar_baz_foo + bar_baz_qux; | ||
| }; | ||
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
98
15.29%14703
-3.6%24
-20%294
-8.7%