Comparing version 1.7.0 to 1.7.1
;(function(undefined) { | ||
'use strict'; | ||
/** | ||
* BottleJS v1.7.0 - 2018-01-29 | ||
* BottleJS v1.7.1 - 2018-05-03 | ||
* A powerful dependency injection micro container | ||
@@ -10,2 +10,4 @@ * | ||
*/ | ||
var Bottle; | ||
/** | ||
@@ -47,3 +49,3 @@ * String constants | ||
var service = obj[prop]; | ||
if (service === undefined && globalConfig.strict) { | ||
if (service === undefined && Bottle.config.strict) { | ||
throw new Error('Bottle was unable to resolve a service. `' + prop + '` is undefined.'); | ||
@@ -83,120 +85,2 @@ } | ||
/** | ||
* Register a constant | ||
* | ||
* @param String name | ||
* @param mixed value | ||
* @return Bottle | ||
*/ | ||
var constant = function constant(name, value) { | ||
var parts = name.split(DELIMITER); | ||
name = parts.pop(); | ||
defineConstant.call(parts.reduce(setValueObject, this.container), name, value); | ||
return this; | ||
}; | ||
var defineConstant = function defineConstant(name, value) { | ||
Object.defineProperty(this, name, { | ||
configurable : false, | ||
enumerable : true, | ||
value : value, | ||
writable : false | ||
}); | ||
}; | ||
/** | ||
* Register decorator. | ||
* | ||
* @param String fullname | ||
* @param Function func | ||
* @return Bottle | ||
*/ | ||
var decorator = function decorator(fullname, func) { | ||
var parts, name; | ||
if (typeof fullname === FUNCTION_TYPE) { | ||
func = fullname; | ||
fullname = GLOBAL_NAME; | ||
} | ||
parts = fullname.split(DELIMITER); | ||
name = parts.shift(); | ||
if (parts.length) { | ||
getNestedBottle.call(this, name).decorator(parts.join(DELIMITER), func); | ||
} else { | ||
if (!this.decorators[name]) { | ||
this.decorators[name] = []; | ||
} | ||
this.decorators[name].push(func); | ||
} | ||
return this; | ||
}; | ||
/** | ||
* Register a function that will be executed when Bottle#resolve is called. | ||
* | ||
* @param Function func | ||
* @return Bottle | ||
*/ | ||
var defer = function defer(func) { | ||
this.deferred.push(func); | ||
return this; | ||
}; | ||
/** | ||
* Immediately instantiates the provided list of services and returns them. | ||
* | ||
* @param Array services | ||
* @return Array Array of instances (in the order they were provided) | ||
*/ | ||
var digest = function digest(services) { | ||
return (services || []).map(getNestedService, this.container); | ||
}; | ||
/** | ||
* Register a factory inside a generic provider. | ||
* | ||
* @param String name | ||
* @param Function Factory | ||
* @return Bottle | ||
*/ | ||
var factory = function factory(name, Factory) { | ||
return provider.call(this, name, function GenericProvider() { | ||
this.$get = Factory; | ||
}); | ||
}; | ||
/** | ||
* Register an instance factory inside a generic factory. | ||
* | ||
* @param {String} name - The name of the service | ||
* @param {Function} Factory - The factory function, matches the signature required for the | ||
* `factory` method | ||
* @return Bottle | ||
*/ | ||
var instanceFactory = function instanceFactory(name, Factory) { | ||
return factory.call(this, name, function GenericInstanceFactory(container) { | ||
return { | ||
instance : Factory.bind(Factory, container) | ||
}; | ||
}); | ||
}; | ||
/** | ||
* A filter function for removing bottle container methods and providers from a list of keys | ||
*/ | ||
var byMethod = function byMethod(name) { | ||
return !/^\$(?:decorator|register|list)$|Provider$/.test(name); | ||
}; | ||
/** | ||
* List the services registered on the container. | ||
* | ||
* @param Object container | ||
* @return Array | ||
*/ | ||
var list = function list(container) { | ||
return Object.keys(container || this.container || {}).filter(byMethod); | ||
}; | ||
/** | ||
* Function used by provider to set up middleware for each request. | ||
@@ -267,42 +151,2 @@ * | ||
/** | ||
* Named bottle instances | ||
* | ||
* @type Object | ||
*/ | ||
var bottles = {}; | ||
/** | ||
* Get an instance of bottle. | ||
* | ||
* If a name is provided the instance will be stored in a local hash. Calling Bottle.pop multiple | ||
* times with the same name will return the same instance. | ||
* | ||
* @param String name | ||
* @return Bottle | ||
*/ | ||
var pop = function pop(name) { | ||
var instance; | ||
if (typeof name === STRING_TYPE) { | ||
instance = bottles[name]; | ||
if (!instance) { | ||
bottles[name] = instance = new Bottle(); | ||
instance.constant('BOTTLE_NAME', name); | ||
} | ||
return instance; | ||
} | ||
return new Bottle(); | ||
}; | ||
/** | ||
* Clear all named bottles. | ||
*/ | ||
var clear = function clear(name) { | ||
if (typeof name === STRING_TYPE) { | ||
delete bottles[name]; | ||
} else { | ||
bottles = {}; | ||
} | ||
}; | ||
/** | ||
* Used to process decorators in the provider | ||
@@ -318,27 +162,3 @@ * | ||
/** | ||
* Register a provider. | ||
* | ||
* @param String fullname | ||
* @param Function Provider | ||
* @return Bottle | ||
*/ | ||
var provider = function provider(fullname, Provider) { | ||
var parts, name; | ||
parts = fullname.split(DELIMITER); | ||
if (this.providerMap[fullname] && parts.length === 1 && !this.container[fullname + PROVIDER_SUFFIX]) { | ||
return console.error(fullname + ' provider already instantiated.'); | ||
} | ||
this.originalProviders[fullname] = Provider; | ||
this.providerMap[fullname] = true; | ||
name = parts.shift(); | ||
if (parts.length) { | ||
getNestedBottle.call(this, name).provider(parts.join(DELIMITER), Provider); | ||
return this; | ||
} | ||
return createProvider.call(this, name, Provider); | ||
}; | ||
/** | ||
@@ -353,2 +173,3 @@ * Get decorators and middleware including globals | ||
/** | ||
@@ -404,64 +225,71 @@ * Create the provider properties on the container | ||
/** | ||
* Register a service, factory, provider, or value based on properties on the object. | ||
* Register a provider. | ||
* | ||
* properties: | ||
* * Obj.$name String required ex: `'Thing'` | ||
* * Obj.$type String optional 'service', 'factory', 'provider', 'value'. Default: 'service' | ||
* * Obj.$inject Mixed optional only useful with $type 'service' name or array of names | ||
* * Obj.$value Mixed optional Normally Obj is registered on the container. However, if this | ||
* property is included, it's value will be registered on the container | ||
* instead of the object itsself. Useful for registering objects on the | ||
* bottle container without modifying those objects with bottle specific keys. | ||
* | ||
* @param Function Obj | ||
* @param String fullname | ||
* @param Function Provider | ||
* @return Bottle | ||
*/ | ||
var register = function register(Obj) { | ||
var value = Obj.$value === undefined ? Obj : Obj.$value; | ||
return this[Obj.$type || 'service'].apply(this, [Obj.$name, value].concat(Obj.$inject || [])); | ||
var provider = function provider(fullname, Provider) { | ||
var parts, name; | ||
parts = fullname.split(DELIMITER); | ||
if (this.providerMap[fullname] && parts.length === 1 && !this.container[fullname + PROVIDER_SUFFIX]) { | ||
return console.error(fullname + ' provider already instantiated.'); | ||
} | ||
this.originalProviders[fullname] = Provider; | ||
this.providerMap[fullname] = true; | ||
name = parts.shift(); | ||
if (parts.length) { | ||
getNestedBottle.call(this, name).provider(parts.join(DELIMITER), Provider); | ||
return this; | ||
} | ||
return createProvider.call(this, name, Provider); | ||
}; | ||
/** | ||
* Deletes providers from the map and container. | ||
* Register a factory inside a generic provider. | ||
* | ||
* @param String name | ||
* @return void | ||
* @param Function Factory | ||
* @return Bottle | ||
*/ | ||
var removeProviderMap = function resetProvider(name) { | ||
delete this.providerMap[name]; | ||
delete this.container[name]; | ||
delete this.container[name + PROVIDER_SUFFIX]; | ||
var factory = function factory(name, Factory) { | ||
return provider.call(this, name, function GenericProvider() { | ||
this.$get = Factory; | ||
}); | ||
}; | ||
/** | ||
* Resets all providers on a bottle instance. | ||
* Private helper for creating service and service factories. | ||
* | ||
* @return void | ||
* @param String name | ||
* @param Function Service | ||
* @return Bottle | ||
*/ | ||
var resetProviders = function resetProviders() { | ||
var providers = this.originalProviders; | ||
Object.keys(this.originalProviders).forEach(function resetPrvider(provider) { | ||
var parts = provider.split(DELIMITER); | ||
if (parts.length > 1) { | ||
parts.forEach(removeProviderMap, getNestedBottle.call(this, parts[0])); | ||
var createService = function createService(name, Service, isClass) { | ||
var deps = arguments.length > 3 ? slice.call(arguments, 3) : []; | ||
var bottle = this; | ||
return factory.call(this, name, function GenericFactory() { | ||
var serviceFactory = Service; // alias for jshint | ||
var args = deps.map(getNestedService, bottle.container); | ||
if (!isClass) { | ||
return serviceFactory.apply(null, args); | ||
} | ||
removeProviderMap.call(this, provider); | ||
this.provider(provider, providers[provider]); | ||
}, this); | ||
return new (Service.bind.apply(Service, [null].concat(args)))(); | ||
}); | ||
}; | ||
/** | ||
* Execute any deferred functions | ||
* Register a class service | ||
* | ||
* @param Mixed data | ||
* @param String name | ||
* @param Function Service | ||
* @return Bottle | ||
*/ | ||
var resolve = function resolve(data) { | ||
this.deferred.forEach(function deferredIterator(func) { | ||
func(data); | ||
}); | ||
return this; | ||
var service = function service(name, Service) { | ||
return createService.apply(this, [name, Service, true].concat(slice.call(arguments, 2))); | ||
}; | ||
@@ -477,33 +305,34 @@ | ||
/** | ||
* Register a class service | ||
* Define a mutable property on the container. | ||
* | ||
* @param String name | ||
* @param Function Service | ||
* @return Bottle | ||
* @param mixed val | ||
* @return void | ||
* @scope container | ||
*/ | ||
var service = function service(name, Service) { | ||
return createService.apply(this, [name, Service, true].concat(slice.call(arguments, 2))); | ||
var defineValue = function defineValue(name, val) { | ||
Object.defineProperty(this, name, { | ||
configurable : true, | ||
enumerable : true, | ||
value : val, | ||
writable : true | ||
}); | ||
}; | ||
/** | ||
* Private helper for creating service and service factories. | ||
* Iterator for setting a plain object literal via defineValue | ||
* | ||
* @param String name | ||
* @param Function Service | ||
* @return Bottle | ||
* @param Object container | ||
* @param string name | ||
*/ | ||
var createService = function createService(name, Service, isClass) { | ||
var deps = arguments.length > 3 ? slice.call(arguments, 3) : []; | ||
var bottle = this; | ||
return factory.call(this, name, function GenericFactory() { | ||
var serviceFactory = Service; // alias for jshint | ||
var args = deps.map(getNestedService, bottle.container); | ||
if (!isClass) { | ||
return serviceFactory.apply(null, args); | ||
} | ||
return new (Service.bind.apply(Service, [null].concat(args)))(); | ||
}); | ||
var setValueObject = function setValueObject(container, name) { | ||
var nestedContainer = container[name]; | ||
if (!nestedContainer) { | ||
nestedContainer = {}; | ||
defineValue.call(container, name, nestedContainer); | ||
} | ||
return nestedContainer; | ||
}; | ||
/** | ||
@@ -525,31 +354,221 @@ * Register a value | ||
/** | ||
* Iterator for setting a plain object literal via defineValue | ||
* Define an enumerable, non-configurable, non-writable value. | ||
* | ||
* @param String name | ||
* @param mixed value | ||
* @return undefined | ||
*/ | ||
var defineConstant = function defineConstant(name, value) { | ||
Object.defineProperty(this, name, { | ||
configurable : false, | ||
enumerable : true, | ||
value : value, | ||
writable : false | ||
}); | ||
}; | ||
/** | ||
* Register a constant | ||
* | ||
* @param String name | ||
* @param mixed value | ||
* @return Bottle | ||
*/ | ||
var constant = function constant(name, value) { | ||
var parts = name.split(DELIMITER); | ||
name = parts.pop(); | ||
defineConstant.call(parts.reduce(setValueObject, this.container), name, value); | ||
return this; | ||
}; | ||
/** | ||
* Register decorator. | ||
* | ||
* @param String fullname | ||
* @param Function func | ||
* @return Bottle | ||
*/ | ||
var decorator = function decorator(fullname, func) { | ||
var parts, name; | ||
if (typeof fullname === FUNCTION_TYPE) { | ||
func = fullname; | ||
fullname = GLOBAL_NAME; | ||
} | ||
parts = fullname.split(DELIMITER); | ||
name = parts.shift(); | ||
if (parts.length) { | ||
getNestedBottle.call(this, name).decorator(parts.join(DELIMITER), func); | ||
} else { | ||
if (!this.decorators[name]) { | ||
this.decorators[name] = []; | ||
} | ||
this.decorators[name].push(func); | ||
} | ||
return this; | ||
}; | ||
/** | ||
* Register a function that will be executed when Bottle#resolve is called. | ||
* | ||
* @param Function func | ||
* @return Bottle | ||
*/ | ||
var defer = function defer(func) { | ||
this.deferred.push(func); | ||
return this; | ||
}; | ||
/** | ||
* Immediately instantiates the provided list of services and returns them. | ||
* | ||
* @param Array services | ||
* @return Array Array of instances (in the order they were provided) | ||
*/ | ||
var digest = function digest(services) { | ||
return (services || []).map(getNestedService, this.container); | ||
}; | ||
/** | ||
* Register an instance factory inside a generic factory. | ||
* | ||
* @param {String} name - The name of the service | ||
* @param {Function} Factory - The factory function, matches the signature required for the | ||
* `factory` method | ||
* @return Bottle | ||
*/ | ||
var instanceFactory = function instanceFactory(name, Factory) { | ||
return factory.call(this, name, function GenericInstanceFactory(container) { | ||
return { | ||
instance : Factory.bind(Factory, container) | ||
}; | ||
}); | ||
}; | ||
/** | ||
* A filter function for removing bottle container methods and providers from a list of keys | ||
*/ | ||
var byMethod = function byMethod(name) { | ||
return !/^\$(?:decorator|register|list)$|Provider$/.test(name); | ||
}; | ||
/** | ||
* List the services registered on the container. | ||
* | ||
* @param Object container | ||
* @param string name | ||
* @return Array | ||
*/ | ||
var setValueObject = function setValueObject(container, name) { | ||
var nestedContainer = container[name]; | ||
if (!nestedContainer) { | ||
nestedContainer = {}; | ||
defineValue.call(container, name, nestedContainer); | ||
var list = function list(container) { | ||
return Object.keys(container || this.container || {}).filter(byMethod); | ||
}; | ||
/** | ||
* Named bottle instances | ||
* | ||
* @type Object | ||
*/ | ||
var bottles = {}; | ||
/** | ||
* Get an instance of bottle. | ||
* | ||
* If a name is provided the instance will be stored in a local hash. Calling Bottle.pop multiple | ||
* times with the same name will return the same instance. | ||
* | ||
* @param String name | ||
* @return Bottle | ||
*/ | ||
var pop = function pop(name) { | ||
var instance; | ||
if (typeof name === STRING_TYPE) { | ||
instance = bottles[name]; | ||
if (!instance) { | ||
bottles[name] = instance = new Bottle(); | ||
instance.constant('BOTTLE_NAME', name); | ||
} | ||
return instance; | ||
} | ||
return nestedContainer; | ||
return new Bottle(); | ||
}; | ||
/** | ||
* Define a mutable property on the container. | ||
* Clear all named bottles. | ||
*/ | ||
var clear = function clear(name) { | ||
if (typeof name === STRING_TYPE) { | ||
delete bottles[name]; | ||
} else { | ||
bottles = {}; | ||
} | ||
}; | ||
/** | ||
* Register a service, factory, provider, or value based on properties on the object. | ||
* | ||
* properties: | ||
* * Obj.$name String required ex: `'Thing'` | ||
* * Obj.$type String optional 'service', 'factory', 'provider', 'value'. Default: 'service' | ||
* * Obj.$inject Mixed optional only useful with $type 'service' name or array of names | ||
* * Obj.$value Mixed optional Normally Obj is registered on the container. However, if this | ||
* property is included, it's value will be registered on the container | ||
* instead of the object itsself. Useful for registering objects on the | ||
* bottle container without modifying those objects with bottle specific keys. | ||
* | ||
* @param Function Obj | ||
* @return Bottle | ||
*/ | ||
var register = function register(Obj) { | ||
var value = Obj.$value === undefined ? Obj : Obj.$value; | ||
return this[Obj.$type || 'service'].apply(this, [Obj.$name, value].concat(Obj.$inject || [])); | ||
}; | ||
/** | ||
* Deletes providers from the map and container. | ||
* | ||
* @param String name | ||
* @param mixed val | ||
* @return void | ||
* @scope container | ||
*/ | ||
var defineValue = function defineValue(name, val) { | ||
Object.defineProperty(this, name, { | ||
configurable : true, | ||
enumerable : true, | ||
value : val, | ||
writable : true | ||
var removeProviderMap = function resetProvider(name) { | ||
delete this.providerMap[name]; | ||
delete this.container[name]; | ||
delete this.container[name + PROVIDER_SUFFIX]; | ||
}; | ||
/** | ||
* Resets providers on a bottle instance. If 'names' array is provided, only the named providers will be reset. | ||
* | ||
* @param Array names | ||
* @return void | ||
*/ | ||
var resetProviders = function resetProviders(names) { | ||
var tempProviders = this.originalProviders; | ||
var shouldFilter = Array.isArray(names); | ||
Object.keys(this.originalProviders).forEach(function resetProvider(originalProviderName) { | ||
if (shouldFilter && names.indexOf(originalProviderName) === -1) { | ||
return; | ||
} | ||
var parts = originalProviderName.split(DELIMITER); | ||
if (parts.length > 1) { | ||
parts.forEach(removeProviderMap, getNestedBottle.call(this, parts[0])); | ||
} | ||
removeProviderMap.call(this, originalProviderName); | ||
this.provider(originalProviderName, tempProviders[originalProviderName]); | ||
}, this); | ||
}; | ||
/** | ||
* Execute any deferred functions | ||
* | ||
* @param Mixed data | ||
* @return Bottle | ||
*/ | ||
var resolve = function resolve(data) { | ||
this.deferred.forEach(function deferredIterator(func) { | ||
func(data); | ||
}); | ||
return this; | ||
}; | ||
@@ -563,3 +582,3 @@ | ||
*/ | ||
var Bottle = function Bottle(name) { | ||
Bottle = function Bottle(name) { | ||
if (!(this instanceof Bottle)) { | ||
@@ -615,3 +634,3 @@ return Bottle.pop(name); | ||
*/ | ||
var globalConfig = Bottle.config = { | ||
Bottle.config = { | ||
strict : false | ||
@@ -618,0 +637,0 @@ }; |
{ | ||
"name": "bottlejs", | ||
"version": "1.7.0", | ||
"version": "1.7.1", | ||
"description": "A powerful dependency injection micro container", | ||
@@ -29,11 +29,11 @@ "main": "dist/bottle.js", | ||
"devDependencies": { | ||
"grunt": "^0.4.5", | ||
"grunt-contrib-clean": "^0.6.0", | ||
"grunt-contrib-concat": "^0.5.0", | ||
"grunt-contrib-jasmine": "^1.0.3", | ||
"grunt-contrib-jshint": "^0.10.0", | ||
"grunt-contrib-uglify": "^0.5.1", | ||
"grunt-wrap": "^0.3.0", | ||
"lodash": "^2.4.1" | ||
"grunt": "^1.0.2", | ||
"grunt-contrib-clean": "^1.1.0", | ||
"grunt-contrib-concat": "^1.0.1", | ||
"grunt-contrib-jasmine": "^1.2.0", | ||
"grunt-contrib-jshint": "^1.1.0", | ||
"grunt-contrib-uglify": "^3.3.0", | ||
"grunt-wrap": "^0.3.1", | ||
"lodash": "^4.17.0" | ||
} | ||
} |
@@ -385,5 +385,8 @@ | ||
#### resetProviders() | ||
#### resetProviders(names) | ||
Param | Type | Details | ||
:--------------------------|:-----------|:-------- | ||
**names**<br />*(optional)*| *Array* | An array of strings which contains names of the providers to be reset. | ||
Used to reset all containers for the next reference to reinstantiate the provider. | ||
Used to reset providers for the next reference to re-instantiate the provider. If `names` param is passed, will reset only the named providers. | ||
@@ -390,0 +393,0 @@ #### register(Obj) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46803
715
457