-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsql.min.js
15 lines (15 loc) · 7.33 KB
/
jsql.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
(function(c,a){"function"===typeof define&&define.amd?define([],a):c.JSQL=a()})(this,function(){"function"!==typeof String.prototype.trim&&(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")});Array.isArray||(Array.isArray=function(a){return"[object Array]"===Object.prototype.toString.call(a)});"function"!==typeof Object.keys&&(Object.keys=function(a){if("object"!==typeof a&&"function"!==typeof a||null===a)throw TypeError("Object.keys called on non-object");var b=[],d;for(d in a)a.hasOwnProperty(d)&&
b.push(d);return b});Array.prototype.indexOf||(Array.prototype.indexOf=function(a,b){var d=this.length>>>0,e=Number(b)||0,e=0>e?Math.ceil(e):Math.floor(e);for(0>e&&(e+=d);e<d;e++)if(e in this&&this[e]===a)return e;return-1});var c=function(a){this.tmp=this.data=a;this.query={debug:{count:a.length,operations:[]},options:{ignoreEmptyString:!1},select:[],distinct:void 0,sort:{}}};c.prototype._extend=function(a,b){var d={},e;for(e in a)a.hasOwnProperty(e)&&(d[e]=a[e]);for(e in b)b.hasOwnProperty(e)&&
(d[e]=b[e]);return d};c.prototype._queryObj=function(a,b){return this._isObjectEmpty(b)?[]:this._filter(a,function(a){for(var e in b)if(b.hasOwnProperty(e)&&String(a[e])!==String(b[e]))return!1;return!0})};c.prototype._isObjectEmpty=function(a){for(var b in a)if(a.hasOwnProperty(b))return!1;return!0};c.prototype._getUnique=function(a){var b=[];return this._filter(a,function(a){if(-1!==b.indexOf(a))return!1;b.push(a);return!0})};c.prototype._filter=function(a,b){var d=[],e,c=a.length;for(e=0;e<c;e++)b(a[e])&&
d.push(a[e]);return d};c.prototype._map=function(a,b){var d=[],e,c=a.length;for(e=0;e<c;e++)d.push(b(a[e]));return d};c.prototype._pluck=function(a,b){for(var d=0,e=a.length,c=[],d=0;d<e;d++)c.push(a[d][b]);return c};c.prototype._pluckMany=function(a,b){return this._map(a,function(a){var e={},c,f=b.length;for(c=0;c<f;c++)e[b[c]]=a[b[c]];return e})};c.prototype._stripEmptyProps=function(a){var b={},d;for(d in a)!a.hasOwnProperty(d)||"string"===typeof a[d]&&""===a[d].trim()||(b[d]=a[d]);return b};c.prototype._stripEmptyPropsFromCollection=
function(a){for(var b=0,d=a.length,b=0;b<d;b++)a[b]=this._stripEmptyProps(a[b]);return a};c.prototype._addOp=function(a){this.query.debug.operations.push(a)};c.prototype.debug=function(){return this.query.debug};c.prototype.options=function(a){this.query.options=this._extend(this.query.options,a);return this};c.prototype._applyOptions=function(a){this._getOpt("ignoreEmptyString")&&(a=this._stripEmptyPropsFromCollection(a));return a};c.prototype._getOpt=function(a){return this.query.options[a]};c.prototype._processArguments=
function(a){var b={clause:"VOID",args:void 0,type:0};if(0===a.length)return b.type=1,b;if(null===a[0]||void 0===a[0])return b.type=2,b;1===a.length&&Array.isArray(a[0])&&(a=a[0]);if(1===a.length&&"object"===typeof a[0])return b.args=a[0],b.clause="AND",b.type=5,b;if(2===a.length&&"string"===typeof a[0]){var d={};d[a[0]]=a[1];b.args=d;b.clause="AND";b.type=6;return b}if(1<a.length&&this._areAllObjects(a))return b.args=a,b.clause="OR",b.type=7,b;b.type=8;return b};c.prototype.select=function(){this._addOp("select");
this._reset();this.query.select=arguments;return this};c.prototype.where=function(){var a=this._processArguments(arguments);a.args=this._applyOptions(a.args);if("AND"===a.clause)return this.tmp=this._queryObj(this.tmp,a.args),this;"OR"===a.clause&&this._orWhere(a.args);return this};c.prototype.contains=function(a){this._addOp("contains");this.tmp=this._filter(this.tmp,function(b){for(var d in b)if(b.hasOwnProperty(d)&&b[d]==a)return!0;return!1});return this};c.prototype._areAllObjects=function(a){var b,
d=a.length;if(0===d)return!1;for(b=0;b<d;b++)if("object"!==typeof a[b]||null===a[b])return!1;return!0};c.prototype._orWhere=function(a){this._addOp("orWhere");var b=this;this.tmp=this._filter(this.tmp,function(d){var e,c=a.length;for(e=0;e<c;e++)if(b._queryObj([d],a[e]).length)return!0;return!1})};c.prototype.sortBy=function(a,b){if("object"===typeof a){for(var d in a)a.hasOwnProperty(d)&&("desc"===a[d]?this.sortDesc(d):this.sortAsc(d));return this}if("desc"===b)return this.sortDesc(a),this;this.sortAsc(a);
return this};c.prototype.sortAsc=function(a){this.query.sort[a]=1;return this};c.prototype.sortDesc=function(a){this.query.sort[a]=-1;return this};c.prototype._getSorted=function(a,b){var d=Object.keys(b),e=this._sortFirstBy(d[0],b[d[0]]);if(1===d.length)return a.sort(e);delete b[d[0]];for(var c in b)b.hasOwnProperty(c)&&(this._addOp("sort"),e=e.thenBy(c,b[c]));return a.sort(e)};c.prototype._sortFirstBy=function(){function a(a,b){if("function"!==typeof a){var d=a;a=function(a,b){return a[d]<b[d]?
-1:a[d]>b[d]?1:0}}if(1===a.length){var c=a;a=function(a,b){return c(a)<c(b)?-1:c(a)>c(b)?1:0}}return-1===b?function(b,d){return-a(b,d)}:a}function b(b,c){b=a(b,c);b.thenBy=d;return b}function d(d,c){var f=this;d=a(d,c);return b(function(a,b){return f(a,b)||d(a,b)})}return b}();c.prototype.limit=function(a,b){this._addOp("limit");b=b||0;this.tmp=this.tmp.slice(0+b,a+b);return this};c.prototype.isIn=function(a,b){this._addOp("in");this.tmp=this._filter(this.tmp,function(d){return-1!==b.indexOf(d[a])});
return this};c.prototype.isNotIn=function(a,b){this._addOp("notIn");this.tmp=this._filter(this.tmp,function(d){return-1===b.indexOf(d[a])});return this};c.prototype.between=function(a,b,d,c){this._addOp("between");if(c)return this.tmp=this._filter(this.tmp,function(c){return b<c[a]&&c[a]<d}),this;this.tmp=this._filter(this.tmp,function(c){return b<=c[a]&&c[a]<=d});return this};c.prototype.regEx=function(a,b,c){this._addOp("regex");var e;if(c)return e=new RegExp(b,"i"),this.tmp=this._filter(this.tmp,
function(b){return e.test(b[a])}),this;e=new RegExp(b);this.tmp=this._filter(this.tmp,function(b){return e.test(b[a])});return this};c.prototype._escapeRegex=function(a){return String(a).replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&")};c.prototype.startsWith=function(a,b){this._addOp("startsWith");b=this._escapeRegex(b);this.regEx(a,"^"+b,!0);return this};c.prototype.endsWith=function(a,b){this._addOp("endsWith");b=this._escapeRegex(b);this.regEx(a,b+"$",!0);return this};c.prototype.like=function(a,b){this._addOp("like");
b=this._escapeRegex(b);this.regEx(a,b,!0);return this};c.prototype.lt=function(a,b){this._addOp("lt");this.tmp=this._filter(this.tmp,function(c){return c[a]<b});return this};c.prototype.gt=function(a,b){this._addOp("gt");this.tmp=this._filter(this.tmp,function(c){return c[a]>b});return this};c.prototype.lte=function(a,b){this._addOp("lte");this.tmp=this._filter(this.tmp,function(c){return c[a]<=b});return this};c.prototype.gte=function(a,b){this._addOp("gte");this.tmp=this._filter(this.tmp,function(c){return c[a]>=
b});return this};c.prototype.transform=function(a){this._addOp("transform");for(var b=0,c=this.tmp.length,b=0;b<c;b++)a(this.tmp[b]);return this};c.prototype.get=function(){var a=this.tmp;this._isObjectEmpty(this.query.sort)||(a=this._getSorted(a,this.query.sort));this.query.select.length&&(a=this._pluckMany(a,this.query.select));this.query.distinct&&(this._addOp("distinct"),a=this._getUnique(this._filter(this._pluck(a,this.query.distinct),function(a){return void 0===a?!1:!0})));return a};c.prototype.getOne=
function(){return this.get().slice(0,1)[0]};c.prototype.distinct=function(a){this.query.distinct=a;return this};c.prototype._reset=function(){this.tmp=this.data;this.query={debug:{count:this.data.length,operations:[]},options:{},select:[],distinct:void 0,sort:{}}};return c});