-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinjector-js.min.js
23 lines (20 loc) · 4.5 KB
/
injector-js.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:</p>
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
! injector-js - v0.2.1 - 2013-12-08
* Copyright (c) 2013 Bigger Boat;
*/
var injector={};injector.InjectionMapping=function(t,n,i){this._type=t,this._name=n,this._id=i,this._value=null,this._toType=null,this._isValid=function(){return null!=this._value||null!=this._toType},this._validateBeforeCreation=function(){if(this._isValid())throw Error("Could not create mapping for "+this._id+" because it already has been defined");return!0}},injector.InjectionMapping.prototype={toValue:function(t){this._validateBeforeCreation()&&(this._value=t)},toType:function(t){this._validateBeforeCreation()&&(this._toType=t)},toSingleton:function(t){this.toValue(new t)},getValue:function(){if(!this._isValid())throw Error("Could not get value for "+this._id+" because the mapping is invalid");return null!=this._value?this._value:null!=this._toType?new this._toType:void 0}},injector.Injector=function(t){this._mappings={},this._parentInjector=null,this._createMapping=function(t,n,i){if(this._hasOwnMapping(t,n))throw Error("Already has mapping for "+t);var e=new injector.InjectionMapping(t,n,i);return this._mappings[i]=e,e},this._getMappingID=function(t,n){return n=void 0==n?"":n,t+"|"+n},this._hasOwnMapping=function(t,n){var i=this._getMappingID(t,n);return void 0!==this._mappings[i]},this._postConstruct=function(t){var n,i,e,r=void 0!==t.postConstructs?t.postConstructs instanceof Array?t.postConstructs:[]:[];for(n in r)i=r[n],e=void 0===t[i]?null:t[i],"function"==typeof e&&e.apply(t)},this.map("injector").toValue(this),this._parentInjector=t||null},injector.Injector.prototype={map:function(t,n){var i=this._getMappingID(t,n);return this._mappings[i]||this._createMapping(t,n,i)},unmap:function(t,n){if(!this.hasMapping(t,n)){var i=void 0==n?"":" by name "+n;throw Error('Cannot unmap "'+t+i+'" because no mapping has been found')}var e=this._getMappingID(t,n);delete this._mappings[e]},hasMapping:function(t,n){return this._hasOwnMapping(t,n)||null!==this._parentInjector&&this._parentInjector.hasMapping(t,n)},hasDirectMapping:function(t,n){return this._hasOwnMapping(t,n)},getInstance:function(t,n){if(this.hasMapping(t,n))return this.getMapping(t,n).getValue();var i=void 0==n?"":" by name "+n;throw Error('Cannot return instance "'+t+i+'" because no mapping has been found')},getMapping:function(t,n){if(this.hasMapping(t,n)){var i=this._getMappingID(t,n);return void 0!==this._mappings[i]?this._mappings[i]:this.getParentInjector().getMapping(t,n)}var e=void 0==n?"":" by name "+n;throw Error('Mapping "'+t+e+'" was not found')},injectInto:function(t){var n,i;for(n in t)if(i=injector.utils.stringToObject(n,t[n]),null!=i){if(!this.hasMapping(i.type,i.name))throw Error("Cannot inject "+i.type+" into "+t+" due to a missing rule");t[n]=this.getInstance(i.type,i.name)}this._postConstruct(t)},teardown:function(){this._mappings={},this.map("injector").toValue(this)},getParentInjector:function(){return this._parentInjector},setParentInjector:function(t){if(null!=t&&!(t instanceof injector.Injector))throw Error("Cannot set the parentInjector because it is not an injector");this._parentInjector=t},createChildInjector:function(){return new injector.Injector(this)}},injector.utils={},injector.utils.stringToObject=function(t,n){if("string"!=typeof n)return null;var i,e,r={name:"",type:t},a=RegExp("^inject"),o=RegExp(/[\w:\-]?name[\s]*?=[\s]*?("[^"]+"|'[^']+'|\w+)/),s=RegExp(":[^:]+$");return"inject"==n?r:n.match(a)?(i=o.exec(n),e=s.exec(n),r.name=null!=i&&2==i.length?i[1].replace(/"/gm,""):"",r.type=null!=e&&1==e.length?e[0].replace(":",""):t,r):null};