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 hidden or 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 hidden or 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
This is fire. My only concern is that "magic parameters" shepherd scare the shit out of me.
Super cool though. "I don't know regular expressions."