JSFiddle - React, Tailwind, and code Playground
by toddmotto
JavaScript
/**
* annotate remake
*/
var FN_ARGS = /^function\s*[^\(]*\(\s*([^\)]*)\)/m;
var FN_ARG_SPLIT = /,/;
var FN_ARG = /^\s*(_?)(\S+?)\1\s*$/;
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
function annotate (fn) {
var $inject;
if (typeof fn === 'function') {
$inject = [];
fn = fn.toString();
var first = fn.replace(STRIP_COMMENTS, '');
var second = first.match(FN_ARGS)[1];
var third = second.split(FN_ARG_SPLIT);
third.forEach(function (arg) {
arg.replace(FN_ARG, function(all, underscore, name){
$inject.push(name);
});
});
} else {
$inject = fn.slice(0, fn.length - 1);
}
return $inject;
}
/**
* functions
*/
function withoutDependencies ($scope, $rootScope, SomeFactory) {
console.log($scope, $rootScope, SomeFactory);
}
function withDependencies (a, b, c) {
console.log(a, b, c);
}
var annotatedWithout = annotate(withoutDependencies);
withoutDependencies.apply(null, annotatedWithout);
var annotatedWith = annotate(['$scope', '$rootScope', 'SomeFactory', withDependencies]);
withDependencies.apply(null, annotatedWith);