Created
August 27, 2014 07:43
-
-
Save kanakiyajay/3c72820e9078bf64ece1 to your computer and use it in GitHub Desktop.
Angular : The actual code behind the magical angular injector dependencies.
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
var FN_ARGS = /^function\s*[^\(]*\(\s*([^\)]*)\)/m; | |
var FN_ARG_SPLIT = /,/; | |
var FN_ARG = /^\s*(_?)(\S+?)\1\s*$/; | |
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; | |
var $injectorMinErr = minErr('$injector'); | |
function annotate(fn) { | |
var $inject, | |
fnText, | |
argDecl, | |
last; | |
if (typeof fn === 'function') { | |
if (!($inject = fn.$inject)) { | |
$inject = []; | |
if (fn.length) { | |
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