freight.js
Version:
An simple CommonJS dependency injection container for both node.js and the browser (using browserify)
1 lines • 4.29 kB
JavaScript
!function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);throw new Error("Cannot find module '"+g+"'")}var j=c[g]={exports:{}};b[g][0].call(j.exports,function(a){var c=b[g][1][a];return e(c?c:a)},j,j.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b){var c=a("./src/freight.js");b.exports=c},{"./src/freight.js":3}],2:[function(a,b){var c=function(a,b,c){var d=this;d.id=a,d.fn=!1,d.args=[],d.tags=[],d.service=!1,d.shared=!1,d.retrieve=function(){if(d.shared===!0&&d.service!==!1)return d.service;var b=d.fn();if(void 0===b)throw new Error('"'+a+'" service returned "undefined" on instantiation.');return d.service=b,b},d.addArgument=function(a){return d.args.push(a),d},d.addTag=function(a){return-1===d.tags.indexOf(a)&&d.tags.push(a),d},d.resolveArguments=function(a){return a="undefined"!=typeof a?a:d.args,a.forEach(function(b,e){c.isId(b)?a[e]=c.get(b.replace(c.idPrefix,"")):Array.isArray(b)?a[e]=d.resolveArguments(b):b.constructor==Object&&Object.keys(b).forEach(function(a){c.isId(b[a])&&(b[a]=c.get(b[a].replace(c.idPrefix,"")))})}),a},d._createCallable=function(a,b){function e(a,b){var c=Object.create(a.prototype),d=a.apply(c,b);return"object"==typeof d?d:c}var f=!1,g=function(e){var f=!1;if("function"==typeof e)f=e;else try{f=c.getParameter(e.replace(c.idPrefix,""))}catch(g){throw new Error('Whene trying to instanciate service "'+d.id+'", the parameter specifying the '+("f"==b?"factory":"constructor")+': "'+a+'" did not exist.')}if("function"!=typeof f)throw new Error('Whene trying to instanciate service "'+d.id+'", the parameter specifying the '+("f"==b?"factory":"constructor")+': "'+a+'" did not return a function, instead received: "'+f+'"');return f};return"c"===b?f=function(){var b=new g(a),c=d.resolveArguments();return e(b,c)}:function(){return g(a).apply(c,d.resolveArguments())}},d.configure=function(a){if(void 0!==a.shared)if("true"===String(a.shared).toLowerCase())d.shared=!0;else{if("false"!==String(a.shared).toLowerCase())throw new Error('Configuration of "'+d.id+'" shared should be an boolean, received: "'+a+'"');d.shared=!1}var b=a.constructorFn,e="c";if(void 0===b)b=a.factoryFn,e="f";else if(void 0!==a.factoryFn)throw new Error('Configuration of "'+d.id+'" should specify a constructor OR an factory function, not both - received: "'+a+'"');if(void 0===b)throw new Error('Configuration of "'+d.id+'" must either specify an constructor or an factory function - received: "'+a+'"');if(c.isId(b)===!1&&"function"!=typeof b)throw new Error('Constructor or an factory function of "'+d.id+'" should be specified using a parameter id - received: "'+b+'"');d.fn=d._createCallable(b,e);var f=a.arguments;if(void 0!==f){if(!Array.isArray(f))throw new Error('Instantiation arguments of "'+d.id+'" should be specified as an array - received: "'+f+'"');f.forEach(function(a){d.addArgument(a)})}var g=a.tags;if(void 0!==g){if(!Array.isArray(g))throw new Error('Service tags of "'+d.id+'" should be specified as an array - received: "'+g+'"');g.forEach(function(a){d.addTag(a)})}return d},d.configure(b)};b.exports=c},{}],3:[function(a,b){var c=a("./definition.js"),d=function(){var a=this;a.idPrefix=":",a._definitions=[],a._parameters={},a.get=function(b){var c=!1;try{c=a.getParameter(b)}catch(d){c=a.getService(b)}return c},a.isId=function(b){return"string"!=typeof b?!1:0===b.indexOf(a.idPrefix)?!0:!1},a.setParameter=function(b,c){return a._parameters[b]=c,a},a.getParameter=function(b){var c=a._parameters[b];if(void 0===c)throw new Error('Parameter "'+b+'" not found.');return c},a.findTaggedServiceIds=function(b){var c=[];return a._definitions.forEach(function(a){-1!==a.tags.indexOf(b)&&c.push(a.id)}),c},a.getDefinition=function(b){var c=!1;return a._definitions.forEach(function(a){a.id==b&&(c=a)}),c},a.getService=function(b){var c=a.getDefinition(b);if(c===!1)throw new Error('Service "'+b+'" not found.');var d=c.retrieve();return d},a.register=function(b,d){var e=new c(b,d,a);return a._definitions.push(e),e},a.registerAll=function(b){var c=[];return Object.keys(b).forEach(function(d){c.push(a.register(d,b[d]))}),c}};b.exports=d},{"./definition.js":2}]},{},[1]);