Last active
December 25, 2015 05:19
-
-
Save thebyrd/6924190 to your computer and use it in GitHub Desktop.
A method that will inject npm modules listed as parameters in a constructor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function SomeConstructor (request, npm, monk) { | |
// do something with the injected node modules | |
} | |
var instance = require('./injector')(SomeConstructor) // create a new instance with dependencies injected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function (C) { | |
var params = C.toString() | |
.replace(/((\/\/.*$)|(\/\*[\s\S]*?\*\/)|(\s))/mg,'') | |
.match(/^function\s*[^\(]*\(\s*([^\)]*)\)/m)[1] | |
.split(/,/) | |
for (var i = 0, param; param = params[i]; i++) | |
C = C.bind(this, require(param)) | |
return new C() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@jdan @ox -> Still need to figure out what to do about npm modules with a dash in the middle like "aws-sdk"