From d25a7ae4577bc158690a27fe5f01af056216cfab Mon Sep 17 00:00:00 2001 From: Aran Deltac Date: Mon, 5 Dec 2016 19:03:05 -0800 Subject: [PATCH] Release 0.2.0. --- CHANGES.txt | 2 +- bower.json | 2 +- o.js | 2 +- o.min.js | 2 +- package.json | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 6b17d96..93831e1 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,4 @@ -DEVELOPMENT VERSION +0.2.0 : 2016-12-05 BREAKING: - Attributes are now installed as properties instead of functions. This means diff --git a/bower.json b/bower.json index 2850ed3..2aee860 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "o", - "version": "0.1.0", + "version": "0.2.0", "main": "o.js", "description": "JavaScript utility belt for rich objects and prototypes. Ooooh!", diff --git a/o.js b/o.js index 85fe3cd..a2acd2c 100644 --- a/o.js +++ b/o.js @@ -1,4 +1,4 @@ -// o.js : DEVELOPMENT VERSION : https://o-js.com : MIT License +// o.js : 0.2.0 : https://o-js.com : MIT License (function() { 'use strict'; diff --git a/o.min.js b/o.min.js index e643cf5..f65f063 100644 --- a/o.min.js +++ b/o.min.js @@ -1,2 +1,2 @@ -// o.js : DEVELOPMENT VERSION : https://o-js.com : MIT License +// o.js : 0.2.0 : https://o-js.com : MIT License (function(){"use strict";var a=this,b=a.o,c={},d=c.clearer=function(a){return function(){delete this[a]}},e=c.getPrototypeOf=function(a){return Object.getPrototypeOf?Object.getPrototypeOf(a):Object.__proto__},f=c.has=function(a,b){return Object.prototype.hasOwnProperty.call(a,b)},g=c.local=function(a,b,c){var d,e=f(a,b),g=a[b];try{d=c()}finally{e?a[b]=g:delete a[b]}return d},h=c.merge=function(){for(var a=Array.prototype.slice.call(arguments),b=a.shift();a.length;){var c=a.shift();for(var d in c)f(c,d)&&(b[d]=c[d])}return b},i=c.predicate=function(a){return function(){return f(this,a)&&void 0!==this[a]}},j=c.ucFirst=function(a){return a.charAt(0).toUpperCase()+a.slice(1)},k=c.writer=function(a,b){return b=b||{},b.augments&&(b.type=b.type||"object"),function(c){if(b.filter&&(c=b.filter.call(this,c)),b.type)if("string"==typeof b.type||b.type instanceof String){if(typeof c!==b.type)throw new Error(c+" failed validation.")}else if(b.type instanceof Function){if(!b.type(c))throw new Error(c+" failed validation.")}else b.type.coerce instanceof Function&&b.type.validate instanceof Function&&(b.coerce?c=b.type.coerce(c):b.type.validate(c));if(b.augments&&!(c instanceof b.augments))throw new Error(c+" failed validation.");this[a]=c}},l=c.after=function(a,b){return function(){var c=a.apply(this,arguments);return b.call(this),c}},m=c.around=function(a,b){return function(){var c=Array.prototype.slice.call(arguments),d=a.bind(this);return c.unshift(d),b.apply(this,c)}},n=c.before=function(a,b){return function(){return b.call(this),a.apply(this,arguments)}},o=c.clone=function(a){var b=Object.create(e(a));return b.constructor=a.constructor,h(b,a),b},p=c.construct=function(a,b){return h(a.prototype,b),a},q=c.prependIdentifier=function(){var a=/^_/;return function(b,c){var d=a.test(c);return d||(d=a.test(b)),c=c.replace(a,""),b=b.replace(a,""),b&&(c=j(c)),c=b+c,d&&(c="_"+c),c}}(),r=c.reader=function(a,b){return b=b||{},b.writer=b.writer||k(a,b),b.predicate=b.predicate||i(a),function(){if(!b.predicate.call(this)){if(b.required)throw new Error(a+" is required.");if(b.devoid){var c=b.devoid;c instanceof Function&&(c=c.call(this)),b.writer.call(this,c)}else b.builder&&b.writer.call(this,this[b.builder].call(this))}return this[a]}},s=c.augment=function(a,b,c){var d=m(a,b),e=Object.create(a.prototype);return c&&h(e,c),d.prototype=e,d.prototype.constructor=a,d},t=c.TypeValidationError=function(){return s(Error,function(a,b,c){this.name="o.TypeValidationError",this.message=b.message(c),this.stack=new Error(this.message).stack,this.type=b,this.value=c})}(),u=c.Type=function(){var a=p(function(a){a=a||{},"function"==typeof a&&(a={validate:a}),a.validate&&(this._validateMethod=a.validate),a.coerce&&(this._coerceMethod=a.coerce),a.parent&&(this._parent=a.parent),this._name=a.name||"unnamedType",a.message&&(this._message=a.message)},{check:function(b){return a.validationDisabled?!0:this._parent&&!this._parent.check(b)?!1:this._validateMethod?this._validateMethod(b)?!0:!1:!0},validate:function(b){if(!a.validationDisabled){if(this._parent&&this._parent.validate(b),!this._validateMethod)return!0;if(!this._validateMethod(b))throw new t(this,b)}},coerce:function(a){return a=this.coerceOnly(a),this.validate(a),a},coerceOnly:function(a){return this._parent&&(a=this._parent.coerceOnly(a)),this._coerceMethod&&(a=this._coerceMethod(a)),a},subtype:function(b){return"function"==typeof b&&(b={validate:b}),new a(h({parent:this},b))},message:function(a){return this._message?this._message(a):a+" failed "+this.name+" validation."}});return Object.defineProperties(a.prototype,{name:{get:function(){return this._name}}}),a.validationDisabled=!1,a}(),v=c.TypeOfType=function(){return s(u,function(a,b,c){c=c||{},c.validate||(c.validate=function(a){return typeof a===b?!0:!1}),c.name||(c.name="o.TypeOfType"),c.message||(c.message=function(a){return a+" failed "+this.name+" validation due to not being a typeof"+b+"."}),a(c)})}(),w=(c.disableTypeValidation=function(a){return a?g(u,"validationDisabled",function(){return u.validationDisabled=!0,a()}):void(u.validationDisabled=!0)},c.AllType=function(){return s(u,function(a,b,c){c=c||{},c.validate||(c.validate=function(a){for(var c=0,d=b.length;d>c;c++)if(!b[c].check(a))return!1;return!0}),c.name||(c.name="o.AllType"),c.message||(c.message=function(a){return a+" failed "+this.name+" validation due to not passing all of the ["+b.map(function(a){return a.name}).join(",")+"] type checks."}),a(c)})}()),x=c.AnyType=function(){return s(u,function(a,b,c){c=c||{},c.validate||(c.validate=function(a){for(var c=0,d=b.length;d>c;c++)if(b[c].check(a))return!0;return!1}),c.name||(c.name="o.AnyType"),c.message||(c.message=function(a){return a+" failed "+this.name+" validation due to not passing any of the ["+b.map(function(a){return a.name}).join(",")+"] type checks."}),a(c)})}(),y=c.EnumType=function(){return s(u,function(a,b,c){c=c||{},c.validate||(c.validate=function(a){for(var c=0,d=b.length;d>c;c++)if(a===b[c])return!0;return!1}),c.name||(c.name="o.EnumType"),c.message||(c.message=function(a){return a+" failed "+this.name+" validation due to not being one of ["+b.join(",")+"]."}),a(c)})}(),z=c.EqualType=function(){return s(u,function(a,b,c){c=c||{},c.validate||(c.validate=function(a){return a===b?!0:!1}),c.name||(c.name="o.EqualType"),c.message||(c.message=function(a){return a+" failed "+this.name+" validation due to not equalling "+b+"."}),a(c)})}(),A=c.InstanceOfType=function(){return s(u,function(a,b,c){c=c||{},c.validate||(c.validate=function(a){return a instanceof b?!0:!1}),c.coerce||(c.coerce=function(a){return this.check(a)?a:new b(a)}),c.name||(c.name="o.InstanceOfType"),c.message||(c.message=function(a){return a+" failed "+this.name+" validation due to not being an instanceof "+(b.name||b)+"."}),a(c)})}(),B=c.LazyType=function(){return s(u,function(a,b,c){c=c||{};var d;c.validate||(c.validate=function(a){return d=d||b(),d.check(a)}),c.coerce||(c.coerce=function(a){return d=d||b(),d.coerce(a)}),c.name||(c.name="o.LazyType"),c.message||(c.message=function(a){return d=d||b(),d.message(a)}),a(c)})}(),C=c.NoneType=function(){return s(u,function(a,b,c){c=c||{};var d=new x(b);c.validate||(c.validate=function(a){return d.check(a)?!1:!0}),c.name||(c.name="o.NoneType"),c.message||(c.message=function(a){return a+" failed "+this.name+" validation due to passing one of the ["+b.map(function(a){return a.name}).join(",")+"] type checks."}),a(c)})}(),D=c.NotType=function(){return s(C,function(a,b,c){c=c||{},c.name||(c.name="o.NotType"),a([b],c)})}(),E=c.arrayType=function(){return new A(Array,{name:"o.arrayType"})}(),F=c.booleanType=function(){return new x([new v("boolean"),new A(Boolean)],{name:"o.booleanType"})}(),G=(c.dateType=function(){return new A(Date,{name:"o.dateType"})}(),c.functionType=function(){return new A(Function,{name:"o.functionType"})}()),H=c.nullType=function(){return new z(null,{name:"o.nullType"})}(),I=c.numberType=function(){return new x([new v("number"),new A(Number)],{name:"o.numberType"})}(),J=c.objectType=function(){return new A(Object,{name:"o.objectType"})}(),K=c.positiveType=function(){return I.subtype({name:"o.positiveType",validate:function(a){return a>0?!0:!1}})}(),L=(c.regExpType=function(){return new A(RegExp,{name:"o.regExpType"})}(),c.simpleObjectType=function(){return J.subtype({name:"o.simpleObjectType",validate:function(a){return a.constructor===Object?!0:!1}})}()),M=c.stringType=function(){return new x([new v("string"),new A(String)],{name:"o.stringType"})}(),N=c.typeType=function(){return new A(u,{name:"o.typeType"})}(),O=c.undefinedType=function(){return new z(void 0,{name:"o.undefinedType"})}(),P=c.ArrayOfType=function(){return s(u,function(a,b,c){c=c||{},c.parent||(c.parent=E),c.validate||(c.validate=function(a){for(var c=0,d=a.length;d>c;c++)if(!b.check(a[c]))return!1;return!0}),c.coerce||(c.coerce=function(a){if(!this.check(a))return a;for(var c=0,d=a.length;d>c;c++)a[c]=b.coerce(a[c]);return a}),c.name||(c.name="o.ArrayOfType"),c.message||(c.message=function(a){return a+" failed "+this.name+" validation due to one of the array values not passing the "+b.name+" type check."}),a(c)})}(),Q=c.DuckType=function(){return s(u,function(a,b,c){c=c||{},c.parent||(c.parent=J),c.validate||(c.validate=function(a){if(E.check(b)){for(var c=0,d=b.length;d>c;c++)if(void 0===a[b[c]])return!1;return!0}if(L.check(b)){for(var e in b){if(void 0===a[e])return!1;if(!b[e].check(a[e]))return!1}return!0}return!1}),c.name||(c.name="o.DuckType"),c.message||(c.message=function(a){var c=[];if(c.push(a+" failed "+this.name+" validation due to "),E.check(b))c.push("not having the ["+b.join(",")+"] properties set");else if(L.check(b)){c.push("not having the {");var d=[];for(var e in b)d.push(e+":"+b[e].name);c.push(d.join(",")),c.push("} properties set")}return c.join("")+"."}),a(c)})}(),R=c.ObjectOfType=function(){return s(u,function(a,b,c){c=c||{},c.parent||(c.parent=J),c.validate||(c.validate=function(a){for(var c in a)if(!b.check(a[c]))return!1;return!0}),c.coerce||(c.coerce=function(a){if(!this.check(a))return a;for(var c in a)a[c]=b.coerce(a[c]);return a}),c.name||(c.name="o.ObjectOfType"),c.message||(c.message=function(a){return a+" failed "+this.name+" validation due to one of the object values not passing the "+b.name+" type check."}),a(c)})}(),S=c.PatternType=function(){return s(u,function(a,b,c){c=c||{},c.validate||(c.validate=function(a){return M.check(a)?b.test(a):!1}),c.name||(c.name="o.PatternType"),c.message||(c.message=function(a){return a+" failed "+this.name+" validation due to not matching "+b}),a(c)})}(),T=(c.TupleType=function(){return s(u,function(a,b,c){c=c||{},c.validate||(c.validate=function(a){if(!E.check(a))return!1;if(a.length!=b.length)return!1;for(var c in a){if(!b[c])return!1;if(!b[c].check(a[c]))return!1}return!0}),c.coerce||(c.coerce=function(a){if(!this.check(a))return a;for(var c in a)b[c]&&(a[c]=b[c].coerce(a[c]));return a}),c.name||(c.name="o.TupleType"),c.message||(c.message=function(a){return a+" failed "+this.name+" validation due to one of the array values not passing the ["+b.map(function(a){return a.name}).join(",")+"] tuple of type checks."}),a(c)})}(),c.definedType=function(){return new D(O,{name:"o.NotType"})}()),U=c.identifierType=function(){return new S(/^[A-Za-z_$][A-Za-z_$0-9]*$/,{name:"o.identifierType"})}(),V=c.integerType=function(){return I.subtype({name:"o.integerType",validate:function(a){return Math.floor(a)===a+0?!0:!1}})}(),W=(c.negativeType=function(){return I.subtype({name:"o.negativeType",validate:function(a){return 0>a?!0:!1}})}(),c.nonEmptyStringType=function(){return M.subtype({name:"o.nonEmptyStringType",validate:function(a){return a.length>0?!0:!1}})}(),c.nonZeroType=function(){return I.subtype({name:"o.nonZeroType",validate:function(a){return 0!==a?!0:!1}})}(),c.positiveIntType=function(){return new w([V,K],{name:"o.positiveIntType"})}(),c.Attribute=function(){var a,b=new x([F,U]),e=new B(function(){return new A(c.Trait)}),f=new B(function(){return new P(e)}),g={key:{type:U,required:!0},argKey:{type:new x([U,H]),devoid:function(){return this.key}},valueKey:{type:U,devoid:function(){return"__"+this.key}},is:{type:e,filter:function(a){return e.check(a)?a:c[a+"AttributeTrait"]}},traits:{type:f,devoid:function(){return[]}},devoid:{type:T},builder:{type:b,filter:function(a){return a===!0&&(a=q("_build",this.key)),a}},required:{type:F},type:{type:new x([new y(["undefined","object","boolean","number","string","function"]),N,G])},coerce:{type:F},filter:{type:G},augments:{type:G},reader:{type:b,devoid:!0,filter:function(a){return a===!0&&(a=this.key),a}},writer:{type:b,filter:function(a){return a===!0&&(a=this.key),a}},predicate:{type:b,filter:function(a){return a===!0&&(a=q("has",this.key)),a}},clearer:{type:b,filter:function(a){return a===!0&&(a=q("clear",this.key)),a}},proxies:{type:new R(U),devoid:function(){return{}}},getMethodName:{type:U,devoid:function(){return q("_get",this.key)}},setMethodName:{type:U,devoid:function(){return q("_set",this.key)}},predicateGetMethodName:{type:U,devoid:function(){return q("_predicateGet",this.key)}}},j={getMethod:{type:G,devoid:function(){return r(this.valueKey,{devoid:this.devoid,builder:this.builder,required:this.required,writer:this.setMethod,predicate:this.predicateMethod})}},propGetMethod:{type:G,devoid:function(){var a=this.getMethodName;return function(){return this[a]()}}},setMethod:{type:G,devoid:function(){return k(this.valueKey,{type:this.type,coerce:this.coerce,filter:this.filter,augments:this.augments})}},propSetMethod:{type:G,devoid:function(){var a=this.setMethodName;return function(b){this[a](b)}}},predicateGetMethod:{type:G,devoid:function(){return i(this.valueKey)}},propPredicateGetMethod:{type:G,devoid:function(){var a=this.predicateGetMethodName;return function(){return this[a]()}}},clearerMethod:{type:G,devoid:function(){return d(this.valueKey)}},proxyProperties:{type:c.simpleObjectType,devoid:function(){var a={},b=this.proxies;if(void 0===b)return a;var c=this;for(var d in b)a[d]=function(a){return{get:function(){var b=c.getValue(this),d=b[a];return"function"!=typeof d?d:d.bind(b)},set:function(b){var d=c.getValue(this);d[a]=b}}}(b[d]);return a}}},l={},m={};for(a in g)l[a]=k("_"+a,g[a]),m[a]=r("_"+a,h({writer:l[a]},g[a]));for(a in j)m[a]=r("_"+a,h({writer:l[a]},j[a]));var n=p(function(a){a=a||{},this._originalArgs=o(a);var b={};l.key.call(this,a.key),b.key=!0;for(var c in a)l[c]&&(l[c].call(this,a[c]),b[c]=!0);var d=this.is;d&&d.install(this,a,b);for(var e=this.traits,f=0,g=e.length;g>f;f++)e[f].install(this,a,b)},{getValue:function(a){return this.getMethod.call(a)},setValue:function(a,b){return this.setMethod.call(a,b)},hasValue:function(a){return this.predicateMethod.call(a)},clearValue:function(a){return this.clearerMethod.call(a)},setValueFromArgs:function(a,b,c){var d=this.argKey;if(d&&void 0!==b[d])return c&&(c[d]=!0),this.setValue(a,b[d])},install:function(a,b){var c={};return this.valueKey!==this.key&&(this.reader&&(a[this.getMethodName]=this.getMethod),this.writer&&(a[this.setMethodName]=this.setMethod),this.writer&&this.writer===this.reader?c[this.writer]={get:this.propGetMethod,set:this.propSetMethod}:(this.reader&&(c[this.reader]={get:this.propGetMethod}),this.writer&&(c[this.writer]={set:this.propSetMethod}))),this.predicate&&(a[this.predicateGetMethodName]=this.predicateGetMethod,c[this.predicate]={get:this.propPredicateGetMethod}),this.clearer&&(a[this.clearer]=this.clearerMethod),Object.defineProperties(a,c),Object.defineProperties(a,this.proxyProperties),b&&this.setValue(a,b),a},rebuild:function(a){var b=this.constructor;return new b(h({},this._originalArgs,a))}}),s={};for(a in m)s[a]={get:m[a]};return Object.defineProperties(n.prototype,s),n}()),X=c.attributeType=function(){return new A(W,{name:"o.attributeType"})}(),Y=c.Trait=function(){var a,b=p(function(b){"function"==typeof b&&(b=this.functionToArgs(b)),b=b||{};for(var c={},d=0,e=a.length;e>d;d++)if(a[d].setValueFromArgs(this,b,c),"is"==a[d].key){var f=this.is;f&&f.install(this,b,c)}},{functionToArgs:function(a){var b={requires:[],traits:[],attributeTraits:[],attributes:{},methods:{},around:{},before:{},after:{}},c={require:function(a){b.requires.push(a)},is:function(a){b.is=a},trait:function(a){b.traits.push(a)},attributesAre:function(a){b.attributesAre=a},attributeTrait:function(a){b.attributeTraits.push(a)},attribute:function(a,c){b.attributes[a]=c},method:function(a,c){b.methods[a]=c},around:function(a,c){b.around[a]=c},before:function(a,c){b.before[a]=c},after:function(a,c){b.after[a]=c}};return a.apply(c),b},install:function(a,b,c){var d,e,f,g=this.requires;g&&g.validate(a);var h=this.methods;for(f in h)a[f]=h[f];var i=this.traits;for(d=0,e=i.length;e>d;d++)i[d].install(a);var j=this.attributes;for(f in j)j[f].install(a);var k=this.around;for(f in k)a[f]=m(a[f],k[f]);var o=this.before;for(f in o)a[f]=n(a[f],o[f]);var p=this.after;for(f in p)a[f]=l(a[f],p[f]);return b&&this.setFromArgs(a,b,c),a},setFromArgs:function(a,b,c){var d=this.attributes;c||(c={});var e,f,g;for(g in d)d[g].filter||d[g].setValueFromArgs(a,b,c);for(g in d)d[g].filter&&d[g].setValueFromArgs(a,b,c);var h=this.traits;for(e=0,f=h.length;f>e;e++)h[e].setFromArgs(a,b,c)},_buildType:function(){var a,b={},c=this.attributes;for(a in c){var d=c[a],e=d.type||(d.required?T:void 0);e&&d.reader&&(b[d.reader]=e),e&&d.writer&&(b[d.writer]=e),d.predicate&&(b[d.predicate]=F),d.clearer&&(b[d.clearer]=G)}for(a in this.methods)b[a]=G;var f=/^_/m;for(a in b)a.match(f)&&delete b[a];b=new Q(b);var g=this.traits;if(!g.length)return b;for(var h=[b],i=0,j=g.length;j>i;i++)h.push(g[i].type);return new w(h)}}),d=new A(b),e=new P(d);a=[{key:"requires",type:new A(Q),coerce:!0},{key:"is",type:d,filter:function(a){return d.check(a)?a:c[a+"ClassTrait"]}},{key:"traits",type:e,devoid:function(){return[]}},{key:"attributesAre",type:d,filter:function(a){return d.check(a)?a:c[a+"AttributeTrait"]}},{key:"attributeTraits",type:e,devoid:function(){return[]}},{key:"attributes",type:new R(X),devoid:function(){return{}},filter:function(a){var b={};for(var c in a){var d=a[c];if(X.check(d))d.key!==c&&(d=d.rebuild({key:c}));else{var e={key:c,traits:this.attributeTraits};this.attributesAre&&(e.is=this.attributesAre),d=new W(h({},d,e))}b[c]=d}return b}},{key:"methods",type:new R(G),devoid:function(){return{}}},{key:"around",type:new R(G),devoid:function(){return{}}},{key:"before",type:new R(G),devoid:function(){return{}}},{key:"after",type:new R(G),devoid:function(){return{}}},{key:"type",type:N,builder:!0,argKey:null}];for(var f=b.prototype,g=0,i=a.length;i>g;g++)a[g]=new W(a[g]),a[g].install(f);return b}(),Z=(c.liteAttributeTrait=function(){return new Y({attributes:{valueKey:{type:U,devoid:function(){return this.key}}}})}(),c.rwAttributeTrait=function(){var a=new x([F,U]);return new Y({attributes:{writer:{type:a,devoid:function(){return this.reader}}}})}(),c.rwpAttributeTrait=function(){var a=new x([F,U]);return new Y({attributes:{writer:{type:a,devoid:function(){return q("_",this.reader)}}}})}(),c.traitType=function(){return new A(Y,{name:"o.traitType"})}()),$=c.classTrait=function(){return new Y({attributes:{type:{type:N,builder:!0,argKey:null},trait:{type:Z,required:!0}},methods:{_buildType:function(){return new A(this)}}})}(),_=(c.liteClassTrait=function(){return new Y({attributes:{attributesAre:{type:Z,devoid:"lite",filter:function(a){return Z.check(a)?a:c[a+"AttributeTrait"]}}}})}(),c.Class=function(){return p(function(a){var b=new Y(a),c=function(a){a=a||{},b.setFromArgs(this,a)};return b.install(c.prototype),$.install(c,{trait:b}),c})}());c.classType=function(){return $.type.subtype({name:"o.classType",coerce:function(a){return this.check(a)?a:new _(a)}})}();a.o=c,a.oJS=c,c.noConflict=function(){return a.o=b,c}}).call(this); \ No newline at end of file diff --git a/package.json b/package.json index c2b3e9a..ca0cd21 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "o", - "version": "0.1.0", + "version": "0.2.0", "description": "JavaScript utility belt for rich objects and prototypes. Ooooh!", "homepage": "https://o-js.com",