It's a very simple & basic dependency injection script. It's nothing fancy, but once you tasted DI, you can't live without it. This is what I actually use for my non-angular projects.
_="84){B3{_Yq_qXBSV^[^ (]* (([^ )]*) )/m,_SPLITV,/,V^(_?)( S+?) 1$/,STRIP_COMMENTSV((UU.*$)|(U *[ s S]*? *U))/mg,Z.toString(),#gsZ.match(S)[1].split(/,/6.apply(.9D(#gs)7,9DX#rB<f,dep#r.map8if(!Y && r([]7Y[]6dep,_Yz.`.5Fn~? 3.~%,z%,`{5FnXfnWtypeof fn == ''}};43)(6833.2\"2\"2(abefW't>-@@'\" nwhile t>-+ +'\"'8console.logKT> guy sa5: n'@)$'WalJtKhijacked jQuJy!'78$$K<H'77)(6functionependenceg5tJreturn sm#tyPants) {63. = \\.hasOwnPropJty()rX0%._<f._processvalueth5iest#9FN_ARG.dragonInjH, ;}(dogcat s*: toRrK(W\"I'm a #ar%dy- says\"'0name2' 3di4global5is6);7)8(9get<sel>he @ + Bv# HectorJerK('U /V/WXYdZtext`utilq{},z[0]~(%) ";for(Y in $="~zq`ZYXWVUKJHB@><987654320-%# ")with(_.split($[Y]))_=join(pop());eval(_)
Xz0iODQpe0IzCHtfWXFfHXEPWBMGQhRTVl4BG1teCShdKgkoGyhbXgkpXSopCSkvbSwUX1NQTElUViwvLBRWXhsoXz8pKAlTKz8pCTEbJC8sU1RSSVBfQ09NTUVOVFNWKChVVS4qJCl8KFUJKlsJcwlTXSo/CSpVKSkvbWcsWggTLnRvU3RyaW5nKCksI2dzCFoubWF0Y2goFFMpWzFdLnNwbGl0KC8bLBsvNgQTLmFwcGx5KBMWES45RBIoI2dzKTcsOUQSWCNyBkI8ZggRLGRlcAgjci5tYXA4EAZpZighDlkLICYmIA4dCwYOcgMoEBYOHVsQXTcEDllbEF0XNgRkZXAXLF8MWXoRLmAuNUZufj8gMy4PfhwlFywMHXolFyxgHHs1Rm5YZm5XdHlwZW9mIGZuID09ICcBJxd9fTs0FQgzFykoETY4MwYzLh4ZMhkfGVwiBx4aMhofGlwiBx4FMgUoYRZiFhoWGRZlFmZXJ3Q+GS1AGUAnXCIWCW53aGlsZSB0PhotKyAaICsnXCInBw84BQZjb25zb2xlLmxvZ0tUPgUgZ3V5IHNhNToJbidABSkHHiQnFhhXGAZhbEp0S2hpamFja2VkIGpRdUp5ISc3Bw84JAYkSzxIJzc3KSgRFTYBZnVuY3Rpb24CZXBlbmRlbmMDZWc1dEoEcmV0dXJuIAVzbSN0eVBhbnRzBikgewcXNjMuCCA9IAlcXAsuaGFzT3duUHJvcEp0eSgQKQxyA1gwFiUGES5fDjxmLl8PcHJvY2VzcxB2YWx1ZRF0aDUSAmllcxN0IzkURk5fQVJHFS5kcmFnb25JbmpIFiwgFzt9GAEoGWRvZxpjYXQbCXMqHDogHXRvUgMecgNLHyhXXCJJJ20gYSAjYXIlZAJ5LSBzYXlzHFwiJzBuYW1lMicWASAzZGk0Z2xvYmFsNWlzNik7NykXOCgYOWdldDxzZWw+aGUgQCArIEJ2IyBIZWN0b3JKZXJLKCdVCS9WCC9XBgRYHBhZZBJadGV4dGB1dGlscRx7fSx6WzBdCH4oJSkgIjtmb3IoWSBpbiAkPSJ+enFgWllYV1ZVS0pIQkA+PDk4NzY1NDMyMC0lIx8eHRwbGhkYFxYVFBMSERAPDgwLCQgHBgUEAwIBIil3aXRoKF8uc3BsaXQoJFtZXSkpXz1qb2luKHBvcCgpKTtldmFsKF8p
// main lib
(function(global){
var di = {
_dependencies: {},
_toRegister: {},
process: function(target) {
var FN_ARGS = /^function\s*[^\(]*\(\s*([^\)]*)\)/m,
FN_ARG_SPLIT = /,/,
FN_ARG = /^\s*(_?)(\S+?)\1\s*$/,
STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg,
text = target.toString(),
args = text.match(FN_ARGS)[1].split(/\s*,\s*/);
return target.apply(target, this.getDependencies(args));
},
getDependencies: function(arr) {
var self = this,
dep = arr.map(function(value) {
// if the dependency has been queued for registration, but hasn't been registered yet,
// force the registration process
if(!self._dependencies.hasOwnProperty(value) && self._toRegister.hasOwnProperty(value)) {
self._register(value, self._toRegister[value]);
}
return self._dependencies[value];
});
return dep;
},
_register: function(name, dependency) {
this._dependencies[name] = this.util.isFn(dependency) ? di.process(dependency) : dependency;
},
register: function(name, dependency) {
this._toRegister[name] = dependency;
},
util: {
isFn: function(fn) {
return typeof fn == 'function';
}
}
};
// give back Ceaser what's Ceaser's
global.dragonInjector = di;
})(this);
// demo
(function(di, global, undefined) {
// an example that shows the DI magic
// this is a dog
di.register('dog', function dog() {
return "I'm a dog";
});
// this is a cat
di.register('cat', function cat() {
return "I'm a cat";
});
// this is a smart dude, but he needs
// a dog and acat in order to be smart
// note: you can alter the number/order
// of the arguments and see it's still working
di.register('smartyPants', function smartyPants(a, b, cat, dog, e, f) {
return 'the dog says: "' + dog + '", \nwhile the cat says: "'+ cat +'"';
});
di.process(function(smartyPants) {
console.log('The smartyPants guy sais:\n' + smartyPants);
});
// another example
// you can hijack jQuery
// by registering a function as $
di.register('$', function() {
return function() {
alert('hijacked jQuery!');
};
});
// and use that function instead of the normal jQuery
di.process(function($) {
$('selector');
});
})(this.dragonInjector, this);