injector-js
Advanced tools
Comparing version 0.0.0 to 0.1.0
{ | ||
"name": "InjectorJS", | ||
"version": "0.0.0", | ||
"version": "0.1.0", | ||
"ignore": [ | ||
@@ -11,4 +11,4 @@ "node_modules", | ||
"dependencies": { | ||
"ClazzJS": "~0.2.0" | ||
"ClazzJS": "~0.3.0" | ||
} | ||
} |
@@ -1,10 +0,379 @@ | ||
;(function(global, undefined) { | ||
; | ||
(function(global, name, dependencies, factory) { | ||
// AMD integration | ||
if (typeof define === 'function' && define.amd) { | ||
define(name, dependencies, factory); | ||
} | ||
// CommonJS integration | ||
else if (typeof exports === "object" && exports) { | ||
for (var i = 0, ii = dependencies.length; i < ii; ++i) { | ||
var dependency = dependencies[i]; | ||
if (typeof dependency === 'string') { | ||
dependency = dependency.replace(/([A-Z]+)/g, function($1) { | ||
return '-' + $1.toLowerCase(); | ||
}).replace(/^-/, ''); | ||
dependencies[i] = require(dependency); | ||
} | ||
} | ||
var module = factory.apply(global, dependencies); | ||
for (var property in module) { | ||
exports[property] = module[property]; | ||
} | ||
} | ||
// Just global variable | ||
else { | ||
for (var i = 0, ii = dependencies.length; i < ii; ++i) { | ||
var dependency = dependencies[i]; | ||
if (typeof dependency === 'string') { | ||
if (!(dependency in global)) { | ||
throw new Error('"' + name + '" dependent on non exited module "' + dependency + '"!'); | ||
} | ||
dependencies[i] = global[dependency]; | ||
} | ||
} | ||
global[name] = factory.apply(global, dependencies); | ||
} | ||
}((new Function('return this'))(), 'InjectorJS', ['ClazzJS'], function(ClazzJS, undefined) { | ||
var namespace = ClazzJS.namespace; | ||
var clazz = ClazzJS.clazz; | ||
var meta = ClazzJS.meta; | ||
;(function(global) { | ||
namespace('InjectorJS', function(clazz, namespace) { | ||
clazz('Injector', function() { | ||
return { | ||
properties: { | ||
defaultType: { | ||
type: ['string'], | ||
constraints: { | ||
typeFactoryExists: function(type) { | ||
return this.hasTypeFactory(type); | ||
} | ||
} | ||
}, | ||
typeFactory: { | ||
type: ['hash', { | ||
element: ['object', { | ||
instanceof: 'TypeFactories/Abstract' | ||
}] | ||
}], | ||
default: {} | ||
}, | ||
createdObject: { | ||
type: ['hash'], | ||
default: {} | ||
}, | ||
objectFactory: { | ||
type: ['hash'], | ||
default: {} | ||
} | ||
}, | ||
methods: { | ||
has: function(name) { | ||
return this.hasCreatedObject(name) || this.hasObjectFactory(name); | ||
}, | ||
get: function(name) { | ||
if (!this.hasCreatedObject(name)) { | ||
if (!this.hasObjectFactory(name)) { | ||
throw new Error('Factory for object "' + name + "' does not exists!'"); | ||
} | ||
this.setCreatedObject(name, this.getObjectFactory(name).call()); | ||
this.removeObjectFactory(name); | ||
} | ||
})(global); | ||
return this.getCreatedObject(name); | ||
}, | ||
set: function(name, type, params) { | ||
if (_.isObject(name)) { | ||
var objects = name; | ||
for (name in objects) { | ||
})(this); | ||
params = objects[name]; | ||
type = params.type; | ||
delete params.type; | ||
this.setObjectFactory(name, this.createFactoryMethod(type, params)); | ||
} | ||
} else { | ||
this.setObjectFactory(name, this.createFactoryMethod(type, params)); | ||
} | ||
return this; | ||
}, | ||
createFactoryMethod: function(type, params) { | ||
if (_.isUndefined(params)) { | ||
params = type; | ||
type = undefined; | ||
} | ||
if (_.isUndefined(type)) { | ||
if (!this.hasDefaultType()) { | ||
throw new Error('You must specify type for object "' + name + '"!'); | ||
} | ||
type = this.getDefaultType(); | ||
} | ||
return this.getTypeFactory(type).getFactoryMethod(params); | ||
} | ||
} | ||
} | ||
}); | ||
clazz('ParameterProcessor', function() { | ||
return { | ||
constants: { | ||
PROCESSORS: { | ||
type: meta('/ClazzJS/Property/Type'), | ||
constraints: meta('/ClazzJS/Property/Constraints'), | ||
converters: meta('/ClazzJS/Property/Converters') | ||
} | ||
}, | ||
methods: { | ||
process: function(value, meta, name, object) { | ||
for (var option in meta) { | ||
switch (option) { | ||
case 'type': | ||
case 'constraints': | ||
case 'converters': | ||
value = this.const('PROCESSORS')(option)().apply(value, meta[option], name, object); | ||
break; | ||
case 'default': | ||
var defaultValue = meta[option]; | ||
if (_.isFunction(defaultValue)) { | ||
defaultValue = defaultValue.call(object); | ||
} | ||
if (_.isUndefined(value) || _.isNull(value)) { | ||
value = defaultValue; | ||
} | ||
break; | ||
} | ||
} | ||
return value; | ||
} | ||
} | ||
} | ||
}); | ||
namespace('TypeFactories', function(clazz) { | ||
clazz('Abstract', function(parameterProcessor) { | ||
return { | ||
methods: { | ||
getName: function() { | ||
this.__abstract() | ||
}, | ||
getFactoryMethod: function(params) { | ||
var self = this; | ||
return function() { | ||
return self.createObject(self.processParams(params)); | ||
} | ||
}, | ||
createObject: function() { | ||
this.__abstract(); | ||
}, | ||
getParamsDefinitions: function() { | ||
return {}; | ||
}, | ||
processParams: function(params) { | ||
var paramsDefinition = this.getParamsDefinitions(); | ||
for (var param in params) { | ||
if (!(param in paramsDefinition)) { | ||
throw new Error('Parameter "' + param + '" does not defined!'); | ||
} | ||
params[param] = parameterProcessor.process(params[param], paramsDefinition[param], param, this); | ||
} | ||
return params; | ||
} | ||
} | ||
}; | ||
}); | ||
clazz('Clazz', 'Abstract', function(clazz, injector) { | ||
return { | ||
methods: { | ||
getName: function() { | ||
return 'clazz' | ||
}, | ||
getParamsDefinitions: function() { | ||
return { | ||
name: { | ||
type: ['string'], | ||
required: true | ||
}, | ||
parent: { | ||
converters: { | ||
stringOrClazz: function(value) { | ||
if (_.isString(value)) { | ||
value = injector.get(value); | ||
} | ||
return value; | ||
} | ||
} | ||
}, | ||
deps: { | ||
type: ['array'], | ||
default: [], | ||
converters: { | ||
resolve: function(value) { | ||
if (_.isString(value)) { | ||
value = injector.get(value); | ||
} | ||
return value; | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
createObject: function(params) { | ||
return clazz(params.name, params.parent, params.deps) | ||
} | ||
} | ||
} | ||
}); | ||
clazz('Parameter', 'Abstract', function() { | ||
return { | ||
methods: { | ||
getName: function() { | ||
return 'parameter'; | ||
}, | ||
processParams: function(value) { | ||
if (_.isFunction(value)) { | ||
value = value(); | ||
} | ||
return value; | ||
}, | ||
createObject: function(value) { | ||
return value; | ||
} | ||
} | ||
}; | ||
}); | ||
clazz('Service', 'Abstract', function(injector) { | ||
return { | ||
methods: { | ||
getName: function() { | ||
return 'service' | ||
}, | ||
getParamsDefinitions: function() { | ||
return { | ||
class: { | ||
type: ['function'], | ||
required: true, | ||
converters: { | ||
resolve: function(klass) { | ||
if (_.isString(klass)) { | ||
klass = injector.get(klass); | ||
} | ||
return klass; | ||
} | ||
} | ||
}, | ||
init: { | ||
type: ['array'], | ||
default: [], | ||
converters: { | ||
resolve: function(initParams) { | ||
initParams = [].concat(initParams); | ||
for (var i = 0, ii = initParams.length; i < ii; ++i) { | ||
if (_.isString(initParams[i])) { | ||
initParams[i] = injector.get(initParams[i]); | ||
} | ||
} | ||
return initParams; | ||
} | ||
} | ||
}, | ||
call: { | ||
type: ['hash', { | ||
element: { | ||
type: 'array' | ||
} | ||
}], | ||
default: {}, | ||
converters: { | ||
resolve: function(methods) { | ||
for (var method in methods) { | ||
methods[method] = [].concat(methods[method]); | ||
for (var i = 0, ii = methods[method].length; i < ii; ++i) { | ||
if (_.isString(methods[method][i])) { | ||
methods[method][i] = injector.get(methods[method][i]); | ||
} | ||
} | ||
} | ||
return methods; | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
createObject: function(params) { | ||
var service = construct(params.class, params.init); | ||
for (var method in params.call) { | ||
service[service].apply(service, params.call[method]); | ||
} | ||
return service; | ||
function construct(klass, params) { | ||
var K = function() { | ||
return klass.apply(this, params); | ||
}; | ||
K.prototype = klass.prototype; | ||
return new K(); | ||
} | ||
} | ||
} | ||
}; | ||
}); | ||
}); | ||
}); | ||
var Injector = clazz('/InjectorJS/Injector'); | ||
var ParameterProcessor = clazz('/InjectorJS/ParameterProcessor'); | ||
var injector = Injector.create(); | ||
var parameterProcessor = ParameterProcessor.create(); | ||
var AbstractTypeFactory = clazz('/InjectorJS/TypeFactories/Abstract', [parameterProcessor]); | ||
var ParameterTypeFactory = clazz('/InjectorJS/TypeFactories/Parameter', AbstractTypeFactory); | ||
var ClazzTypeFactory = clazz('/InjectorJS/TypeFactories/Clazz', AbstractTypeFactory, [clazz, injector]); | ||
var ServiceTypeFactory = clazz('/InjectorJS/TypeFactories/Service', AbstractTypeFactory, [injector]); | ||
var parameterTypeFactory = ParameterTypeFactory.create(); | ||
var clazzTypeFactory = ClazzTypeFactory.create(); | ||
var serviceTypeFactory = ServiceTypeFactory.create(); | ||
injector | ||
.setTypeFactory(parameterTypeFactory.getName(), parameterTypeFactory) | ||
.setTypeFactory(clazzTypeFactory.getName(), clazzTypeFactory) | ||
.setTypeFactory(serviceTypeFactory.getName(), serviceTypeFactory) | ||
.setDefaultType(parameterTypeFactory.getName()); | ||
return { | ||
TypeFactory: { | ||
Abstract: AbstractTypeFactory, | ||
Parameter: ParameterTypeFactory, | ||
Clazz: ClazzTypeFactory, | ||
Service: ServiceTypeFactory | ||
}, | ||
Injector: Injector, | ||
ParameterProcessor: ParameterProcessor, | ||
injector: injector, | ||
parameterProcessor: parameterProcessor | ||
}; | ||
})); |
@@ -1,2 +0,2 @@ | ||
!function(a,b){!function(a){}(a)}(this); | ||
!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=a.clazz,e=a.meta;c("InjectorJS",function(a,c){a("Injector",function(){return{properties:{defaultType:{type:["string"],constraints:{typeFactoryExists:function(a){return this.hasTypeFactory(a)}}},typeFactory:{type:["hash",{element:["object",{"instanceof":"TypeFactories/Abstract"}]}],"default":{}},createdObject:{type:["hash"],"default":{}},objectFactory:{type:["hash"],"default":{}}},methods:{has:function(a){return this.hasCreatedObject(a)||this.hasObjectFactory(a)},get:function(a){if(!this.hasCreatedObject(a)){if(!this.hasObjectFactory(a))throw new Error('Factory for object "'+a+"' does not exists!'");this.setCreatedObject(a,this.getObjectFactory(a).call()),this.removeObjectFactory(a)}return this.getCreatedObject(a)},set:function(a,b,c){if(_.isObject(a)){var d=a;for(a in d)c=d[a],b=c.type,delete c.type,this.setObjectFactory(a,this.createFactoryMethod(b,c))}else this.setObjectFactory(a,this.createFactoryMethod(b,c));return this},createFactoryMethod:function(a,c){if(_.isUndefined(c)&&(c=a,a=b),_.isUndefined(a)){if(!this.hasDefaultType())throw new Error('You must specify type for object "'+name+'"!');a=this.getDefaultType()}return this.getTypeFactory(a).getFactoryMethod(c)}}}}),a("ParameterProcessor",function(){return{constants:{PROCESSORS:{type:e("/ClazzJS/Property/Type"),constraints:e("/ClazzJS/Property/Constraints"),converters:e("/ClazzJS/Property/Converters")}},methods:{process:function(a,b,c,d){for(var e in b)switch(e){case"type":case"constraints":case"converters":a=this.const("PROCESSORS")(e)().apply(a,b[e],c,d);break;case"default":var f=b[e];_.isFunction(f)&&(f=f.call(d)),(_.isUndefined(a)||_.isNull(a))&&(a=f)}return a}}}}),c("TypeFactories",function(a){a("Abstract",function(a){return{methods:{getName:function(){this.__abstract()},getFactoryMethod:function(a){var b=this;return function(){return b.createObject(b.processParams(a))}},createObject:function(){this.__abstract()},getParamsDefinitions:function(){return{}},processParams:function(b){var c=this.getParamsDefinitions();for(var d in b){if(!(d in c))throw new Error('Parameter "'+d+'" does not defined!');b[d]=a.process(b[d],c[d],d,this)}return b}}}}),a("Clazz","Abstract",function(a,b){return{methods:{getName:function(){return"clazz"},getParamsDefinitions:function(){return{name:{type:["string"],required:!0},parent:{converters:{stringOrClazz:function(a){return _.isString(a)&&(a=b.get(a)),a}}},deps:{type:["array"],"default":[],converters:{resolve:function(a){return _.isString(a)&&(a=b.get(a)),a}}}}},createObject:function(b){return a(b.name,b.parent,b.deps)}}}}),a("Parameter","Abstract",function(){return{methods:{getName:function(){return"parameter"},processParams:function(a){return _.isFunction(a)&&(a=a()),a},createObject:function(a){return a}}}}),a("Service","Abstract",function(a){return{methods:{getName:function(){return"service"},getParamsDefinitions:function(){return{"class":{type:["function"],required:!0,converters:{resolve:function(b){return _.isString(b)&&(b=a.get(b)),b}}},init:{type:["array"],"default":[],converters:{resolve:function(b){b=[].concat(b);for(var c=0,d=b.length;d>c;++c)_.isString(b[c])&&(b[c]=a.get(b[c]));return b}}},call:{type:["hash",{element:{type:"array"}}],"default":{},converters:{resolve:function(b){for(var c in b){b[c]=[].concat(b[c]);for(var d=0,e=b[c].length;e>d;++d)_.isString(b[c][d])&&(b[c][d]=a.get(b[c][d]))}return b}}}}},createObject:function(a){function b(a,b){var c=function(){return a.apply(this,b)};return c.prototype=a.prototype,new c}var c=b(a.class,a.init);for(var d in a.call)c[c].apply(c,a.call[d]);return c}}}})})});var f=d("/InjectorJS/Injector"),g=d("/InjectorJS/ParameterProcessor"),h=f.create(),i=g.create(),j=d("/InjectorJS/TypeFactories/Abstract",[i]),k=d("/InjectorJS/TypeFactories/Parameter",j),l=d("/InjectorJS/TypeFactories/Clazz",j,[d,h]),m=d("/InjectorJS/TypeFactories/Service",j,[h]),n=k.create(),o=l.create(),p=m.create();return h.setTypeFactory(n.getName(),n).setTypeFactory(o.getName(),o).setTypeFactory(p.getName(),p).setDefaultType(n.getName()),{TypeFactory:{Abstract:j,Parameter:k,Clazz:l,Service:m},Injector:f,ParameterProcessor:g,injector:h,parameterProcessor:i}}); | ||
//# sourceMappingURL=dist/InjectorJS.min.map |
@@ -7,2 +7,3 @@ "use strict"; | ||
grunt.loadNpmTasks('grunt-contrib-uglify'); | ||
grunt.loadNpmTasks('grunt-jsbeautifier'); | ||
@@ -16,5 +17,6 @@ grunt.initConfig({ | ||
"src/.prefix", | ||
"src/Injector.js", | ||
"src/*.js", | ||
"src/TypeFactories/.prefix", | ||
"src/TypeFactories/*.js", | ||
"src/TypeFactories/.suffix", | ||
"src/.build", | ||
@@ -39,6 +41,11 @@ "src/.suffix" | ||
} | ||
}, | ||
jsbeautifier: { | ||
dev: { | ||
src: ["<%= concat.dev.dest %>"] | ||
} | ||
} | ||
}) | ||
grunt.registerTask('default', ['concat', 'uglify']); | ||
grunt.registerTask('default', ['concat', 'uglify', 'jsbeautifier']); | ||
}; |
{ | ||
"name": "injector-js", | ||
"title": "InjectorJS", | ||
"version": "0.0.0", | ||
"description": "Dependency Injection for JavaScript", | ||
"version": "0.1.0", | ||
"description": "IoC container for JavaScript", | ||
"author": { | ||
@@ -18,3 +18,5 @@ "name": "Aleksey Podskrebyshev (Alex Pods)", | ||
"service", | ||
"inject" | ||
"inject", | ||
"injector", | ||
"dependency injection" | ||
], | ||
@@ -26,8 +28,12 @@ "license": "MIT", | ||
}, | ||
"dependencies": { | ||
"clazz-js": "~0.3.0" | ||
}, | ||
"devDependencies": { | ||
"grunt": "~0.4.1", | ||
"grunt-contrib-concat": "~0.3.0", | ||
"grunt-contrib-uglify": "~0.2.4" | ||
"grunt-contrib-uglify": "~0.2.4", | ||
"grunt-jsbeautifier": "~0.2.3" | ||
}, | ||
"readmeFilename": "README.md" | ||
} |
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
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
37416
19
677
1
4
3
2
3
+ Addedclazz-js@~0.3.0
+ Addedclazz-js@0.3.3(transitive)