Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

injector-js

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

injector-js - npm Package Compare versions

Comparing version 0.2.0 to 0.2.1

6

bower.json
{
"name": "InjectorJS",
"version": "0.2",
"version": "0.2.1",
"ignore": [

@@ -11,4 +11,4 @@ "node_modules",

"dependencies": {
"ClazzJS": "~0.5.1"
"ClazzJS": "~0.5.2"
}
}
}

@@ -46,5 +46,16 @@ ;

/**
* Injector
* Realization of DI container.
*
* @class
*/
clazz('Injector', function(self) {
return {
properties: {
/**
* Supported factories
* @var {object}
*/
factory: {

@@ -54,2 +65,7 @@ type: ['hash'],

},
/**
* Default factory
* @var {AbstractFactory}
*/
defaultFactory: {

@@ -78,2 +94,7 @@ converters: {

},
/**
* Created objects in the container
* @var {object}
*/
_object: {

@@ -83,2 +104,7 @@ type: ['hash'],

},
/**
* Object creators. Used when gets some object.
* @var {object}
*/
_objectCreator: {

@@ -91,2 +117,12 @@ type: ['hash'],

/**
* Sets new object to the container
*
* @param {string|object} name Object name or hash of the objects
* @param {string} factory Factory name
* @param {*} object Object or its factory method
* @returns {Injector} this
*
* @this {Injector}
*/
set: function(name, factory, object) {

@@ -106,2 +142,10 @@

/**
* Checks whether specified object exist
*
* @param {string} name Object name
* @returns {boolean} true if specified object exist
*
* @this {Injector}
*/
has: function(name) {

@@ -111,2 +155,12 @@ return this._hasObject([name]) || this._hasObjectCreator([name]);

/**
* Gets specified object
*
* @param {string} name Object name
* @returns {*} Specified object
*
* @throws {Error} if specified object does not exist
*
* @this {Injector}
*/
get: function(name) {

@@ -121,2 +175,12 @@ this._checkObject(name);

/**
* Removes specified object
*
* @param {string} name Object name
* @returns {Injector} this
*
* @throws {Error} if specified object does not exist
*
* @this {Injector}
*/
remove: function(name) {

@@ -128,14 +192,34 @@ this._checkObject(name);

setFactory: function(fields, value) {
if (_.isUndefined(value)) {
value = fields;
fields = undefined;
/**
* Sets object factory
*
* @param {string,Factory} name Factory name of factory instance
* @param {function|Factory} factory Object factory
* @returns {Injector} this
*
* @this {Injector}
*/
setFactory: function(name, factory) {
if (_.isUndefined(factory)) {
factory = name;
name = undefined;
}
if (value && value.__clazz && value.__clazz.__isSubclazzOf('/InjectorJS/Factories/Abstract')) {
return this.__setPropertyValue(['factory', value.getName()], value);
if (factory && factory.__clazz && factory.__clazz.__isSubclazzOf('/InjectorJS/Factories/Abstract')) {
return this.__setPropertyValue(['factory', factory.getName()], factory);
}
return this.__setPropertyValue(['factory'].concat(_.isString(fields) ? fields.split('.') : fields || []), value);
var fields = _.isString(name) ? name.split('.') : name || [];
return this.__setPropertyValue(['factory'].concat(fields), factory);
},
/**
* Checks whether specified factory exist
*
* @param {string|Factory} factory Object factory or its name
* @returns {boolean} true if object factory exist
*
* @this {Injector}
*/
hasFactory: function(factory) {

@@ -146,2 +230,9 @@ var factoryName = _.isString(factory) ? factory : factory.getName();

/**
* Sets default factory
* @param {string|Factory} factory Default factory or its name
* @returns {Injector} this
*
* @this {Injector}
*/
setDefaultFactory: function(factory) {

@@ -151,2 +242,13 @@ return this.setFactory(factory);

/**
* Checks whether specified object exist
*
* @param {string} name Object name
* @returns {Injector} this
*
* @throws {Error} if specified object does not exist
*
* @this {Injector}
* @private
*/
_checkObject: function(name) {

@@ -157,4 +259,18 @@ if (!this.has(name)) {

}
return this;
},
/**
* Resolves specified objects
*
* @see set() method
*
* @param {string|object} name Object name or hash of the objects
* @param {string} factory Factory name
* @param {*} object Object or its factory method
* @returns {object} Resolved objects
*
* @this {Injector}
* @private
*/
_resolveObjects: function(name, factory, object) {

@@ -196,2 +312,12 @@

/**
* Creates object creator
*
* @param {string} factoryName Factory name
* @param {*|factory} object Object or its factory function
* @returns {Function} Object creator
*
* @this {Injector}
* @private
*/
_createObjectCreator: function(factoryName, object) {

@@ -217,5 +343,17 @@ if (_.isUndefined(object)) {

});
/**
* Parameter processor
* Checks and convert parameter value
*
* @class
*/
clazz('ParameterProcessor', function(self) {
return {
properties: {
/**
* Processors
* By default there are four processors: type, constraints, converters and default
* @var {object}
*/
processor: {

@@ -227,16 +365,16 @@ type: ['hash', {

return {
type: function(paramValue, metaData, paramName, object) {
return meta('/ClazzJS/Property/Type').apply(paramValue, metaData, paramName, [], object);
type: function(value, metaData, name, object) {
return meta('/ClazzJS/Property/Type').apply(value, metaData, name, [], object);
},
constraints: function(paramValue, metaData, paramName, object) {
return meta('/ClazzJS/Property/Constraints').apply(paramValue, metaData, paramName, [], object);
constraints: function(value, metaData, name, object) {
return meta('/ClazzJS/Property/Constraints').apply(value, metaData, name, [], object);
},
converters: function(paramValue, metaData, paramName, object) {
return meta('/ClazzJS/Property/Converters').apply(paramValue, metaData, paramName, [], object);
converters: function(value, metaData, name, object) {
return meta('/ClazzJS/Property/Converters').apply(value, metaData, name, [], object);
},
default: function(paramValue, metaData, paramName, object) {
if (_.isUndefined(paramValue) || _.isNull(paramValue)) {
paramValue = _.isFunction(metaData) ? metaData.call(object) : metaData;
"default": function(value, metaData, name, object) {
if (_.isUndefined(value) || _.isNull(value)) {
value = _.isFunction(metaData) ? metaData.call(object) : metaData;
}
return paramValue;
return value;
}

@@ -248,5 +386,17 @@ };

methods: {
process: function(paramValue, metaData, paramName, object) {
paramName = paramName || 'unknown';
/**
* Process parameter value
*
* @param {*} value Parameter value
* @param {object} metaData Meta data for parameter
* @param {string} name Parameter name
* @param {object} object Object of specified parameter
* @returns {*} Processed parameter value
*
* @this {ParameterProcessor}
*/
process: function(value, metaData, name, object) {
name = name || 'unknown';
object = object || this;

@@ -262,6 +412,6 @@

paramValue = processors[option].call(that, paramValue, data, paramName, object);
value = processors[option].call(that, value, data, name, object);
});
return paramValue;
return value;
}

@@ -272,5 +422,21 @@ }

namespace('Factories', function(clazz) {
/**
* Abstract object factory
*
* @typedef {function} AbstractFactory
* @class
*/
clazz('Abstract', function(self) {
return {
properties: {
/**
* Parameter processor
* Processed parameters before pass them to create method
*
* @see create()
* @see processParams()
*
* @var {ParameterProcessor}
*/
parameterProcessor: {

@@ -280,3 +446,3 @@ type: ['object', {

}],
default: function() {
"default": function() {
return clazz('/InjectorJS/ParameterProcessor').create();

@@ -287,14 +453,59 @@ }

methods: {
/**
* Gets object factory name
* Must be realized in child clazz
*
* @returns {string} Object factory name
*
* @this {AbstractFactory}
*/
getName: function() {
throw new Error('You must specify type name in child clazz!');
},
/**
* Creates object using specified raw parameters
*
* @param {object} params Raw parameters for object creation
* @returns {*} Created object
*
* @this {AbstractFactory}
*/
create: function(params) {
return this.createObject(this.processParams(params));
},
/**
* Creates object using specified processed parameters
* Must be realized in child clazz
*
* @param {object} params Parameters for object creation
* @returns {*} Created object
*
* @this {AbstractFactory}
*/
createObject: function(params) {
throw new Error('You must realize "createObject" method in child clazz!');
},
/**
* Gets definition of supported parameters for object creation
*
* @returns {object}
*
* @this {AbstractFactory}
*/
getParamsDefinition: function() {
return {};
},
/**
* Process parameters for object creation
*
* @param {object} params Raw object parameters for object creation
* @returns {object} Processed object parameters
*
* @this {AbstractFactory}
*/
processParams: function(params) {

@@ -318,8 +529,20 @@

});
clazz('Clazz', 'Abstract', function(slef) {
/**
* Clazz object factory
* Create clazz based on 'name', 'parent' and 'deps' (dependencies) parameters
*
* @typedef {function} ClazzFactory
* @class
*/
clazz('Clazz', 'Abstract', function(self) {
return {
properties: {
/**
* Clazz constructor
* @var
*/
clazz: {
type: 'function',
default: function() {
"default": function() {
return ClazzJS.clazz;

@@ -330,5 +553,20 @@ }

methods: {
/**
* Gets object factory name
* @returns {string} Object factory name
*
* @this {ClazzFactory}
*/
getName: function() {
return 'clazz'
},
/**
* Gets parameters definition for clazz creation
*
* @returns {object} Parameters definition
*
* @this {ClazzFactory}
*/
getParamsDefinition: function() {

@@ -348,2 +586,11 @@ return {

},
/**
* Creates clazz using specified processed parameters
*
* @param {object} params Parameters for clazz creation
* @returns {*} Created clazz
*
* @this {ClazzFactory}
*/
createObject: function(params) {

@@ -356,8 +603,32 @@ var clazz = this.getClazz();

});
/**
* Object object factory
* Just returns specified value
*
* @typedef {function} ObjectFactory
* @class
*/
clazz('Object', 'Abstract', function(self) {
return {
methods: {
/**
* Gets object factory name
* @returns {string} Object factory name
*
* @this {ObjectFactory}
*/
getName: function() {
return 'object';
},
/**
* Creates object
* Just returns specified value
*
* @param {*} value Some value (must be returned)
* @returns {*} Unprocessed value
*
* @this {AbstractFactory}
*/
create: function(value) {

@@ -369,5 +640,19 @@ return value;

});
/**
* Service object factory
* Instantiate object based on specified class and initialization parameters
*
* @typedef {function} ServiceFactory
* @class
*/
clazz('Service', 'Abstract', function(self) {
return {
methods: {
/**
* Gets object factory name
* @returns {string} Object factory name
*
* @this {ServiceFactory}
*/
getName: function() {

@@ -377,2 +662,9 @@ return 'service'

/**
* Gets parameters definition for object instantiation creation
*
* @returns {object} Parameters definition
*
* @this {ClazzFactory}
*/
getParamsDefinition: function() {

@@ -396,2 +688,10 @@ return {

/**
* Creates object using specified processed parameters
*
* @param {object} params Parameters for object creation
* @returns {*} Created object
*
* @this {ServiceFactory}
*/
createObject: function(params) {

@@ -409,2 +709,13 @@

/**
* Instantiate object of specified class
* Needs to pass random length parameters (to use 'apply' method for class)
*
* @param {function} klass Class
* @param {object} params Initialization parameters
* @returns {object} Instantiated object
*
* @this {ServiceFactory}
* @private
*/
_createService: function(klass, params) {

@@ -411,0 +722,0 @@ var K = function() {

@@ -1,2 +0,2 @@

!function(a,b,c,d){if("function"==typeof define&&define.amd)define(b,c,d);else if("object"==typeof exports&&exports){for(var e=0,f=c.length;f>e;++e){var g=c[e];"string"==typeof g&&(g=g.replace(/([A-Z]+)/g,function(a){return"-"+a.toLowerCase()}).replace(/^-/,""),c[e]=require(g))}var h=d.apply(a,c);for(var i in h)exports[i]=h[i]}else{for(var e=0,f=c.length;f>e;++e){var g=c[e];if("string"==typeof g){if(!(g in a))throw new Error('"'+b+'" dependent on non exited module "'+g+'"!');c[e]=a[g]}}a[b]=d.apply(a,c)}}(new Function("return this")(),"InjectorJS",["ClazzJS"],function(a,b){var c=a.namespace,d=c("InjectorJS").get("clazz"),e=c("InjectorJS").get("meta"),f=a._;c("InjectorJS",function(c,d){c("Injector",function(a){return{properties:{factory:{type:["hash"],"default":{}},defaultFactory:{converters:{fromString:function(a){return f.isUndefined(a)&&(a=this.getFactory(a)),a}},constraints:{exists:function(a){return this.hasFactory(a)}},"default":function(){var a=c("Factories/Object").create();return this.hasFactory(a)||this.setFactory(a),a}},_object:{type:["hash"],"default":{}},_objectCreator:{type:["hash"],"default":{}}},methods:{set:function(a,b,c){var d=this,e=this._resolveObjects(a,b,c);return f.each(e,function(a,b){f.each(a,function(a,c){d._setObjectCreator([c],d._createObjectCreator(b,a))})}),this},has:function(a){return this._hasObject([a])||this._hasObjectCreator([a])},get:function(a){return this._checkObject(a),this._hasObject([a])||this._setObject([a],this._getObjectCreator([a]).call())._removeObjectCreator([a]),this._getObject([a])},remove:function(a){return this._checkObject(a),this._hasObject([a])&&this._removeObject([a])||this._hasObjectCreator([a])&&this._removeObjectCreator([a])},setFactory:function(a,c){return f.isUndefined(c)&&(c=a,a=b),c&&c.__clazz&&c.__clazz.__isSubclazzOf("/InjectorJS/Factories/Abstract")?this.__setPropertyValue(["factory",c.getName()],c):this.__setPropertyValue(["factory"].concat(f.isString(a)?a.split("."):a||[]),c)},hasFactory:function(a){var b=f.isString(a)?a:a.getName();return this.__hasPropertyValue(["factory",b])},setDefaultFactory:function(a){return this.setFactory(a)},_checkObject:function(a){if(!this.has(a))throw new Error('Object "'+a+"' does not exists!'")},_resolveObjects:function(a,c,d){var e=this,g={},h=this.getDefaultFactory().getName();return f.isObject(a)?g=a:(f.isUndefined(d)&&(d=c,c=b),f.isUndefined(c)&&(c=h),g[c]={},g[c][a]=d),f.each(g,function(a,b){e.hasFactory(b)||(h in g||(g[h]={}),g[h][b]=a,delete g[b])}),g},_createObjectCreator:function(a,c){f.isUndefined(c)&&(c=a,a=b);var d=this;return function(){var b=f.isUndefined(a)?d.getDefaultFactory():d.getFactory(a),e=f.isFunction(c)?c.call(d):c;return f.isFunction(b)?b(e):b.create(e)}}}}}),c("ParameterProcessor",function(a){return{properties:{processor:{type:["hash",{element:"function"}],"default":function(){return{type:function(a,b,c,d){return e("/ClazzJS/Property/Type").apply(a,b,c,[],d)},constraints:function(a,b,c,d){return e("/ClazzJS/Property/Constraints").apply(a,b,c,[],d)},converters:function(a,b,c,d){return e("/ClazzJS/Property/Converters").apply(a,b,c,[],d)},"default":function(a,b,c,d){return(f.isUndefined(a)||f.isNull(a))&&(a=f.isFunction(b)?b.call(d):b),a}}}}},methods:{process:function(a,b,c,d){c=c||"unknown",d=d||this;var e=this,g=this.getProcessor();return f.each(b,function(b,f){f in g&&(a=g[f].call(e,a,b,c,d))}),a}}}}),d("Factories",function(b){b("Abstract",function(a){return{properties:{parameterProcessor:{type:["object",{instanceOf:"/InjectorJS/ParameterProcessor"}],"default":function(){return b("/InjectorJS/ParameterProcessor").create()}}},methods:{getName:function(){throw new Error("You must specify type name in child clazz!")},create:function(a){return this.createObject(this.processParams(a))},createObject:function(a){throw new Error('You must realize "createObject" method in child clazz!')},getParamsDefinition:function(){return{}},processParams:function(a){var b=this,c=this.getParamsDefinition(),d=this.getParameterProcessor();return f.each(a,function(e,f){if(!(f in c))throw new Error('Parameter "'+f+'" does not defined!');a[f]=d.process(e,c[f],f,b)}),a}}}}),b("Clazz","Abstract",function(b){return{properties:{clazz:{type:"function","default":function(){return a.clazz}}},methods:{getName:function(){return"clazz"},getParamsDefinition:function(){return{name:{type:"string"},parent:{type:"function"},deps:{type:["array"],"default":[]}}},createObject:function(a){var b=this.getClazz();return b(a.name,a.parent,a.deps)}}}}),b("Object","Abstract",function(a){return{methods:{getName:function(){return"object"},create:function(a){return a}}}}),b("Service","Abstract",function(a){return{methods:{getName:function(){return"service"},getParamsDefinition:function(){return{"class":{type:["function"]},init:{type:["array"],"default":[]},call:{type:["hash",{element:"array"}],"default":{}}}},createObject:function(a){var b=this._createService(a.class,a.init);return f.each(a.call,function(a,c){b[c].apply(b,a)}),b},_createService:function(a,b){var c=function(){return a.apply(this,b)};return c.prototype=a.prototype,new c}}}})})});var g=d("Injector"),h=d("ParameterProcessor"),i=h.create(),j=d("Factories/Abstract"),k=d("Factories/Object",j),l=d("Factories/Clazz",j),m=d("Factories/Service",j),n=k.create(),o=l.create(),p=m.create(),q=g.create().setFactory(n).setFactory(o).setFactory(p).setDefaultFactory(n);return{Factory:{Abstract:j,Object:k,Clazz:l,Service:m},Injector:g,ParameterProcessor:h,injector:q,parameterProcessor:i}});
!function(a,b,c,d){if("function"==typeof define&&define.amd)define(b,c,d);else if("object"==typeof exports&&exports){for(var e=0,f=c.length;f>e;++e){var g=c[e];"string"==typeof g&&(g=g.replace(/([A-Z]+)/g,function(a){return"-"+a.toLowerCase()}).replace(/^-/,""),c[e]=require(g))}var h=d.apply(a,c);for(var i in h)exports[i]=h[i]}else{for(var e=0,f=c.length;f>e;++e){var g=c[e];if("string"==typeof g){if(!(g in a))throw new Error('"'+b+'" dependent on non exited module "'+g+'"!');c[e]=a[g]}}a[b]=d.apply(a,c)}}(new Function("return this")(),"InjectorJS",["ClazzJS"],function(a,b){var c=a.namespace,d=c("InjectorJS").get("clazz"),e=c("InjectorJS").get("meta"),f=a._;c("InjectorJS",function(c,d){c("Injector",function(a){return{properties:{factory:{type:["hash"],"default":{}},defaultFactory:{converters:{fromString:function(a){return f.isUndefined(a)&&(a=this.getFactory(a)),a}},constraints:{exists:function(a){return this.hasFactory(a)}},"default":function(){var a=c("Factories/Object").create();return this.hasFactory(a)||this.setFactory(a),a}},_object:{type:["hash"],"default":{}},_objectCreator:{type:["hash"],"default":{}}},methods:{set:function(a,b,c){var d=this,e=this._resolveObjects(a,b,c);return f.each(e,function(a,b){f.each(a,function(a,c){d._setObjectCreator([c],d._createObjectCreator(b,a))})}),this},has:function(a){return this._hasObject([a])||this._hasObjectCreator([a])},get:function(a){return this._checkObject(a),this._hasObject([a])||this._setObject([a],this._getObjectCreator([a]).call())._removeObjectCreator([a]),this._getObject([a])},remove:function(a){return this._checkObject(a),this._hasObject([a])&&this._removeObject([a])||this._hasObjectCreator([a])&&this._removeObjectCreator([a])},setFactory:function(a,c){if(f.isUndefined(c)&&(c=a,a=b),c&&c.__clazz&&c.__clazz.__isSubclazzOf("/InjectorJS/Factories/Abstract"))return this.__setPropertyValue(["factory",c.getName()],c);var d=f.isString(a)?a.split("."):a||[];return this.__setPropertyValue(["factory"].concat(d),c)},hasFactory:function(a){var b=f.isString(a)?a:a.getName();return this.__hasPropertyValue(["factory",b])},setDefaultFactory:function(a){return this.setFactory(a)},_checkObject:function(a){if(!this.has(a))throw new Error('Object "'+a+"' does not exists!'");return this},_resolveObjects:function(a,c,d){var e=this,g={},h=this.getDefaultFactory().getName();return f.isObject(a)?g=a:(f.isUndefined(d)&&(d=c,c=b),f.isUndefined(c)&&(c=h),g[c]={},g[c][a]=d),f.each(g,function(a,b){e.hasFactory(b)||(h in g||(g[h]={}),g[h][b]=a,delete g[b])}),g},_createObjectCreator:function(a,c){f.isUndefined(c)&&(c=a,a=b);var d=this;return function(){var b=f.isUndefined(a)?d.getDefaultFactory():d.getFactory(a),e=f.isFunction(c)?c.call(d):c;return f.isFunction(b)?b(e):b.create(e)}}}}}),c("ParameterProcessor",function(a){return{properties:{processor:{type:["hash",{element:"function"}],"default":function(){return{type:function(a,b,c,d){return e("/ClazzJS/Property/Type").apply(a,b,c,[],d)},constraints:function(a,b,c,d){return e("/ClazzJS/Property/Constraints").apply(a,b,c,[],d)},converters:function(a,b,c,d){return e("/ClazzJS/Property/Converters").apply(a,b,c,[],d)},"default":function(a,b,c,d){return(f.isUndefined(a)||f.isNull(a))&&(a=f.isFunction(b)?b.call(d):b),a}}}}},methods:{process:function(a,b,c,d){c=c||"unknown",d=d||this;var e=this,g=this.getProcessor();return f.each(b,function(b,f){f in g&&(a=g[f].call(e,a,b,c,d))}),a}}}}),d("Factories",function(b){b("Abstract",function(a){return{properties:{parameterProcessor:{type:["object",{instanceOf:"/InjectorJS/ParameterProcessor"}],"default":function(){return b("/InjectorJS/ParameterProcessor").create()}}},methods:{getName:function(){throw new Error("You must specify type name in child clazz!")},create:function(a){return this.createObject(this.processParams(a))},createObject:function(a){throw new Error('You must realize "createObject" method in child clazz!')},getParamsDefinition:function(){return{}},processParams:function(a){var b=this,c=this.getParamsDefinition(),d=this.getParameterProcessor();return f.each(a,function(e,f){if(!(f in c))throw new Error('Parameter "'+f+'" does not defined!');a[f]=d.process(e,c[f],f,b)}),a}}}}),b("Clazz","Abstract",function(b){return{properties:{clazz:{type:"function","default":function(){return a.clazz}}},methods:{getName:function(){return"clazz"},getParamsDefinition:function(){return{name:{type:"string"},parent:{type:"function"},deps:{type:["array"],"default":[]}}},createObject:function(a){var b=this.getClazz();return b(a.name,a.parent,a.deps)}}}}),b("Object","Abstract",function(a){return{methods:{getName:function(){return"object"},create:function(a){return a}}}}),b("Service","Abstract",function(a){return{methods:{getName:function(){return"service"},getParamsDefinition:function(){return{"class":{type:["function"]},init:{type:["array"],"default":[]},call:{type:["hash",{element:"array"}],"default":{}}}},createObject:function(a){var b=this._createService(a.class,a.init);return f.each(a.call,function(a,c){b[c].apply(b,a)}),b},_createService:function(a,b){var c=function(){return a.apply(this,b)};return c.prototype=a.prototype,new c}}}})})});var g=d("Injector"),h=d("ParameterProcessor"),i=h.create(),j=d("Factories/Abstract"),k=d("Factories/Object",j),l=d("Factories/Clazz",j),m=d("Factories/Service",j),n=k.create(),o=l.create(),p=m.create(),q=g.create().setFactory(n).setFactory(o).setFactory(p).setDefaultFactory(n);return{Factory:{Abstract:j,Object:k,Clazz:l,Service:m},Injector:g,ParameterProcessor:h,injector:q,parameterProcessor:i}});
//# sourceMappingURL=dist/InjectorJS.min.map

@@ -63,1 +63,3 @@ Installation

Enjoy!
[![githalytics.com alpha](https://cruel-carlota.pagodabox.com/c5ce91382a8a9b7ff9ab16e4dee5a25c "githalytics.com")](http://githalytics.com/alexpods/InjectorJS)
{
"name": "injector-js",
"title": "InjectorJS",
"version": "0.2.0",
"version": "0.2.1",
"description": "Simple Dependency Injection Container for JavaScript",

@@ -29,3 +29,3 @@ "author": {

"dependencies": {
"clazz-js": "~0.5.1"
"clazz-js": "~0.5.2"
},

@@ -32,0 +32,0 @@ "devDependencies": {

InjectorJS
==========
InjectorJS is a simple Dependency Injection Container for JavaScript. It helps you to remove of hard-corded
dependencies and make it possible to change them at any time.
InjectorJS is a simple Dependency Injection Container for JavaScript. It helps you to remove hard-corded
dependencies and make it possible to change them at any time. For more information about this pattern look at:
* http://en.wikipedia.org/wiki/Dependency_injection
* http://en.wikipedia.org/wiki/Inversion_of_control
* http://www.youtube.com/watch?v=IKD2-MAkXyQ

@@ -22,5 +25,8 @@ Documentation

```js
console.log(injector.has('name')); // false
injector.set('name', 'Bob');
injector.set('PI', 3.14);
injector.set('currentTime', new Date());
injector.set('PI', 3.14).set('currentTime', new Date());
console.log(injector.has('name')); // true
```

@@ -27,0 +33,0 @@

@@ -0,7 +1,23 @@

/**
* Abstract object factory
*
* @typedef {function} AbstractFactory
* @class
*/
clazz('Abstract', function(self) {
return {
properties: {
/**
* Parameter processor
* Processed parameters before pass them to create method
*
* @see create()
* @see processParams()
*
* @var {ParameterProcessor}
*/
parameterProcessor: {
type: ['object', { instanceOf: '/InjectorJS/ParameterProcessor' }],
default: function() {
"default": function() {
return clazz('/InjectorJS/ParameterProcessor').create();

@@ -12,14 +28,59 @@ }

methods: {
/**
* Gets object factory name
* Must be realized in child clazz
*
* @returns {string} Object factory name
*
* @this {AbstractFactory}
*/
getName: function() {
throw new Error('You must specify type name in child clazz!');
},
/**
* Creates object using specified raw parameters
*
* @param {object} params Raw parameters for object creation
* @returns {*} Created object
*
* @this {AbstractFactory}
*/
create: function(params) {
return this.createObject(this.processParams(params));
},
/**
* Creates object using specified processed parameters
* Must be realized in child clazz
*
* @param {object} params Parameters for object creation
* @returns {*} Created object
*
* @this {AbstractFactory}
*/
createObject: function(params) {
throw new Error('You must realize "createObject" method in child clazz!');
},
/**
* Gets definition of supported parameters for object creation
*
* @returns {object}
*
* @this {AbstractFactory}
*/
getParamsDefinition: function() {
return {};
},
/**
* Process parameters for object creation
*
* @param {object} params Raw object parameters for object creation
* @returns {object} Processed object parameters
*
* @this {AbstractFactory}
*/
processParams: function(params) {

@@ -26,0 +87,0 @@

@@ -1,7 +0,19 @@

clazz('Clazz', 'Abstract', function(slef) {
/**
* Clazz object factory
* Create clazz based on 'name', 'parent' and 'deps' (dependencies) parameters
*
* @typedef {function} ClazzFactory
* @class
*/
clazz('Clazz', 'Abstract', function(self) {
return {
properties: {
/**
* Clazz constructor
* @var
*/
clazz: {
type: 'function',
default: function() {
"default": function() {
return ClazzJS.clazz;

@@ -12,5 +24,20 @@ }

methods: {
/**
* Gets object factory name
* @returns {string} Object factory name
*
* @this {ClazzFactory}
*/
getName: function() {
return 'clazz'
},
/**
* Gets parameters definition for clazz creation
*
* @returns {object} Parameters definition
*
* @this {ClazzFactory}
*/
getParamsDefinition: function() {

@@ -30,2 +57,11 @@ return {

},
/**
* Creates clazz using specified processed parameters
*
* @param {object} params Parameters for clazz creation
* @returns {*} Created clazz
*
* @this {ClazzFactory}
*/
createObject: function(params) {

@@ -32,0 +68,0 @@ var clazz = this.getClazz();

@@ -0,7 +1,31 @@

/**
* Object object factory
* Just returns specified value
*
* @typedef {function} ObjectFactory
* @class
*/
clazz('Object', 'Abstract', function(self) {
return {
methods: {
/**
* Gets object factory name
* @returns {string} Object factory name
*
* @this {ObjectFactory}
*/
getName: function() {
return 'object';
},
/**
* Creates object
* Just returns specified value
*
* @param {*} value Some value (must be returned)
* @returns {*} Unprocessed value
*
* @this {AbstractFactory}
*/
create: function(value) {

@@ -8,0 +32,0 @@ return value;

@@ -0,4 +1,18 @@

/**
* Service object factory
* Instantiate object based on specified class and initialization parameters
*
* @typedef {function} ServiceFactory
* @class
*/
clazz('Service', 'Abstract', function(self) {
return {
methods: {
/**
* Gets object factory name
* @returns {string} Object factory name
*
* @this {ServiceFactory}
*/
getName: function() {

@@ -8,2 +22,9 @@ return 'service'

/**
* Gets parameters definition for object instantiation creation
*
* @returns {object} Parameters definition
*
* @this {ClazzFactory}
*/
getParamsDefinition: function() {

@@ -25,2 +46,10 @@ return {

/**
* Creates object using specified processed parameters
*
* @param {object} params Parameters for object creation
* @returns {*} Created object
*
* @this {ServiceFactory}
*/
createObject: function(params) {

@@ -38,2 +67,13 @@

/**
* Instantiate object of specified class
* Needs to pass random length parameters (to use 'apply' method for class)
*
* @param {function} klass Class
* @param {object} params Initialization parameters
* @returns {object} Instantiated object
*
* @this {ServiceFactory}
* @private
*/
_createService: function(klass, params) {

@@ -40,0 +80,0 @@ var K = function() {

@@ -0,4 +1,15 @@

/**
* Injector
* Realization of DI container.
*
* @class
*/
clazz('Injector', function(self) {
return {
properties: {
/**
* Supported factories
* @var {object}
*/
factory: {

@@ -8,2 +19,7 @@ type: ['hash'],

},
/**
* Default factory
* @var {AbstractFactory}
*/
defaultFactory: {

@@ -32,2 +48,7 @@ converters: {

},
/**
* Created objects in the container
* @var {object}
*/
_object: {

@@ -37,2 +58,7 @@ type: ['hash'],

},
/**
* Object creators. Used when gets some object.
* @var {object}
*/
_objectCreator: {

@@ -45,2 +71,12 @@ type: ['hash'],

/**
* Sets new object to the container
*
* @param {string|object} name Object name or hash of the objects
* @param {string} factory Factory name
* @param {*} object Object or its factory method
* @returns {Injector} this
*
* @this {Injector}
*/
set: function(name, factory, object) {

@@ -60,2 +96,10 @@

/**
* Checks whether specified object exist
*
* @param {string} name Object name
* @returns {boolean} true if specified object exist
*
* @this {Injector}
*/
has: function(name) {

@@ -65,2 +109,12 @@ return this._hasObject([name]) || this._hasObjectCreator([name]);

/**
* Gets specified object
*
* @param {string} name Object name
* @returns {*} Specified object
*
* @throws {Error} if specified object does not exist
*
* @this {Injector}
*/
get: function(name) {

@@ -75,2 +129,12 @@ this._checkObject(name);

/**
* Removes specified object
*
* @param {string} name Object name
* @returns {Injector} this
*
* @throws {Error} if specified object does not exist
*
* @this {Injector}
*/
remove: function(name) {

@@ -83,14 +147,34 @@ this._checkObject(name);

setFactory: function(fields, value) {
if (_.isUndefined(value)) {
value = fields;
fields = undefined;
/**
* Sets object factory
*
* @param {string,Factory} name Factory name of factory instance
* @param {function|Factory} factory Object factory
* @returns {Injector} this
*
* @this {Injector}
*/
setFactory: function(name, factory) {
if (_.isUndefined(factory)) {
factory = name;
name = undefined;
}
if (value && value.__clazz && value.__clazz.__isSubclazzOf('/InjectorJS/Factories/Abstract')) {
return this.__setPropertyValue(['factory', value.getName()], value);
if (factory && factory.__clazz && factory.__clazz.__isSubclazzOf('/InjectorJS/Factories/Abstract')) {
return this.__setPropertyValue(['factory', factory.getName()], factory);
}
return this.__setPropertyValue(['factory'].concat(_.isString(fields) ? fields.split('.') : fields || []), value);
var fields = _.isString(name) ? name.split('.') : name || [];
return this.__setPropertyValue(['factory'].concat(fields), factory);
},
/**
* Checks whether specified factory exist
*
* @param {string|Factory} factory Object factory or its name
* @returns {boolean} true if object factory exist
*
* @this {Injector}
*/
hasFactory: function(factory) {

@@ -101,2 +185,9 @@ var factoryName = _.isString(factory) ? factory : factory.getName();

/**
* Sets default factory
* @param {string|Factory} factory Default factory or its name
* @returns {Injector} this
*
* @this {Injector}
*/
setDefaultFactory: function(factory) {

@@ -106,2 +197,13 @@ return this.setFactory(factory);

/**
* Checks whether specified object exist
*
* @param {string} name Object name
* @returns {Injector} this
*
* @throws {Error} if specified object does not exist
*
* @this {Injector}
* @private
*/
_checkObject: function(name) {

@@ -112,4 +214,18 @@ if (!this.has(name)) {

}
return this;
},
/**
* Resolves specified objects
*
* @see set() method
*
* @param {string|object} name Object name or hash of the objects
* @param {string} factory Factory name
* @param {*} object Object or its factory method
* @returns {object} Resolved objects
*
* @this {Injector}
* @private
*/
_resolveObjects: function(name, factory, object) {

@@ -152,2 +268,12 @@

/**
* Creates object creator
*
* @param {string} factoryName Factory name
* @param {*|factory} object Object or its factory function
* @returns {Function} Object creator
*
* @this {Injector}
* @private
*/
_createObjectCreator: function(factoryName, object) {

@@ -159,3 +285,3 @@ if (_.isUndefined(object)) {

var that = this;
var that = this;

@@ -162,0 +288,0 @@ return function() {

@@ -0,4 +1,16 @@

/**
* Parameter processor
* Checks and convert parameter value
*
* @class
*/
clazz('ParameterProcessor', function(self) {
return {
properties: {
/**
* Processors
* By default there are four processors: type, constraints, converters and default
* @var {object}
*/
processor: {

@@ -8,18 +20,18 @@ type: ['hash', { element: 'function' }],

return {
type: function(paramValue, metaData, paramName, object) {
return meta('/ClazzJS/Property/Type').apply(paramValue, metaData, paramName, [], object);
type: function(value, metaData, name, object) {
return meta('/ClazzJS/Property/Type').apply(value, metaData, name, [], object);
},
constraints: function(paramValue, metaData, paramName, object) {
return meta('/ClazzJS/Property/Constraints').apply(paramValue, metaData, paramName, [], object);
constraints: function(value, metaData, name, object) {
return meta('/ClazzJS/Property/Constraints').apply(value, metaData, name, [], object);
},
converters: function(paramValue, metaData, paramName, object) {
return meta('/ClazzJS/Property/Converters').apply(paramValue, metaData, paramName, [], object);
converters: function(value, metaData, name, object) {
return meta('/ClazzJS/Property/Converters').apply(value, metaData, name, [], object);
},
default: function(paramValue, metaData, paramName, object) {
if (_.isUndefined(paramValue) || _.isNull(paramValue)) {
paramValue = _.isFunction(metaData)
"default": function(value, metaData, name, object) {
if (_.isUndefined(value) || _.isNull(value)) {
value = _.isFunction(metaData)
? metaData.call(object)
: metaData;
}
return paramValue;
return value;
}

@@ -31,7 +43,19 @@ };

methods: {
process: function(paramValue, metaData, paramName, object) {
paramName = paramName || 'unknown';
object = object || this;
/**
* Process parameter value
*
* @param {*} value Parameter value
* @param {object} metaData Meta data for parameter
* @param {string} name Parameter name
* @param {object} object Object of specified parameter
* @returns {*} Processed parameter value
*
* @this {ParameterProcessor}
*/
process: function(value, metaData, name, object) {
name = name || 'unknown';
object = object || this;
var that = this;

@@ -45,6 +69,6 @@ var processors = this.getProcessor();

paramValue = processors[option].call(that, paramValue, data, paramName, object);
value = processors[option].call(that, value, data, name, object);
});
return paramValue;
return value;
}

@@ -51,0 +75,0 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc