Last active
December 17, 2015 05:59
-
-
Save dustingetz/5562409 to your computer and use it in GitHub Desktop.
angularjs's annotate(fn) extracted and ported to underscore
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
/** | |
* Parse a constructor's signature to find out the names of his arguments. This | |
* is central to AngularJS dependency injection mechanism. | |
*/ | |
function annotate(fn) { | |
var $inject, fnText, argDecl, last; | |
if (typeof fn == 'function') { | |
if (!($inject = fn.$inject)) { | |
$inject = []; | |
fnText = fn.toString().replace(STRIP_COMMENTS, ''); | |
argDecl = fnText.match(FN_ARGS); | |
_.forEach(argDecl[1].split(FN_ARG_SPLIT), function(arg) { | |
arg.replace(FN_ARG, function(all, underscore, name) { | |
$inject.push(name); | |
}); | |
}); | |
fn.$inject = $inject; | |
} | |
} else if (_.isArray(fn)) { | |
last = fn.length - 1; | |
//assertArgFn(fn[last], 'fn') | |
$inject = fn.slice(0, last); | |
} else { | |
//assertArgFn(fn, 'fn', true); | |
} | |
return $inject; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
supporting blog: http://www.dustingetz.com/2013/05/13/javascript-dependency-injection-angularjs-style.html