Comparing version 0.2.0 to 0.2.1
{ | ||
"name": "ClazzJS", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"ignore": [ | ||
@@ -11,4 +11,4 @@ "node_modules", | ||
"dependencies": { | ||
"MetaJS": "~0.3.0" | ||
"MetaJS": "~0.3.1" | ||
} | ||
} |
@@ -1,4 +0,37 @@ | ||
;(function(global, meta, 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'))(), 'ClazzJS', ['MetaJS'], function (MetaJS, undefined) { | ||
var meta = MetaJS.meta; | ||
var Clazz = function(manager, factory, namespace) { | ||
@@ -37,7 +70,8 @@ | ||
Clazz.prototype = { | ||
get: function(name, dependencies) { | ||
get: function(originName, dependencies) { | ||
var name; | ||
name = this.resolveName(name); | ||
name = this.resolveName(originName); | ||
if (!name) { | ||
throw new Error('Clazz with name "' + name + '" does not exits!'); | ||
throw new Error('Clazz with name "' + originName + '" does not exits!'); | ||
} | ||
@@ -571,3 +605,18 @@ | ||
__getProperties: function() { | ||
return this.__properties; | ||
var parent = this; | ||
var properties = {}; | ||
while (parent) { | ||
if (parent.__properties) { | ||
for (var property in parent.__properties) { | ||
if (property in properties) { | ||
continue; | ||
} | ||
properties[property] = parent.__properties[property]; | ||
} | ||
} | ||
parent = parent.parent; | ||
} | ||
return properties; | ||
}, | ||
@@ -594,5 +643,7 @@ | ||
__getProperty: function(property, key) { | ||
var properties = this.__getProperties(); | ||
return typeof key === 'undefined' | ||
? this.__properties[property] | ||
: this.__properties[property] && this.__properties[property][key]; | ||
? properties[property] | ||
: properties[property] && properties[property][key]; | ||
}, | ||
@@ -603,3 +654,3 @@ | ||
return property in this.__properties; | ||
return property in this.__getProperties(); | ||
}, | ||
@@ -918,3 +969,3 @@ | ||
return { | ||
name: (0 !== property.indexOf('is') ? 'is' : '') + property[0].toUpperCase() + property.slice(1), | ||
name: (0 !== property.indexOf('is') ? 'is' + property[0].toUpperCase() : '' + property[0]) + property.slice(1), | ||
body: function() { | ||
@@ -991,9 +1042,9 @@ return this.__isPropertyValue.apply(this, [property].concat(Array.prototype.slice.call(arguments))); | ||
if ('instanceof' in params) { | ||
var clazz = params.instanceof; | ||
var clazzInstance = params.instanceof; | ||
if (Object.prototype.toString.call(clazz) === '[object Array]') { | ||
clazz = Clazz(clazz[0], clazz[1] || []); | ||
if (Object.prototype.toString.call(clazzInstance) === '[object Array]') { | ||
clazzInstance = clazz(clazzInstance[0], clazzInstance[1] || []); | ||
} | ||
if (!(value instanceof clazz)) { | ||
throw new Error('Value does not instance of clazz "' + clazz.NAME + '"!'); | ||
if (!(value instanceof clazzInstance)) { | ||
throw new Error('Value does not instance of clazz "' + clazzInstance.NAME + '"!'); | ||
} | ||
@@ -1035,15 +1086,19 @@ } | ||
} | ||
}) | ||
;(function(global, meta) { | ||
}); | ||
var factory = new Factory(Base, meta); | ||
var manager = new Manager(); | ||
var namespace = new Namespace(manager, factory, null, '', global, Clazz); | ||
var clazz = namespace(); | ||
var factory = new Factory(Base, meta); | ||
var manager = new Manager(); | ||
var namespace = new Namespace(manager, factory, null, '', (new Function('return this'))(), Clazz); | ||
var clazz = namespace(); | ||
global.namespace = namespace; | ||
global.clazz = clazz; | ||
return { | ||
Base: Base, | ||
Mangaer: Manager, | ||
Namespace: Namespace, | ||
Clazz: Clazz, | ||
})(global, meta); | ||
namespace: namespace, | ||
clazz: clazz | ||
} | ||
})(this, meta); | ||
})) |
@@ -1,2 +0,2 @@ | ||
!function(a,b,c){var d=function(a,b,c){var e=function(a,b,c,d){var f=arguments[arguments.length-1];return"function"!=typeof f&&"[object Object]"!==Object.prototype.toString.call(f)?e.get(a,b):(e.set(a,b,c,d),void 0)};for(var f in d.prototype)e[f]=d.prototype[f];return e.getManager=function(){return a},e.getFactory=function(){return b},e.getNamespace=function(){return c},e};d.prototype={get:function(a,b){if(a=this.resolveName(a),!a)throw new Error('Clazz with name "'+a+'" does not exits!');b=b||[];var c=this.getManager(),d=this.getFactory();if(!c.hasClazz(a,b)){var e=c.getMeta(a);c.setClazz(d.create({name:a,dependencies:b,process:e.process,parent:this.adjustParent(e.parent),meta:e.meta}))}return c.getClazz(a,b)},has:function(a){return!!this.resolveName(a)},set:function(a,b,c,d){var e=this.getNamespace(),f=this.getManager();return"object"==typeof a?(b=a.parent,c=a.process,d=a.meta,a=a.name):("undefined"==typeof d&&(d=c,c=null),"undefined"==typeof d&&(d=b,b=null),"[object Array]"===Object.prototype.toString.call(b)&&(c=b,b=null)),a=e.apply(a),f.setMeta(a,{parent:b,process:c,meta:d}),this},resolveName:function(a){var b,c,d,e,f=this.getManager(),g=this.getNamespace();for(b=g.getPaths(),d=0,e=b.length;e>d;++d)if(c=g.apply(a,b[d]),f.hasMeta(c))return c;return!1},adjustParent:function(a){return"string"==typeof a&&(a=[a]),"[object Array]"===Object.prototype.toString.call(a)&&(a=this.get(a[0],a[1]||[])),a}};var e=function(a,b,c,f,g,h){h=h||d;var i=function(c,d){var f=new e(a,b,i,c,g),j=new h(a,b,i);return d&&d(f,j),j};return i.getManager=function(){return a},i.getFactory=function(){return b},i.getBaseNamespace=function(){return c},i.getGlobal=function(){return g},i.getPath=function(){return e.adjust((c?c.getPath():e.GLOBAL)+e.getDelimiter()+f)},i.getPaths=function(){var a=this.getPath();return-1===a.indexOf(e.GLOBAL)&&a.push(e.GLOBAL),[this.getPath()]},i.apply=function(a,b){return e.adjust((b||this.getPath())+e.getDelimiter()+a)},i};e.GLOBAL="GLOBAL",e.DEFAULT_DELIMITER=".",e.DELIMITERS=["\\","/","_","-","."],e._delimiter=null,e.setDelimiter=function(a){if(!(a in this.DELIMITERS))throw new Error("Unsupported delimiter")},e.getDelimiter=function(){return this._delimiter||this.DEFAULT_DELIMITER},e.adjust=function(a){var b="[\\"+this.DELIMITERS.join("\\")+"]";return a.replace(new RegExp(b+"+","g"),this.getDelimiter()).replace(new RegExp("^"+b+"|"+b+"$"),"")};var f=function(){if(this.uid=++f.uid,"function"==typeof this.init){var a=this.init.apply(this,Array.prototype.slice.call(arguments));if("undefined"!=typeof a)return a}};f.NAME="__BASE_CLAZZ__",f.DEPENDENCIES=[],f.uid=0,f.parent=null,f.create=function(){var a=arguments;return new this(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],a[10])},f.prototype={parent:null,clazz:f};var g=function(a,b){this._clazzUID=0,this.BaseClazz=a,this.meta=b};g.prototype={DEFAULT_PROCESSORS:{clazz:["Clazz.Clazz"],proto:["Clazz.Proto"]},CLASS_NAME:"Clazz{uid}",create:function(a){var b=a.name||this.generateName(),c=a.parent,d=a.process||this.DEFAULT_PROCESSORS,e=a.meta,f=a.dependencies||[],g=this.createClazz(b,c);return g.DEPENDENCIES=f,"function"==typeof e&&(e=e.apply(g,f)),e&&this.applyMeta(g,e,d),g},createClazz:function(a,b){b||(b=this.BaseClazz);var d=function(){var a=b.apply(this,Array.prototype.slice.call(arguments));return"undefined"!=typeof a?a:void 0};for(var e in b)"function"==typeof b[e]?d[e]=b[e]:"_"===e[0]&&(d[e]=c);return d.NAME=a||this.generateName(),d.parent=b,d.prototype=Object.create(b.prototype),d.prototype.clazz=d,d.prototype.parent=b.prototype,d},applyMeta:function(a,b,c){var d={clazz:a,proto:a.prototype},e,f,g,h;for(var i in d)if(i in c)for(e=c[i],"[object Array]"!==Object.prototype.toString.call(e)&&(e=[e]),g=0,h=e.length;h>g;++g)f=e[g],"string"==typeof f&&(f=this.meta.processor(f)),f.process(d[i],b)},generateName:function(){return this.CLASS_NAME.replace("{uid}",++this._clazzUID)}};var h=function(){this._clazz={},this._meta={}};h.prototype={setMeta:function(a,b){return this._meta[a]=b,this},hasMeta:function(a){return a in this._meta},getMeta:function(a){if(!this.hasMeta(a))throw new Error('Meta does not exists for "'+a+'"!');return this._meta[a]},getClazz:function(a,b){var c,d,e,f,g,h;if(g=this._clazz[a],"[object Array]"===Object.prototype.toString.apply(g))for(b||(b=[]),c=0,d=g.length;d>c;++c){for(h=!0,e=0,f=g[c].DEPENDENCIES.length;f>e;++e)if(g[c].DEPENDENCIES[e]!==b[e]){h=!1;break}if(h)return g[c]}throw new Error('Clazz "'+a+'" does not exists!')},hasClazz:function(a,b){var c,d,e,f,g,h;if(g=this._clazz[a],"[object Array]"===Object.prototype.toString.apply(g)){if(!b)return!0;for(c=0,d=g.length;d>c;++c){for(h=!0,e=0,f=g[c].DEPENDENCIES.length;f>e;++e)if(g[c].DEPENDENCIES[e]!==b[e]){h=!1;break}if(h)return!0}}return!1},setClazz:function(a,b){if("function"==typeof a&&(b=a,a=b.NAME),"function"!=typeof b)throw new Error("Clazz must be a function!");return a in this._clazz||(this._clazz[a]=[]),this._clazz[a].push(b),this}},b.processor("Clazz.Clazz","Meta.Options",{options:{constants:"Clazz.Constants",clazz_properties:"Clazz.Properties",clazz_methods:"Clazz.Methods"}}),b.processor("Clazz.Constants","Meta.Chain",{processors:{init:"Clazz.Constants.Init","interface":"Clazz.Constants.Interface"}}),b.processor("Clazz.Constants.Init",function(a,b){a.__constants={};for(var c in b)a.__constants[c]=b[c]}),b.processor("Clazz.Constants.Interface","Meta.Interface",{"interface":{"const":function(a){return this.__getConstant(a)},__getConstant:function(a,b){var c=this;if("undefined"==typeof b&&(b=c.__getConstants()),"undefined"!=typeof a){if(!(a in b))throw new Error('Constant "'+a+'" does not defined!');if(b=b[a],"[object Object]"===Object.prototype.toString.apply(b))return function(a){return c.__getConstant(a,b)}}return b},__getConstants:function(){for(var a={},b=this;b;){if(b.hasOwnProperty("__constants"))for(var c in b.__constants)c in a||(a[c]=b.__constants[c]);b=b.parent}return a}}}),b.processor("Clazz.Methods",function(a,b){if("function"==typeof a&&a.parent)for(var c in a.parent)"function"==typeof a.parent[c]&&(a[c]=a.parent[c]);for(var c in b){if("function"!=typeof b[c])throw new Error('Method "'+c+'" must be a function!');a[c]=b[c]}}),b.processor("Clazz.Properties","Meta.Chain",{processors:{init:"Clazz.Properties.Init","interface":"Clazz.Properties.Interface",meta:"Clazz.Properties.Meta",defaults:"Clazz.Properties.Defaults"}}),b.processor("Clazz.Properties.Defaults",{DEFAULT:{hash:{},array:[]},process:function(a){var b,c,d,e=a.__properties;for(d in e)c=e[d]["default"],"undefined"==typeof c&&(b=e[d].type,"undefined"!=typeof b&&b in this.DEFAULT&&(c=this.DEFAULT[b])),a["_"+d]=this.__copy(c)}}),b.processor("Clazz.Properties.Init",function(a,b){for(var d in b)a["_"+d]=c}),b.processor("Clazz.Properties.Interface","Meta.Interface",{"interface":{__setters:{},__getters:{},__properties:{},init:function(a){this.__setData(a)},__setProperties:function(a){for(var b in a)this.__setProperty(b,a[b]);return this},__getProperties:function(){return this.__properties},__setProperty:function(a,b,c){if(a=this.__adjustPropertyName(a),"undefined"==typeof this.__properties[a]&&(this.__properties[a]={}),{}.constructor===b.constructor)for(var d in b)this.__properties[a][d]=b[d];else this.__properties[a][b]=c;return this},__getProperty:function(a,b){return"undefined"==typeof b?this.__properties[a]:this.__properties[a]&&this.__properties[a][b]},__hasProperty:function(a){return a=this.__adjustPropertyName(a),a in this.__properties},__adjustPropertyName:function(a){return a.replace(/(?:_)\w/,function(a){return a[1].toUpperCase()})},__setData:function(a){for(var b in a)this.__hasProperty(b)&&this.__setPropertyValue(b,a[b]);return this},__getPropertyValue:function(a){var b,c,d,e,f;if(a=this.__adjustPropertyName(a),!this.__hasProperty(a))throw new Error("Can't get! Property \""+a+'" does not exists!');f=this["_"+a],b=this.__getGetters(a);for(e in b)f=b[e].call(this,f);var g="[object Array]"===Object.prototype.toString.call(arguments[1])?arguments[1]:Array.prototype.slice.call(arguments,1);for(c=0,d=g.length;d>c;++c)f=f[g[c]];return f},__setPropertyValue:function(a){var b,c,d,e,f,g,h=arguments[arguments.length-1];if(a=this.__adjustPropertyName(a),!this.__hasProperty(a))throw new Error("Can't set! Property \""+a+'" does not exists!');if(f="[object Array]"===Object.prototype.toString.call(arguments[1])?arguments[1]:Array.prototype.slice.call(arguments,1,-1),f&&f.length){for(g=this["_"+a],c=0,d=f.length-1;d>c;++c)f[c]in g||(g[f[c]]={}),g=g[f[c]];g[f[c]]=h}else g=h;b=this.__getSetters(a);for(e in b)g=b[e].call(this,g);return this["_"+a]=g,this},__isPropertyValue:function(a){var b="[object Array]"===Object.prototype.toString.apply(arguments[1])?arguments[1]:Array.prototype.slice.call(arguments,1),c=this.__getPropertyValue(a,b),d=arguments[arguments.length-1];return"undefined"!=typeof c?c==d:!!c},__hasPropertyValue:function(a){var b="[object Array]"===Object.prototype.toString.apply(arguments[1])?arguments[1]:Array.prototype.slice.call(arguments,1),c=this.__getPropertyValue(a,b);if("[object Object]"===Object.prototype.toString.apply(c)){for(var d in c)return!0;return!1}return!("undefined"==typeof this[c]||null===c||"string"==typeof c&&""===c||"[object Array]"===Object.prototype.toString.apply(c)&&0===c.length)},__addSetter:function(a,b,c){if("undefined"==typeof c&&(c=b,b=0),"function"!=typeof c)throw new Error("Set callback must be a function!");return a in this.__setters||(this.__setters[a]=[]),this.__setters[a].push([b,c]),this},__getSetters:function(a){for(var b,c,d,e,f={},g=this.clazz.prototype;g;){if(g.hasOwnProperty("__setters"))for(e in g.__setters)e in f||(f[e]=g.__setters[e]);g=g.parent}if("undefined"!=typeof a){if(d=[],a in f&&f[a].length)for(f[a].sort(function(a,b){return b[0]-a[0]}),b=0,c=f[a].length;c>b;++b)d.push(f[a][b][1])}else d=f;return d},__addGetter:function(a,b,c){if("undefined"==typeof c&&(c=b,b=0),"function"!=typeof c)throw new Error("Get callback must be a function!");return a in this.__getters||(this.__getters[a]=[]),this.__getters[a].push([b,c]),this},__getGetters:function(a){for(var b,c,d,e,f={},g=this.clazz.prototype;g;){if(g.hasOwnProperty("__getters"))for(d in g.__getters)d in f||(f[d]=g.__getters[d]);g=g.parent}if("undefined"!=typeof a){if(e=[],a in f&&f[a].length)for(f[a].sort(function(a,b){return b[0]-a[0]}),b=0,c=f[a].length;c>b;++b)e.push(f[a][b][1])}else e=f;return e}}}),b.processor("Clazz.Properties.Meta",function(a,c){for(var d in c){var e=c[d];"[object Array]"===Object.prototype.toString.call(e)?c[d]=e=3===e.length?{type:[e[0],e[2]],"default":e[1]}:{type:e}:("object"!=typeof e||null===e)&&(c[d]=e={"default":e}),"methods"in e||(e.methods=["get","set","has","is"]),b.processor("Clazz.Property").process(a,e,d)}}),b.processor("Clazz.Property","Meta.Options",{options:{type:"Clazz.Property.Type","default":"Clazz.Property.Default",methods:"Clazz.Property.Methods",converters:"Clazz.Property.Converters",constraints:"Clazz.Property.Constraints"}}),b.processor("Clazz.Property.Constraints",function(a,b,c){a.__addSetter(c,function(a){for(var c in b)if(!b[c].call(this,a))throw new Error('Constraint "'+c+'" was failed!');return a})}),b.processor("Clazz.Property.Converters",function(a,b,c){a.__addSetter(c,1e3,function(a){for(var c in b)a=b[c].call(this,a);return a})}),b.processor("Clazz.Property.Default",function(a,b,c){"function"==typeof b&&(b=b()),a.__setProperty(c,"default",b)}),b.processor("Clazz.Property.Methods",{process:function(a,b,c){"[object Array]"!==Object.prototype.toString.apply(b)&&(b=[b]);for(var d=0,e=b.length;e>d;++d)this.addMethod(b[d],a,c)},addMethod:function(a,b,c){var d=this.createMethod(a,c);b[d.name]=d.body},createMethod:function(a,b){if(!(a in this.METHODS))throw new Error('Unsupported method "'+a+'"!');var c=this.METHODS[a](b);return"function"==typeof c&&(c={name:a+b[0].toUpperCase()+b.slice(1),body:c}),c},METHODS:{get:function(a){return function(){return this.__getPropertyValue.apply(this,[a].concat(Array.prototype.slice.call(arguments)))}},set:function(a){return function(b){return this.__setPropertyValue.apply(this,[a].concat(Array.prototype.slice.call(arguments)))}},is:function(a){return{name:(0!==a.indexOf("is")?"is":"")+a[0].toUpperCase()+a.slice(1),body:function(){return this.__isPropertyValue.apply(this,[a].concat(Array.prototype.slice.call(arguments)))}}},has:function(a){return function(){return this.__hasPropertyValue.apply(this,[a].concat(Array.prototype.slice.call(arguments)))}}}}),b.processor("Clazz.Property.Type",{process:function(a,b,c){var d=this,e={};if("[object Array]"===Object.prototype.toString.apply(b)&&(e=b[1]||[],b=b[0]),!(b in this.TYPES))throw new Error('Unsupported property type "'+b+'"!');a.__setProperty(c,"type",b),a.__addSetter(c,function(a){return d.checkValue(a,b,e,c)})},checkValue:function(a,b,c,d){return this.TYPES[b].call(this,a,c,d)},TYPES:{"boolean":function(a){return Boolean(a)},number:function(a,b){if(a=Number(a),"min"in b&&a<b.min)throw new Error('Value "'+a+'" must not be less then "'+b.min+'"!');if("max"in b&&a>b.max)throw new Error('Value "'+a+'" must not be greater then "'+b.max+'"!');return a},string:function(a,b){if(a=String(a),"pattern"in b&&!b.pattern.test(a))throw new Error('Value "'+a+'" does not match pattern "'+b.pattern+'"!');if("variants"in b&&-1===b.variants.indexOf(a))throw new Error('Value "'+a+'" must be one of "'+b.variants.join(", ")+'"!');return a},datetime:function(a){return a instanceof Date||(a=new Date(Date.parse(a))),a},object:function(a,b,c){if("object"!=typeof a||"[object Array]"===Object.prototype.toString.call(a))throw new Error('Incorrect value: not object type for property "'+c+'"!');if("instanceof"in b){var e=b.instanceof;if("[object Array]"===Object.prototype.toString.call(e)&&(e=d(e[0],e[1]||[])),!(a instanceof e))throw new Error('Value does not instance of clazz "'+e.NAME+'"!')}return a},array:function(a,b){return"string"==typeof a?a.split(b.delimiter||","):[].concat(a)},hash:function(a,b,c){if({}.constructor!==a.constructor)throw new Error('Incorrect value: not hash type for property "'+c+'"!');if("keys"in b)for(var d in a)if(!(d in b.keys))throw new Error('Unsupported hash key "'+d+'"!');return"element"in b&&this.checkValue.apply(this,[].concat(b.element)),a},clazz:function(a,b,c){if(!("function"==typeof a&&"NAME"in a&&"parent"in a))throw new Error('Incorrect value: not clazz type for property "'+c+'"!');return a}}}),b.processor("Clazz.Proto","Meta.Options",{options:{properties:"Clazz.Properties",methods:"Clazz.Methods"}}),function(a,b){var c=new g(f,b),i=new h,j=new e(i,c,null,"",a,d),k=j();a.namespace=j,a.clazz=k}(a,b)}(this,meta); | ||
!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")(),"ClazzJS",["MetaJS"],function(a,b){var c=a.meta,d=function(a,b,c){var e=function(a,b,c,d){var f=arguments[arguments.length-1];return"function"!=typeof f&&"[object Object]"!==Object.prototype.toString.call(f)?e.get(a,b):(e.set(a,b,c,d),void 0)};for(var f in d.prototype)e[f]=d.prototype[f];return e.getManager=function(){return a},e.getFactory=function(){return b},e.getNamespace=function(){return c},e};d.prototype={get:function(a,b){var c;if(c=this.resolveName(a),!c)throw new Error('Clazz with name "'+a+'" does not exits!');b=b||[];var d=this.getManager(),e=this.getFactory();if(!d.hasClazz(c,b)){var f=d.getMeta(c);d.setClazz(e.create({name:c,dependencies:b,process:f.process,parent:this.adjustParent(f.parent),meta:f.meta}))}return d.getClazz(c,b)},has:function(a){return!!this.resolveName(a)},set:function(a,b,c,d){var e=this.getNamespace(),f=this.getManager();return"object"==typeof a?(b=a.parent,c=a.process,d=a.meta,a=a.name):("undefined"==typeof d&&(d=c,c=null),"undefined"==typeof d&&(d=b,b=null),"[object Array]"===Object.prototype.toString.call(b)&&(c=b,b=null)),a=e.apply(a),f.setMeta(a,{parent:b,process:c,meta:d}),this},resolveName:function(a){var b,c,d,e,f=this.getManager(),g=this.getNamespace();for(b=g.getPaths(),d=0,e=b.length;e>d;++d)if(c=g.apply(a,b[d]),f.hasMeta(c))return c;return!1},adjustParent:function(a){return"string"==typeof a&&(a=[a]),"[object Array]"===Object.prototype.toString.call(a)&&(a=this.get(a[0],a[1]||[])),a}};var e=function(a,b,c,f,g,h){h=h||d;var i=function(c,d){var f=new e(a,b,i,c,g),j=new h(a,b,i);return d&&d(f,j),j};return i.getManager=function(){return a},i.getFactory=function(){return b},i.getBaseNamespace=function(){return c},i.getGlobal=function(){return g},i.getPath=function(){return e.adjust((c?c.getPath():e.GLOBAL)+e.getDelimiter()+f)},i.getPaths=function(){var a=this.getPath();return-1===a.indexOf(e.GLOBAL)&&a.push(e.GLOBAL),[this.getPath()]},i.apply=function(a,b){return e.adjust((b||this.getPath())+e.getDelimiter()+a)},i};e.GLOBAL="GLOBAL",e.DEFAULT_DELIMITER=".",e.DELIMITERS=["\\","/","_","-","."],e._delimiter=null,e.setDelimiter=function(a){if(!(a in this.DELIMITERS))throw new Error("Unsupported delimiter")},e.getDelimiter=function(){return this._delimiter||this.DEFAULT_DELIMITER},e.adjust=function(a){var b="[\\"+this.DELIMITERS.join("\\")+"]";return a.replace(new RegExp(b+"+","g"),this.getDelimiter()).replace(new RegExp("^"+b+"|"+b+"$"),"")};var f=function(){if(this.uid=++f.uid,"function"==typeof this.init){var a=this.init.apply(this,Array.prototype.slice.call(arguments));if("undefined"!=typeof a)return a}};f.NAME="__BASE_CLAZZ__",f.DEPENDENCIES=[],f.uid=0,f.parent=null,f.create=function(){var a=arguments;return new this(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],a[10])},f.prototype={parent:null,clazz:f};var g=function(a,b){this._clazzUID=0,this.BaseClazz=a,this.meta=b};g.prototype={DEFAULT_PROCESSORS:{clazz:["Clazz.Clazz"],proto:["Clazz.Proto"]},CLASS_NAME:"Clazz{uid}",create:function(a){var b=a.name||this.generateName(),c=a.parent,d=a.process||this.DEFAULT_PROCESSORS,e=a.meta,f=a.dependencies||[],g=this.createClazz(b,c);return g.DEPENDENCIES=f,"function"==typeof e&&(e=e.apply(g,f)),e&&this.applyMeta(g,e,d),g},createClazz:function(a,c){c||(c=this.BaseClazz);var d=function(){var a=c.apply(this,Array.prototype.slice.call(arguments));return"undefined"!=typeof a?a:void 0};for(var e in c)"function"==typeof c[e]?d[e]=c[e]:"_"===e[0]&&(d[e]=b);return d.NAME=a||this.generateName(),d.parent=c,d.prototype=Object.create(c.prototype),d.prototype.clazz=d,d.prototype.parent=c.prototype,d},applyMeta:function(a,b,c){var d={clazz:a,proto:a.prototype},e,f,g,h;for(var i in d)if(i in c)for(e=c[i],"[object Array]"!==Object.prototype.toString.call(e)&&(e=[e]),g=0,h=e.length;h>g;++g)f=e[g],"string"==typeof f&&(f=this.meta.processor(f)),f.process(d[i],b)},generateName:function(){return this.CLASS_NAME.replace("{uid}",++this._clazzUID)}};var h=function(){this._clazz={},this._meta={}};h.prototype={setMeta:function(a,b){return this._meta[a]=b,this},hasMeta:function(a){return a in this._meta},getMeta:function(a){if(!this.hasMeta(a))throw new Error('Meta does not exists for "'+a+'"!');return this._meta[a]},getClazz:function(a,b){var c,d,e,f,g,h;if(g=this._clazz[a],"[object Array]"===Object.prototype.toString.apply(g))for(b||(b=[]),c=0,d=g.length;d>c;++c){for(h=!0,e=0,f=g[c].DEPENDENCIES.length;f>e;++e)if(g[c].DEPENDENCIES[e]!==b[e]){h=!1;break}if(h)return g[c]}throw new Error('Clazz "'+a+'" does not exists!')},hasClazz:function(a,b){var c,d,e,f,g,h;if(g=this._clazz[a],"[object Array]"===Object.prototype.toString.apply(g)){if(!b)return!0;for(c=0,d=g.length;d>c;++c){for(h=!0,e=0,f=g[c].DEPENDENCIES.length;f>e;++e)if(g[c].DEPENDENCIES[e]!==b[e]){h=!1;break}if(h)return!0}}return!1},setClazz:function(a,b){if("function"==typeof a&&(b=a,a=b.NAME),"function"!=typeof b)throw new Error("Clazz must be a function!");return a in this._clazz||(this._clazz[a]=[]),this._clazz[a].push(b),this}},c.processor("Clazz.Clazz","Meta.Options",{options:{constants:"Clazz.Constants",clazz_properties:"Clazz.Properties",clazz_methods:"Clazz.Methods"}}),c.processor("Clazz.Constants","Meta.Chain",{processors:{init:"Clazz.Constants.Init","interface":"Clazz.Constants.Interface"}}),c.processor("Clazz.Constants.Init",function(a,b){a.__constants={};for(var c in b)a.__constants[c]=b[c]}),c.processor("Clazz.Constants.Interface","Meta.Interface",{"interface":{"const":function(a){return this.__getConstant(a)},__getConstant:function(a,b){var c=this;if("undefined"==typeof b&&(b=c.__getConstants()),"undefined"!=typeof a){if(!(a in b))throw new Error('Constant "'+a+'" does not defined!');if(b=b[a],"[object Object]"===Object.prototype.toString.apply(b))return function(a){return c.__getConstant(a,b)}}return b},__getConstants:function(){for(var a={},b=this;b;){if(b.hasOwnProperty("__constants"))for(var c in b.__constants)c in a||(a[c]=b.__constants[c]);b=b.parent}return a}}}),c.processor("Clazz.Methods",function(a,b){if("function"==typeof a&&a.parent)for(var c in a.parent)"function"==typeof a.parent[c]&&(a[c]=a.parent[c]);for(var c in b){if("function"!=typeof b[c])throw new Error('Method "'+c+'" must be a function!');a[c]=b[c]}}),c.processor("Clazz.Properties","Meta.Chain",{processors:{init:"Clazz.Properties.Init","interface":"Clazz.Properties.Interface",meta:"Clazz.Properties.Meta",defaults:"Clazz.Properties.Defaults"}}),c.processor("Clazz.Properties.Defaults",{DEFAULT:{hash:{},array:[]},process:function(a){var b,c,d,e=a.__properties;for(d in e)c=e[d]["default"],"undefined"==typeof c&&(b=e[d].type,"undefined"!=typeof b&&b in this.DEFAULT&&(c=this.DEFAULT[b])),a["_"+d]=this.__copy(c)}}),c.processor("Clazz.Properties.Init",function(a,c){for(var d in c)a["_"+d]=b}),c.processor("Clazz.Properties.Interface","Meta.Interface",{"interface":{__setters:{},__getters:{},__properties:{},init:function(a){this.__setData(a)},__setProperties:function(a){for(var b in a)this.__setProperty(b,a[b]);return this},__getProperties:function(){for(var a=this,b={};a;){if(a.__properties)for(var c in a.__properties)c in b||(b[c]=a.__properties[c]);a=a.parent}return b},__setProperty:function(a,b,c){if(a=this.__adjustPropertyName(a),"undefined"==typeof this.__properties[a]&&(this.__properties[a]={}),{}.constructor===b.constructor)for(var d in b)this.__properties[a][d]=b[d];else this.__properties[a][b]=c;return this},__getProperty:function(a,b){var c=this.__getProperties();return"undefined"==typeof b?c[a]:c[a]&&c[a][b]},__hasProperty:function(a){return a=this.__adjustPropertyName(a),a in this.__getProperties()},__adjustPropertyName:function(a){return a.replace(/(?:_)\w/,function(a){return a[1].toUpperCase()})},__setData:function(a){for(var b in a)this.__hasProperty(b)&&this.__setPropertyValue(b,a[b]);return this},__getPropertyValue:function(a){var b,c,d,e,f;if(a=this.__adjustPropertyName(a),!this.__hasProperty(a))throw new Error("Can't get! Property \""+a+'" does not exists!');f=this["_"+a],b=this.__getGetters(a);for(e in b)f=b[e].call(this,f);var g="[object Array]"===Object.prototype.toString.call(arguments[1])?arguments[1]:Array.prototype.slice.call(arguments,1);for(c=0,d=g.length;d>c;++c)f=f[g[c]];return f},__setPropertyValue:function(a){var b,c,d,e,f,g,h=arguments[arguments.length-1];if(a=this.__adjustPropertyName(a),!this.__hasProperty(a))throw new Error("Can't set! Property \""+a+'" does not exists!');if(f="[object Array]"===Object.prototype.toString.call(arguments[1])?arguments[1]:Array.prototype.slice.call(arguments,1,-1),f&&f.length){for(g=this["_"+a],c=0,d=f.length-1;d>c;++c)f[c]in g||(g[f[c]]={}),g=g[f[c]];g[f[c]]=h}else g=h;b=this.__getSetters(a);for(e in b)g=b[e].call(this,g);return this["_"+a]=g,this},__isPropertyValue:function(a){var b="[object Array]"===Object.prototype.toString.apply(arguments[1])?arguments[1]:Array.prototype.slice.call(arguments,1),c=this.__getPropertyValue(a,b),d=arguments[arguments.length-1];return"undefined"!=typeof c?c==d:!!c},__hasPropertyValue:function(a){var b="[object Array]"===Object.prototype.toString.apply(arguments[1])?arguments[1]:Array.prototype.slice.call(arguments,1),c=this.__getPropertyValue(a,b);if("[object Object]"===Object.prototype.toString.apply(c)){for(var d in c)return!0;return!1}return!("undefined"==typeof this[c]||null===c||"string"==typeof c&&""===c||"[object Array]"===Object.prototype.toString.apply(c)&&0===c.length)},__addSetter:function(a,b,c){if("undefined"==typeof c&&(c=b,b=0),"function"!=typeof c)throw new Error("Set callback must be a function!");return a in this.__setters||(this.__setters[a]=[]),this.__setters[a].push([b,c]),this},__getSetters:function(a){for(var b,c,d,e,f={},g=this.clazz.prototype;g;){if(g.hasOwnProperty("__setters"))for(e in g.__setters)e in f||(f[e]=g.__setters[e]);g=g.parent}if("undefined"!=typeof a){if(d=[],a in f&&f[a].length)for(f[a].sort(function(a,b){return b[0]-a[0]}),b=0,c=f[a].length;c>b;++b)d.push(f[a][b][1])}else d=f;return d},__addGetter:function(a,b,c){if("undefined"==typeof c&&(c=b,b=0),"function"!=typeof c)throw new Error("Get callback must be a function!");return a in this.__getters||(this.__getters[a]=[]),this.__getters[a].push([b,c]),this},__getGetters:function(a){for(var b,c,d,e,f={},g=this.clazz.prototype;g;){if(g.hasOwnProperty("__getters"))for(d in g.__getters)d in f||(f[d]=g.__getters[d]);g=g.parent}if("undefined"!=typeof a){if(e=[],a in f&&f[a].length)for(f[a].sort(function(a,b){return b[0]-a[0]}),b=0,c=f[a].length;c>b;++b)e.push(f[a][b][1])}else e=f;return e}}}),c.processor("Clazz.Properties.Meta",function(a,b){for(var d in b){var e=b[d];"[object Array]"===Object.prototype.toString.call(e)?b[d]=e=3===e.length?{type:[e[0],e[2]],"default":e[1]}:{type:e}:("object"!=typeof e||null===e)&&(b[d]=e={"default":e}),"methods"in e||(e.methods=["get","set","has","is"]),c.processor("Clazz.Property").process(a,e,d)}}),c.processor("Clazz.Property","Meta.Options",{options:{type:"Clazz.Property.Type","default":"Clazz.Property.Default",methods:"Clazz.Property.Methods",converters:"Clazz.Property.Converters",constraints:"Clazz.Property.Constraints"}}),c.processor("Clazz.Property.Constraints",function(a,b,c){a.__addSetter(c,function(a){for(var c in b)if(!b[c].call(this,a))throw new Error('Constraint "'+c+'" was failed!');return a})}),c.processor("Clazz.Property.Converters",function(a,b,c){a.__addSetter(c,1e3,function(a){for(var c in b)a=b[c].call(this,a);return a})}),c.processor("Clazz.Property.Default",function(a,b,c){"function"==typeof b&&(b=b()),a.__setProperty(c,"default",b)}),c.processor("Clazz.Property.Methods",{process:function(a,b,c){"[object Array]"!==Object.prototype.toString.apply(b)&&(b=[b]);for(var d=0,e=b.length;e>d;++d)this.addMethod(b[d],a,c)},addMethod:function(a,b,c){var d=this.createMethod(a,c);b[d.name]=d.body},createMethod:function(a,b){if(!(a in this.METHODS))throw new Error('Unsupported method "'+a+'"!');var c=this.METHODS[a](b);return"function"==typeof c&&(c={name:a+b[0].toUpperCase()+b.slice(1),body:c}),c},METHODS:{get:function(a){return function(){return this.__getPropertyValue.apply(this,[a].concat(Array.prototype.slice.call(arguments)))}},set:function(a){return function(b){return this.__setPropertyValue.apply(this,[a].concat(Array.prototype.slice.call(arguments)))}},is:function(a){return{name:(0!==a.indexOf("is")?"is"+a[0].toUpperCase():""+a[0])+a.slice(1),body:function(){return this.__isPropertyValue.apply(this,[a].concat(Array.prototype.slice.call(arguments)))}}},has:function(a){return function(){return this.__hasPropertyValue.apply(this,[a].concat(Array.prototype.slice.call(arguments)))}}}}),c.processor("Clazz.Property.Type",{process:function(a,b,c){var d=this,e={};if("[object Array]"===Object.prototype.toString.apply(b)&&(e=b[1]||[],b=b[0]),!(b in this.TYPES))throw new Error('Unsupported property type "'+b+'"!');a.__setProperty(c,"type",b),a.__addSetter(c,function(a){return d.checkValue(a,b,e,c)})},checkValue:function(a,b,c,d){return this.TYPES[b].call(this,a,c,d)},TYPES:{"boolean":function(a){return Boolean(a)},number:function(a,b){if(a=Number(a),"min"in b&&a<b.min)throw new Error('Value "'+a+'" must not be less then "'+b.min+'"!');if("max"in b&&a>b.max)throw new Error('Value "'+a+'" must not be greater then "'+b.max+'"!');return a},string:function(a,b){if(a=String(a),"pattern"in b&&!b.pattern.test(a))throw new Error('Value "'+a+'" does not match pattern "'+b.pattern+'"!');if("variants"in b&&-1===b.variants.indexOf(a))throw new Error('Value "'+a+'" must be one of "'+b.variants.join(", ")+'"!');return a},datetime:function(a){return a instanceof Date||(a=new Date(Date.parse(a))),a},object:function(a,b,c){if("object"!=typeof a||"[object Array]"===Object.prototype.toString.call(a))throw new Error('Incorrect value: not object type for property "'+c+'"!');if("instanceof"in b){var d=b.instanceof;if("[object Array]"===Object.prototype.toString.call(d)&&(d=l(d[0],d[1]||[])),!(a instanceof d))throw new Error('Value does not instance of clazz "'+d.NAME+'"!')}return a},array:function(a,b){return"string"==typeof a?a.split(b.delimiter||","):[].concat(a)},hash:function(a,b,c){if({}.constructor!==a.constructor)throw new Error('Incorrect value: not hash type for property "'+c+'"!');if("keys"in b)for(var d in a)if(!(d in b.keys))throw new Error('Unsupported hash key "'+d+'"!');return"element"in b&&this.checkValue.apply(this,[].concat(b.element)),a},clazz:function(a,b,c){if(!("function"==typeof a&&"NAME"in a&&"parent"in a))throw new Error('Incorrect value: not clazz type for property "'+c+'"!');return a}}}),c.processor("Clazz.Proto","Meta.Options",{options:{properties:"Clazz.Properties",methods:"Clazz.Methods"}});var i=new g(f,c),j=new h,k=new e(j,i,null,"",new Function("return this")(),d),l=k();return{Base:f,Mangaer:h,Namespace:e,Clazz:d,namespace:k,clazz:l}}); | ||
//# sourceMappingURL=dist/ClazzJS.min.map |
{ | ||
"name": "clazz-js", | ||
"title": "ClazzJS", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Portable JavaScript library for class-style OOP programming", | ||
@@ -11,2 +11,3 @@ "author": { | ||
}, | ||
"main": "dist/ClazzJS.js", | ||
"keywords": [ | ||
@@ -24,2 +25,5 @@ "oop", | ||
}, | ||
"dependencies": { | ||
"meta-js": "~0.3.1" | ||
}, | ||
"devDependencies": { | ||
@@ -26,0 +30,0 @@ "grunt": "~0.4.1", |
@@ -34,7 +34,8 @@ var Clazz = function(manager, factory, namespace) { | ||
Clazz.prototype = { | ||
get: function(name, dependencies) { | ||
get: function(originName, dependencies) { | ||
var name; | ||
name = this.resolveName(name); | ||
name = this.resolveName(originName); | ||
if (!name) { | ||
throw new Error('Clazz with name "' + name + '" does not exits!'); | ||
throw new Error('Clazz with name "' + originName + '" does not exits!'); | ||
} | ||
@@ -41,0 +42,0 @@ |
@@ -22,3 +22,18 @@ meta.processor('Clazz.Properties.Interface', 'Meta.Interface', { | ||
__getProperties: function() { | ||
return this.__properties; | ||
var parent = this; | ||
var properties = {}; | ||
while (parent) { | ||
if (parent.__properties) { | ||
for (var property in parent.__properties) { | ||
if (property in properties) { | ||
continue; | ||
} | ||
properties[property] = parent.__properties[property]; | ||
} | ||
} | ||
parent = parent.parent; | ||
} | ||
return properties; | ||
}, | ||
@@ -45,5 +60,7 @@ | ||
__getProperty: function(property, key) { | ||
var properties = this.__getProperties(); | ||
return typeof key === 'undefined' | ||
? this.__properties[property] | ||
: this.__properties[property] && this.__properties[property][key]; | ||
? properties[property] | ||
: properties[property] && properties[property][key]; | ||
}, | ||
@@ -54,3 +71,3 @@ | ||
return property in this.__properties; | ||
return property in this.__getProperties(); | ||
}, | ||
@@ -57,0 +74,0 @@ |
@@ -45,3 +45,3 @@ meta.processor('Clazz.Property.Methods', { | ||
return { | ||
name: (0 !== property.indexOf('is') ? 'is' : '') + property[0].toUpperCase() + property.slice(1), | ||
name: (0 !== property.indexOf('is') ? 'is' + property[0].toUpperCase() : '' + property[0]) + property.slice(1), | ||
body: function() { | ||
@@ -48,0 +48,0 @@ return this.__isPropertyValue.apply(this, [property].concat(Array.prototype.slice.call(arguments))); |
@@ -60,9 +60,9 @@ meta.processor('Clazz.Property.Type', { | ||
if ('instanceof' in params) { | ||
var clazz = params.instanceof; | ||
var clazzInstance = params.instanceof; | ||
if (Object.prototype.toString.call(clazz) === '[object Array]') { | ||
clazz = Clazz(clazz[0], clazz[1] || []); | ||
if (Object.prototype.toString.call(clazzInstance) === '[object Array]') { | ||
clazzInstance = clazz(clazzInstance[0], clazzInstance[1] || []); | ||
} | ||
if (!(value instanceof clazz)) { | ||
throw new Error('Value does not instance of clazz "' + clazz.NAME + '"!'); | ||
if (!(value instanceof clazzInstance)) { | ||
throw new Error('Value does not instance of clazz "' + clazzInstance.NAME + '"!'); | ||
} | ||
@@ -69,0 +69,0 @@ } |
@@ -6,2 +6,2 @@ meta.processor('Clazz.Proto', 'Meta.Options', { | ||
} | ||
}) | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
86824
1856
1
3
2
4
+ Addedmeta-js@~0.3.1