@soundstep/soma
Version:
Soma is a javascript framework created to build scalable and maintainable applications.
2 lines • 7.63 kB
JavaScript
/* infuse - v3.0.1 - 10/12/2019 - https://github.com/soundstep/infuse */
!function(t){"use strict";var r=/^function\s*[^\(]*\(\s*([^\)]*)\)/m,n=/(?!function)\s*constructor\s*[^\(|function]*\(\s*([^\)]*)\)\s*{/m,e=/,/,i=/^\s*(_?)(\S+?)\1\s*$/,o=/((\/\/.*$)|(\/\*[\s\S]*?\*\/))/gm;t.errors={MAPPING_BAD_PROP:"[Error Injector.MAPPING_BAD_PROP] The first parameter is invalid, a string is expected.",MAPPING_BAD_VALUE:"[Error Injector.MAPPING_BAD_VALUE] The second parameter is invalid, it can't null or undefined, with property: ",MAPPING_BAD_CLASS:"[Error Injector.MAPPING_BAD_CLASS] The second parameter is invalid, a function is expected, with property: ",MAPPING_BAD_SINGLETON:"[Error Injector.MAPPING_BAD_SINGLETON] The third parameter is invalid, a boolean is expected, with property: ",MAPPING_ALREADY_EXISTS:"[Error Injector.MAPPING_ALREADY_EXISTS] This mapping already exists, with property: ",CREATE_INSTANCE_INVALID_PARAM:"[Error Injector.CREATE_INSTANCE_INVALID_PARAM] Invalid parameter, a function is expected.",NO_MAPPING_FOUND:"[Error Injector.NO_MAPPING_FOUND] No mapping found",INJECT_INSTANCE_IN_ITSELF_PROPERTY:"[Error Injector.INJECT_INSTANCE_IN_ITSELF_PROPERTY] A matching property has been found in the target, you can't inject an instance in itself.",INJECT_INSTANCE_IN_ITSELF_CONSTRUCTOR:"[Error Injector.INJECT_INSTANCE_IN_ITSELF_CONSTRUCTOR] A matching constructor parameter has been found in the target, you can't inject an instance in itself.",DEPENDENCIES_MISSING_IN_STRICT_MODE:'[Error Injector.DEPENDENCIES_MISSING_IN_STRICT_MODE] An "inject" property (array) that describes the dependencies is missing in strict mode.',DEPENDENCIES_MISSING_IN_STRICT_MODE_CONSTRUCTOR_INJECTION:'[Error Injector.DEPENDENCIES_MISSING_IN_STRICT_MODE_CONSTRUCTOR_INJECTION] An "inject" property (array) that describes the dependencies of constructor is missing in strict mode.',DEPENDENCIES_INVALID_TARGET:"[Error Injector.DEPENDENCIES_INVALID_TARGET] Invalid target, a function or a class is expected (arrow function cannot be instantiated)."};var s=function(t,r,n,e){this.prop=t,this.value=r,this.cl=n,this.singleton=e||!1},a=function(r){if("string"!=typeof r)throw new Error(t.errors.MAPPING_BAD_PROP)},p=function(r,n){if(function(t,r){for(var n=t.length;n--;)if(t[n]===r)return!0;return!1}(t.getDependencies(n),r))throw new Error(t.errors.INJECT_INSTANCE_IN_ITSELF_CONSTRUCTOR)},c=function(r,n){var e=void 0!==r?' for the injection name: "'+r+'"':"",i=void 0!==n?' when instantiating: "'+n+'"':"";return t.errors.NO_MAPPING_FOUND+e+i+"."};t.Injector=function(){this.mappings={},this.parent=null,this.strictMode=!1,this.strictModeConstructorInjection=!1,this.throwOnMissing=!0},t.getDependencies=function(s){var a,p=[];function c(t,r,n){p.push(n)}s.hasOwnProperty("inject")&&"[object Array]"===Object.prototype.toString.call(s.inject)&&s.inject.length>0&&(a=s.inject);var h,u=s.toString().replace(o,"");if(0===u.indexOf("class"))h=u.replace(/function constructor/g,"").match(n);else{if(0!==u.indexOf("function"))throw new Error(t.errors.DEPENDENCIES_INVALID_TARGET);h=u.match(r)}if(h)for(var I=h[1].split(e),E=0,l=I.length;E<l;E++){var f=I[E].split("=")[0].replace(/\s/g,"");(a&&a[E]?a[E]:f).replace(i,c)}return p},t.Injector.prototype={createChild:function(){var r=new t.Injector;return r.parent=this,r.strictMode=this.strictMode,r.strictModeConstructorInjection=this.strictModeConstructorInjection,r.throwOnMissing=this.throwOnMissing,r},getMappingVo:function(t){return this.mappings?this.mappings[t]?this.mappings[t]:this.parent?this.parent.getMappingVo(t):null:null},mapValue:function(r,n){if(this.mappings[r])throw new Error(t.errors.MAPPING_ALREADY_EXISTS+r);return a(r),function(r,n){if(null==n)throw new Error(t.errors.MAPPING_BAD_VALUE+r)}(r,n),this.mappings[r]=new s(r,n,void 0,void 0),this},mapClass:function(r,n,e){if(this.mappings[r])throw new Error(t.errors.MAPPING_ALREADY_EXISTS+r);return a(r),function(r,n){if("function"!=typeof n)throw new Error(t.errors.MAPPING_BAD_CLASS+r)}(r,n),e&&function(r,n){if("boolean"!=typeof n)throw new Error(t.errors.MAPPING_BAD_SINGLETON+r)}(r,e),this.mappings[r]=new s(r,null,n,e),this},removeMapping:function(t){return this.mappings[t]=null,delete this.mappings[t],this},hasMapping:function(t){return!!this.mappings[t]},hasInheritedMapping:function(t){return!!this.getMappingVo(t)},getMapping:function(t){for(var r in this.mappings)if(this.mappings.hasOwnProperty(r)){var n=this.mappings[r];if(n.value===t||n.cl===t)return n.prop}},getValue:function(t){var r=this.mappings[t];if(!r){if(!this.parent){if(this.throwOnMissing)throw new Error(c(t));return}r=this.parent.getMappingVo.apply(this.parent,arguments)}if(r.cl){var n=Array.prototype.slice.call(arguments);return n[0]=r.cl,r.singleton?(r.value||(r.value=this.createInstance.apply(this,n)),r.value):this.createInstance.apply(this,n)}return r.value},getClass:function(t){var r=this.mappings[t];if(!r){if(!this.parent)return;r=this.parent.getMappingVo.apply(this.parent,arguments)}if(r.cl)return r.cl},instantiate:function(r){if("function"!=typeof r)throw new Error(t.errors.CREATE_INSTANCE_INVALID_PARAM);var n=t.getDependencies(r);if(this.strictMode&&!r.hasOwnProperty("inject"))throw new Error(t.errors.DEPENDENCIES_MISSING_IN_STRICT_MODE);if(this.strictModeConstructorInjection&&n.length>0&&!r.hasOwnProperty("inject"))throw new Error(t.errors.DEPENDENCIES_MISSING_IN_STRICT_MODE_CONSTRUCTOR_INJECTION);for(var e=[null],i=0,o=n.length;i<o;i++)if(arguments.length>i+1&&void 0!==arguments[i+1]&&null!==arguments[i+1])e.push(arguments[i+1]);else{var s=n[i],a=this.getMappingVo(s);if(a){var p=this.getInjectedValue(a,s);e.push(p)}else{if(this.throwOnMissing)throw new Error(c(s,r.name));e.push(void 0)}}return new(Function.prototype.bind.apply(r,e))},inject:function(t,r){for(var n in this.parent&&this.parent.inject(t,!0),this.mappings)if(this.mappings.hasOwnProperty(n)){var e=this.getMappingVo(n);(t.hasOwnProperty(e.prop)||t.constructor&&t.constructor.prototype&&t.constructor.prototype.hasOwnProperty(e.prop))&&(t[n]=this.getInjectedValue(e,n))}return"function"!=typeof t.postConstruct||r||t.postConstruct(),this},getInjectedValue:function(r,n){var e,i=r.value;return r.cl&&(r.singleton?(r.value||(p(n,r.cl),r.value=this.instantiate(r.cl),e=r.value),i=r.value):(p(n,r.cl),e=i=this.instantiate(r.cl))),e&&(!function(r,n){if(n.hasOwnProperty(r))throw new Error(t.errors.INJECT_INSTANCE_IN_ITSELF_PROPERTY)}(n,e),this.inject(e)),i},createInstance:function(){var t=this.instantiate.apply(this,arguments);return this.inject(t),t},getValueFromClass:function(t){for(var r in this.mappings)if(this.mappings.hasOwnProperty(r)){var n=this.mappings[r];if(n.cl===t)return n.singleton?(n.value||(n.value=this.createInstance.apply(this,arguments)),n.value):this.createInstance.apply(this,arguments)}if(this.parent)return this.parent.getValueFromClass.apply(this.parent,arguments);if(this.throwOnMissing)throw new Error(c(void 0,t.name))},dispose:function(){this.mappings={}}},Function.prototype.bind||(Function.prototype.bind=function(t){var r=this;if("function"!=typeof r)throw new Error("Error, you must bind a function.");var n=Array.prototype.slice.call(arguments,1),e=function(){if(this instanceof e){var i=function(){};i.prototype=r.prototype;var o=new i,s=r.apply(o,n.concat(Array.prototype.slice.call(arguments)));return Object(s)===s?s:o}return r.apply(t,n.concat(Array.prototype.slice.call(arguments)))};return e}),"function"==typeof define&&void 0!==define.amd&&define(t),"undefined"!=typeof module&&void 0!==module.exports&&(module.exports=t),"undefined"!=typeof exports&&(exports=t)}(this.infuse=this.infuse||{});