/** * GO Query! * Query executor on js array of objects * Build date: 2014-11-28 * @author Rubens Pinheiro Gonçalves Cavalcante * @version 1.0.1 * @license GPLv3 */ !function(a,b){"function"==typeof define&&define.amd?define(["exports"],function(c){b(a.GO=c)}):b("object"==typeof GO?module:a.GO={})}(this,function(a){"use strict";return a.Clause={},a.Core={},a.Core.Modifier={},a.Core.Helpers={},a.Error={},a.Utils={},a.query={},a.query.type={SELECT:0,DELETE:1,UPDATE:2},a.query.logic_op={AND:0,OR:1,XOR:2},a.query.WILDCARD="*",a.op={TAUTOLOGICAL:0,CONTRADICTORY:1,EQ:2,NEQ:3,GT:4,GTE:5,MT:6,MTE:7,LIKE:8,HAS:9},a.order={ASC:0,DESC:1},a.comparison={GREATER:1,EQUALS:0,LESSER:-1},function(a){a.Query=function(b){this.collection=b;var c=new a.Core.Record;this._getRecord=function(){return c},this._setRecord=function(a,b){c[a]instanceof Array?c[a].push(b):c[a]=b},this.select=function(){return c.type=a.query.type.SELECT,c.selection=arguments,new a.Clause.From(this)},this.update=function(){return c.type=a.query.type.UPDATE,c.selection=a.query.WILDCARD,new a.Clause.From(this)},this.remove=function(){return c.type=a.query.type.DELETE,c.selection=arguments,new a.Clause.From(this)}}}(a),function(a){a.Filter=function(b,c,d){b instanceof a.Filter?this.associate=b||null:(this.attribute=b||null,this.operator=c||a.op.TAUTOLOGICAL,this.value=d||null);var e=null,f=null;this._setPredecessor=function(a){e=a},this._getFilterChain=function(){return f},this.back=function(){return e},this.next=function(){return f.link},this.root=function(){for(var a=this;null!=a.back();)a=a.back();return a},this.isEmpty=function(){return this.hasOwnProperty("associate")?null==this.associate:null==this.attribute&&null==this.value},this.init=function(b,c,d){b instanceof a.Filter?this.associate=b:(this.attribute=b,this.operator=c,this.value=d)},this.and=function(b,c,d){var e=new a.Filter(b,c,d);return e._setPredecessor(this),f=new a.Core.FilterChain(a.query.logic_op.AND,e),e},this.or=function(b,c,d){var e=new a.Filter(b,c,d);return e._setPredecessor(this),f=new a.Core.FilterChain(a.query.logic_op.OR,e),e},this.xor=function(b,c,d){var e=new a.Filter(b,c,d);return e._setPredecessor(this),f=new a.Core.FilterChain(a.query.logic_op.XOR,e),e}}}(a),function(a){a.Error.CloneError=function(a){this.name="CloneError",this.message="Object couldn't be cloned. One or more inner attributes types are not supported",this.data=a||null},a.Error.CloneError.prototype=new Error}(a),function(a){a.Error.NotImplementedError=function(a,b){this.name="NotImplementedError",this.message="Constructor must implement "+a,this.data=b||null},a.Error.NotImplementedError.prototype=new Error}(a),function(a){a.Error.OperatorError=function(a,b){this.name="OperatorError",this.message=a,this.data=b||null},a.Error.OperatorError.prototype=new Error}(a),function(a){a.Error.PropertyNotDefinedError=function(a,b){this.msg="Property "+a+" is not defined",this.data=b},a.Error.PropertyNotDefinedError.prototype=new Error}(a),function(a){a.Error.QueryMethodError=function(a,b){this.name="QueryMethodError",this.message=a,this.data=b||null},a.Error.QueryMethodError.prototype=new Error}(a),function(a){a.Utils.ObjectUtils={_deepSearch:function(b,c,d,e){if(null==b)return null;var f=b.indexOf("."),g=null;if(-1!=f){var h=b.slice(0,f);if(b=b.slice(f+1),!c.hasOwnProperty(h))throw new a.Error.PropertyNotDefinedError(h,c);g=c[h]||null}else g=c;return-1!=b.indexOf(".")&&null!=g?this._deepSearch(g,b,d):d?(delete g[b],null):"undefined"!=typeof e?(g[b]=e,null):g[b]},unsafeDeepSelect:function(a,b){return this._deepSearch(a,b)},deepSelect:function(b,c){try{return this._deepSearch(b,c)}catch(d){if(d instanceof a.Error.PropertyNotDefinedError)return null}},deepSet:function(b,c,d){try{this._deepSearch(b,c,!1,d)}catch(e){if(e instanceof a.Error.PropertyNotDefinedError)return!1}return!0},deepDelete:function(b,c){try{this._deepSearch(b,c,!0)}catch(d){if(d instanceof a.Error.PropertyNotDefinedError)return!1}return!0}}}(a),function(a){a.Clause.From=function(b){var c=b;this.from=function(b){return"undefined"==typeof b&&(b=Object),c._setRecord("from",b),new a.Clause.Where(c)}}}(a),function(a){a.Clause.Where=function(b){var c=b;b._setRecord("where",this);var d={};this.filter=null;var e=function(){var b=c._getRecord();switch(b.type){case a.query.type.SELECT:d.orderBy=new a.Core.Modifier.OrderBy(b);break;case a.query.type.UPDATE:d.set=new a.Core.Modifier.Set(b)}};this.where=function(b){return b=b||new a.Filter("",a.op.TAUTOLOGICAL,""),this.filter=b.root(),e(),new a.Core.Processor(c,d)}}}(a),function(a){a.Core.Modifier.PostProcess=function(b){this._processor=null,this.modifierName=b,this.setProcessorReference=function(a){this._processor=a},this.init=function(){throw new a.Error.NotImplementedError("init",this.constructor)},this.modify=function(){throw new a.Error.NotImplementedError("modify",this.constructor)}}}(a),function(a){a.Core.Modifier.OrderBy=function(b){b.modifiers.push(this);var c=this,d=null,e=null,f=null,g={number:function(b,c,d){return d==a.order.ASC?b-c:c-b},string:function(b,c,d){var e=null;return e=b>c?1:c>b?-1:0,d==a.order.ASC?e:!e}};this.init=function(a,b,g){return d=a,e=b,f=g||null,c._processor},this.modify=function(b){b.sort(null==f?function(b,c){try{var f=a.Utils.ObjectUtils.unsafeDeepSelect(d,b),h=a.Utils.ObjectUtils.unsafeDeepSelect(d,c);if(g.hasOwnProperty(typeof f))return g[typeof f](f,h,e)}catch(i){}return 0}:f)}},a.Core.Modifier.OrderBy.prototype=new a.Core.Modifier.PostProcess("OrderBy"),a.Core.Modifier.OrderBy.constructor=a.Core.Modifier.OrderBy}(a),function(a){a.Core.Modifier.Set=function(b){b.modifiers.push(this);var c=null,d=this;this.init=function(a){return c=a,d._processor},this.modify=function(b){for(var d=0;d=this.filter.value;case a.op.MTE:return this.value<=this.filter.value;case a.op.GT:return this.value>this.filter.value;case a.op.MT:return this.valued;d++)c[d]=clone(b[d]);return c}if(b instanceof Object){c={};for(var f in b)b.hasOwnProperty(f)&&(c[f]=clone(b[f]));return c}throw new a.Error.CloneError(b)},objectMerge:function(a,b){for(var c in b)b.hasOwnProperty(c)&&(a[c]=b[c])}}}(a),a});