blister
Advanced tools
Comparing version 0.5.0 to 0.9.0
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Blister = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
'use strict'; | ||
/** | ||
* Wrapper functions to store the different types of dependencies in the | ||
* container | ||
* @private | ||
* @type {Object} | ||
*/ | ||
var wrappers = { | ||
var wrappers = require('./wrappers'); | ||
/** | ||
* Returns a wrapper for a VALUE dependency to be stored in the container | ||
* @param {*} value | ||
* @return {Function} | ||
*/ | ||
VALUE: function wrapValue(value) { | ||
return function() { | ||
return value; | ||
}; | ||
}, | ||
/** | ||
* Returns a wrapper for a FACTORY dependency to be stored in the container | ||
* @param {Function} factory The factory function | ||
* @param {Blister} container The container to use as argument and context | ||
* for the factory | ||
*/ | ||
FACTORY: function wrapFactory(factory, container) { | ||
return factory.bind(container, container); | ||
}, | ||
/** | ||
* Returns a wrapper for a SINGLETON dependency to be stored in the container | ||
* @param {Function} singletonFactory The singleton generator function | ||
* @param {Blister} container The container to use as argument and context | ||
* for the factory | ||
*/ | ||
SINGLETON: function wrapSingleton(singletonFactory, container) { | ||
var cached = false; | ||
var cachedValue; | ||
return function() { | ||
if (!cached) { | ||
cached = true; | ||
cachedValue = singletonFactory.call(container, container); | ||
singletonFactory = null; | ||
} | ||
return cachedValue; | ||
}; | ||
} | ||
}; | ||
/** | ||
* @name DependencyType | ||
* @description Possible dependency types to register in a Blister container. | ||
* These constants are available as properties of all Blister | ||
* instances. | ||
* @name BlisterDependencyType | ||
* @enum {string} | ||
* | ||
* @property {string} VALUE | ||
* @property {string} SINGLETON | ||
* @property {string} FACTORY | ||
* @property {string} SINGLETON | ||
*/ | ||
var VALUE = 'VALUE'; | ||
var SINGLETON = 'SINGLETON'; | ||
var FACTORY = 'FACTORY'; | ||
/** | ||
@@ -69,3 +23,3 @@ * Dependency injection container constructor | ||
* @example | ||
* var container = new Blister(); | ||
* var container = new BlisterContainer(); | ||
* container.set('id', 'value'); | ||
@@ -76,9 +30,9 @@ * container.get('id'); //> 'value'; | ||
*/ | ||
function Blister() { | ||
function BlisterContainer() { | ||
this._deps = {}; | ||
} | ||
Blister.prototype = { | ||
BlisterContainer.prototype = { | ||
constructor: Blister, | ||
constructor: BlisterContainer, | ||
@@ -92,3 +46,3 @@ /** | ||
*/ | ||
VALUE: 'VALUE', | ||
VALUE: VALUE, | ||
@@ -101,3 +55,3 @@ /** | ||
*/ | ||
SINGLETON: 'SINGLETON', | ||
SINGLETON: SINGLETON, | ||
@@ -109,3 +63,3 @@ /** | ||
*/ | ||
FACTORY: 'FACTORY', | ||
FACTORY: FACTORY, | ||
@@ -120,3 +74,3 @@ /** | ||
var wrapper = this._deps[id]; | ||
return wrapper && wrapper(); | ||
return wrapper && wrapper(this); | ||
}, | ||
@@ -139,7 +93,44 @@ | ||
* @param {*|Function} [value] The dependency definition | ||
* @param {DependencyType} [type] VALUE, SINGLETON or FACTORY properties of a | ||
* Blister instance | ||
* @return {Blister} The container itself | ||
* @param {BlisterDependencyType} [type] VALUE, SINGLETON or FACTORY | ||
* properties | ||
* @return {BlisterContainer} The container itself | ||
*/ | ||
set: function(id, value, type) { | ||
return this._set(id, value, type); | ||
}, | ||
/** | ||
* Extends the specified dependency in the container with the given type. | ||
* | ||
* If no type is passed, inherits the original type if it is a function or | ||
* it is defined as VALUE otherwise. | ||
* | ||
* @param {string} id The dependency id | ||
* @param {*|Function} [value] The dependency definition | ||
* @param {BlisterDependencyType} [type] VALUE, SINGLETON or FACTORY | ||
* properties | ||
* @return {BlisterContainer} The container itself | ||
*/ | ||
extend: function(id, value, type) { | ||
if (!this._deps[id]) { | ||
throw new Error('Cannot extend a dependency not previously set: ' + id); | ||
} | ||
return this._set(id, value, type, true); | ||
}, | ||
/** | ||
* Internal dependency setter that adds extension support | ||
* | ||
* @private | ||
* @param {string} id The dependency id | ||
* @param {*|Function} [value] The dependency definition | ||
* @param {BlisterDependencyType} [type] VALUE, SINGLETON or FACTORY | ||
* properties | ||
* @param {boolean} isExtension Determines if extends a previous dependency, | ||
* so the original value is stored and passed to | ||
* the new definition | ||
* @return {BlisterContainer} The container itself | ||
*/ | ||
_set: function(id, value, type, isExtension) { | ||
if (typeof id !== 'string') { | ||
@@ -149,12 +140,22 @@ throw new TypeError('The dependency id must be a string: ' + id); | ||
var originalWrapper = isExtension ? this._deps[id] : undefined; | ||
var typeOfValue = typeof value; | ||
if (!type) { | ||
type = (typeOfValue === 'function') ? this.SINGLETON : this.VALUE; | ||
if (typeOfValue !== 'function') { | ||
type = VALUE; | ||
} else if (isExtension) { | ||
type = originalWrapper.type; | ||
} else { | ||
type = SINGLETON; | ||
} | ||
} | ||
if (typeOfValue !== 'function' && type !== this.VALUE) { | ||
throw new TypeError('The value must be a function for types SINGLETON and FACTORY: ' + value); | ||
if (typeOfValue !== 'function' && type !== VALUE) { | ||
throw new TypeError( | ||
'The value must be a function for types SINGLETON and FACTORY: ' + | ||
value); | ||
} | ||
this._deps[id] = wrappers[type](value, this); | ||
this._deps[id] = wrappers.create(type, value, this, originalWrapper); | ||
return this; | ||
@@ -166,3 +167,3 @@ }, | ||
* @param {BlisterServiceProvider} provider | ||
* @return {Blister} the container itself | ||
* @return {BlisterContainer} the container itself | ||
*/ | ||
@@ -177,3 +178,3 @@ register: function(provider) { | ||
/** | ||
* Interface for service providers to use with Blister instances | ||
* Interface for service providers to use with BlisterContainer instances | ||
* | ||
@@ -192,3 +193,3 @@ * @interface BlisterServiceProvider | ||
* | ||
* var container = new Blister(); | ||
* var container = new BlisterContainer(); | ||
* container.register(provider); | ||
@@ -200,10 +201,87 @@ */ | ||
* @name BlisterServiceProvider#register | ||
* @description Registers an indeterminate number of dependencies in the passed container | ||
* @param {Blister} container | ||
* @description Registers an indeterminate number of dependencies in the passed | ||
* container | ||
* @param {BlisterContainer} container | ||
*/ | ||
module.exports = Blister; | ||
module.exports = BlisterContainer; | ||
},{"./wrappers":2}],2:[function(require,module,exports){ | ||
'use strict'; | ||
/** | ||
* Wrapper functions to store the different types of dependencies in the | ||
* container | ||
* @private | ||
* @type {Object} | ||
*/ | ||
var wrappers = { | ||
/** | ||
* Returns a wrapper for a VALUE dependency to be stored in the container | ||
* @param {*} value | ||
* @return {Function} | ||
*/ | ||
VALUE: function wrapValue(value) { | ||
return function() { | ||
return value; | ||
}; | ||
}, | ||
/** | ||
* Returns a wrapper for a FACTORY dependency to be stored in the container | ||
* @param {Function} value The factory function | ||
* @param {BlisterContainer} container | ||
* @param {Function} [originalWrapper] | ||
* @return {Function} | ||
*/ | ||
FACTORY: function wrapFactory(value, container, originalWrapper) { | ||
return function() { | ||
var originalValue = originalWrapper && originalWrapper(); | ||
return value.call(container, container, originalValue); | ||
}; | ||
}, | ||
/** | ||
* Returns a wrapper for a SINGLETON dependency to be stored in the container | ||
* @param {Function} value The singleton generator function | ||
* @param {BlisterContainer} container | ||
* @param {Function} [originalWrapper] | ||
* @return {Function} | ||
*/ | ||
SINGLETON: function wrapSingleton(value, container, originalWrapper) { | ||
var cached = false; | ||
var cachedValue; | ||
return function() { | ||
var originalValue; | ||
if (!cached) { | ||
cached = true; | ||
originalValue = originalWrapper && originalWrapper(); | ||
cachedValue = value.call(container, container, originalValue); | ||
value = null; | ||
} | ||
return cachedValue; | ||
}; | ||
}, | ||
/** | ||
* Returns a wrapper for the given parameters | ||
* @param {BlisterDependencyType} type | ||
* @param {*|Function} value | ||
* @param {BlisterContainer} container | ||
* @param {Function} [originalWrapper] | ||
* @return {Function} | ||
*/ | ||
create: function(type, value, container, originalWrapper) { | ||
var wrapper = this[type](value, container, originalWrapper); | ||
wrapper.type = type; | ||
return wrapper; | ||
} | ||
}; | ||
module.exports = wrappers; | ||
},{}]},{},[1])(1) | ||
}); | ||
//# sourceMappingURL=blister.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var t;t="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,t.Blister=e()}}(function(){return function e(t,n,r){function i(u,f){if(!n[u]){if(!t[u]){var s="function"==typeof require&&require;if(!f&&s)return s(u,!0);if(o)return o(u,!0);var c=new Error("Cannot find module '"+u+"'");throw c.code="MODULE_NOT_FOUND",c}var d=n[u]={exports:{}};t[u][0].call(d.exports,function(e){var n=t[u][1][e];return i(n?n:e)},d,d.exports,e,t,n,r)}return n[u].exports}for(var o="function"==typeof require&&require,u=0;u<r.length;u++)i(r[u]);return i}({1:[function(e,t){"use strict";function n(){this._deps={}}var r={VALUE:function(e){return function(){return e}},FACTORY:function(e,t){return e.bind(t,t)},SINGLETON:function(e,t){var n,r=!1;return function(){return r||(r=!0,n=e.call(t,t),e=null),n}}};n.prototype={constructor:n,VALUE:"VALUE",SINGLETON:"SINGLETON",FACTORY:"FACTORY",get:function(e){var t=this._deps[e];return t&&t()},set:function(e,t,n){if("string"!=typeof e)throw new TypeError("The dependency id must be a string: "+e);var i=typeof t;if(n||(n="function"===i?this.SINGLETON:this.VALUE),"function"!==i&&n!==this.VALUE)throw new TypeError("The value must be a function for types SINGLETON and FACTORY: "+t);return this._deps[e]=r[n](t,this),this},register:function(e){return e.register(this),this}},t.exports=n},{}]},{},[1])(1)}); | ||
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var t;t="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,t.Blister=e()}}(function(){return function e(t,n,r){function i(u,f){if(!n[u]){if(!t[u]){var s="function"==typeof require&&require;if(!f&&s)return s(u,!0);if(o)return o(u,!0);var c=new Error("Cannot find module '"+u+"'");throw c.code="MODULE_NOT_FOUND",c}var p=n[u]={exports:{}};t[u][0].call(p.exports,function(e){var n=t[u][1][e];return i(n?n:e)},p,p.exports,e,t,n,r)}return n[u].exports}for(var o="function"==typeof require&&require,u=0;u<r.length;u++)i(r[u]);return i}({1:[function(e,t){"use strict";function n(){this._deps={}}var r=e("./wrappers"),i="VALUE",o="SINGLETON",u="FACTORY";n.prototype={constructor:n,VALUE:i,SINGLETON:o,FACTORY:u,get:function(e){var t=this._deps[e];return t&&t(this)},set:function(e,t,n){return this._set(e,t,n)},extend:function(e,t,n){if(!this._deps[e])throw new Error("Cannot extend a dependency not previously set: "+e);return this._set(e,t,n,!0)},_set:function(e,t,n,u){if("string"!=typeof e)throw new TypeError("The dependency id must be a string: "+e);var f=u?this._deps[e]:void 0,s=typeof t;if(n||(n="function"!==s?i:u?f.type:o),"function"!==s&&n!==i)throw new TypeError("The value must be a function for types SINGLETON and FACTORY: "+t);return this._deps[e]=r.create(n,t,this,f),this},register:function(e){return e.register(this),this}},t.exports=n},{"./wrappers":2}],2:[function(e,t){"use strict";var n={VALUE:function(e){return function(){return e}},FACTORY:function(e,t,n){return function(){var r=n&&n();return e.call(t,t,r)}},SINGLETON:function(e,t,n){var r,i=!1;return function(){var o;return i||(i=!0,o=n&&n(),r=e.call(t,t,o),e=null),r}},create:function(e,t,n,r){var i=this[e](t,n,r);return i.type=e,i}};t.exports=n},{}]},{},[1])(1)}); | ||
//# sourceMappingURL=blister.min.js.map |
{ | ||
"name": "blister", | ||
"version": "0.5.0", | ||
"version": "0.9.0", | ||
"author": "Rubén Norte <rubennorte@gmail.com>", | ||
@@ -35,3 +35,3 @@ "description": "Minimalist dependency injection container for JavaScript", | ||
"build:js": "npm run browserify:js && npm run uglify:js", | ||
"build": "npm test && npm run build:js", | ||
"build": "npm run build:js", | ||
"browserify:tests": "browserify test/unit/index.js -o test/unit/gen/unit-test-bundle.js --debug", | ||
@@ -44,2 +44,3 @@ "watchify:tests": "watchify test/unit/index.js -o test/unit/gen/unit-test-bundle.js --verbose", | ||
"develop": "parallelshell \"npm run watchify:tests\" \"karma start test/unit/config/karma-dev.conf.js\"", | ||
"prepare-release": "npm test && npm run build", | ||
"release": "mversion -m \"New version: %s\"", | ||
@@ -46,0 +47,0 @@ "doc:js": "jsdoc src --destination doc/js --readme README.md --verbose", |
@@ -90,2 +90,25 @@ # Blister | ||
#### Extending dependencies | ||
Dependencies already defined in the container can be modified or extended. That functionality can be useful, for example, to add plugins to a service. | ||
If both the extension and the original definitions were functions, the extension inherits the type of dependency (SINGLETON or FACTORY) by default. | ||
Example: | ||
```js | ||
container.set('some-service', function() { | ||
return service; | ||
}); | ||
// after that definition | ||
container.extend('some-service', function(c, service) { | ||
service.addLogger(c.get('logger')); | ||
}); | ||
container.get('service'); //> singleton service with logger | ||
``` | ||
If the previous dependency is not used in the definition of the extension, it can be replaced using `set` instead. | ||
#### Registering service providers | ||
@@ -122,3 +145,3 @@ | ||
```bash | ||
npm instal && npm test | ||
npm install && npm test | ||
``` | ||
@@ -125,0 +148,0 @@ |
'use strict'; | ||
/** | ||
* Wrapper functions to store the different types of dependencies in the | ||
* container | ||
* @private | ||
* @type {Object} | ||
*/ | ||
var wrappers = { | ||
var wrappers = require('./wrappers'); | ||
/** | ||
* Returns a wrapper for a VALUE dependency to be stored in the container | ||
* @param {*} value | ||
* @return {Function} | ||
*/ | ||
VALUE: function wrapValue(value) { | ||
return function() { | ||
return value; | ||
}; | ||
}, | ||
/** | ||
* Returns a wrapper for a FACTORY dependency to be stored in the container | ||
* @param {Function} factory The factory function | ||
* @param {Blister} container The container to use as argument and context | ||
* for the factory | ||
*/ | ||
FACTORY: function wrapFactory(factory, container) { | ||
return factory.bind(container, container); | ||
}, | ||
/** | ||
* Returns a wrapper for a SINGLETON dependency to be stored in the container | ||
* @param {Function} singletonFactory The singleton generator function | ||
* @param {Blister} container The container to use as argument and context | ||
* for the factory | ||
*/ | ||
SINGLETON: function wrapSingleton(singletonFactory, container) { | ||
var cached = false; | ||
var cachedValue; | ||
return function() { | ||
if (!cached) { | ||
cached = true; | ||
cachedValue = singletonFactory.call(container, container); | ||
singletonFactory = null; | ||
} | ||
return cachedValue; | ||
}; | ||
} | ||
}; | ||
/** | ||
* @name DependencyType | ||
* @description Possible dependency types to register in a Blister container. | ||
* These constants are available as properties of all Blister | ||
* instances. | ||
* @name BlisterDependencyType | ||
* @enum {string} | ||
* | ||
* @property {string} VALUE | ||
* @property {string} SINGLETON | ||
* @property {string} FACTORY | ||
* @property {string} SINGLETON | ||
*/ | ||
var VALUE = 'VALUE'; | ||
var SINGLETON = 'SINGLETON'; | ||
var FACTORY = 'FACTORY'; | ||
/** | ||
@@ -68,3 +22,3 @@ * Dependency injection container constructor | ||
* @example | ||
* var container = new Blister(); | ||
* var container = new BlisterContainer(); | ||
* container.set('id', 'value'); | ||
@@ -75,9 +29,9 @@ * container.get('id'); //> 'value'; | ||
*/ | ||
function Blister() { | ||
function BlisterContainer() { | ||
this._deps = {}; | ||
} | ||
Blister.prototype = { | ||
BlisterContainer.prototype = { | ||
constructor: Blister, | ||
constructor: BlisterContainer, | ||
@@ -91,3 +45,3 @@ /** | ||
*/ | ||
VALUE: 'VALUE', | ||
VALUE: VALUE, | ||
@@ -100,3 +54,3 @@ /** | ||
*/ | ||
SINGLETON: 'SINGLETON', | ||
SINGLETON: SINGLETON, | ||
@@ -108,3 +62,3 @@ /** | ||
*/ | ||
FACTORY: 'FACTORY', | ||
FACTORY: FACTORY, | ||
@@ -119,3 +73,3 @@ /** | ||
var wrapper = this._deps[id]; | ||
return wrapper && wrapper(); | ||
return wrapper && wrapper(this); | ||
}, | ||
@@ -138,7 +92,44 @@ | ||
* @param {*|Function} [value] The dependency definition | ||
* @param {DependencyType} [type] VALUE, SINGLETON or FACTORY properties of a | ||
* Blister instance | ||
* @return {Blister} The container itself | ||
* @param {BlisterDependencyType} [type] VALUE, SINGLETON or FACTORY | ||
* properties | ||
* @return {BlisterContainer} The container itself | ||
*/ | ||
set: function(id, value, type) { | ||
return this._set(id, value, type); | ||
}, | ||
/** | ||
* Extends the specified dependency in the container with the given type. | ||
* | ||
* If no type is passed, inherits the original type if it is a function or | ||
* it is defined as VALUE otherwise. | ||
* | ||
* @param {string} id The dependency id | ||
* @param {*|Function} [value] The dependency definition | ||
* @param {BlisterDependencyType} [type] VALUE, SINGLETON or FACTORY | ||
* properties | ||
* @return {BlisterContainer} The container itself | ||
*/ | ||
extend: function(id, value, type) { | ||
if (!this._deps[id]) { | ||
throw new Error('Cannot extend a dependency not previously set: ' + id); | ||
} | ||
return this._set(id, value, type, true); | ||
}, | ||
/** | ||
* Internal dependency setter that adds extension support | ||
* | ||
* @private | ||
* @param {string} id The dependency id | ||
* @param {*|Function} [value] The dependency definition | ||
* @param {BlisterDependencyType} [type] VALUE, SINGLETON or FACTORY | ||
* properties | ||
* @param {boolean} isExtension Determines if extends a previous dependency, | ||
* so the original value is stored and passed to | ||
* the new definition | ||
* @return {BlisterContainer} The container itself | ||
*/ | ||
_set: function(id, value, type, isExtension) { | ||
if (typeof id !== 'string') { | ||
@@ -148,12 +139,22 @@ throw new TypeError('The dependency id must be a string: ' + id); | ||
var originalWrapper = isExtension ? this._deps[id] : undefined; | ||
var typeOfValue = typeof value; | ||
if (!type) { | ||
type = (typeOfValue === 'function') ? this.SINGLETON : this.VALUE; | ||
if (typeOfValue !== 'function') { | ||
type = VALUE; | ||
} else if (isExtension) { | ||
type = originalWrapper.type; | ||
} else { | ||
type = SINGLETON; | ||
} | ||
} | ||
if (typeOfValue !== 'function' && type !== this.VALUE) { | ||
throw new TypeError('The value must be a function for types SINGLETON and FACTORY: ' + value); | ||
if (typeOfValue !== 'function' && type !== VALUE) { | ||
throw new TypeError( | ||
'The value must be a function for types SINGLETON and FACTORY: ' + | ||
value); | ||
} | ||
this._deps[id] = wrappers[type](value, this); | ||
this._deps[id] = wrappers.create(type, value, this, originalWrapper); | ||
return this; | ||
@@ -165,3 +166,3 @@ }, | ||
* @param {BlisterServiceProvider} provider | ||
* @return {Blister} the container itself | ||
* @return {BlisterContainer} the container itself | ||
*/ | ||
@@ -176,3 +177,3 @@ register: function(provider) { | ||
/** | ||
* Interface for service providers to use with Blister instances | ||
* Interface for service providers to use with BlisterContainer instances | ||
* | ||
@@ -191,3 +192,3 @@ * @interface BlisterServiceProvider | ||
* | ||
* var container = new Blister(); | ||
* var container = new BlisterContainer(); | ||
* container.register(provider); | ||
@@ -199,6 +200,7 @@ */ | ||
* @name BlisterServiceProvider#register | ||
* @description Registers an indeterminate number of dependencies in the passed container | ||
* @param {Blister} container | ||
* @description Registers an indeterminate number of dependencies in the passed | ||
* container | ||
* @param {BlisterContainer} container | ||
*/ | ||
module.exports = Blister; | ||
module.exports = BlisterContainer; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
48778
10
492
155
3
6
1