-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathogx.list.min.js
1 lines (1 loc) · 16.7 KB
/
ogx.list.min.js
1
if(typeof OGX==="undefined"){var OGX={}}OGX.List=function(){if(typeof moment==="undefined"){console.log("Error: moment.js not found!");return}if(typeof debug==="undefined"){debug=function(){}}var filteringMode="and";var filterCount=0;var indexes=false;if(arguments.length===1&&arguments[0]instanceof Array){arguments=arguments[0]}var array=Object.create(Array.prototype);array=Array.apply(array,arguments)||array;for(var method in OGX.List.prototype){if(OGX.List.prototype.hasOwnProperty(method)){array[method]=OGX.List.prototype[method]}}array.insert=function(e,t){if(typeof t==="undefined"){array.push(e)}else{array.splice(t,0,e)}if(indexes){addToCache(e)}return true};array.delete=function(e){var t=this;var i=array.splice(e,1);if(i.length){if(indexes){var r=matchesCache(i[0]);if(r){setTimeout(()=>{t.rebuild(r)},0)}}return i[0]}return false};array.last=function(){if(this.length){return this[this.length-1]}return};array.findDelete=function(__property,__value,__limit){var t=0;if(typeof __limit==="undefined"){__limit=0}var removed=new OGX.List;var ev,prop;for(var i=0;i<this.length;i++){prop=null;ev="this["+i+"]."+__property;try{prop=eval(ev)}catch(error){debug(1,"Property does not exist",__property)}if(prop===null||typeof prop==="undefined"){}else{if(prop===__value){removed.push(this.splice(i,1)[0]);if(indexes){var idxs=matchesCache(removed.last());if(idxs){this.rebuild(idxs)}}t++;if(__limit>0&&__limit===t){if(__limit===1){return removed[0]}else{return removed}}i--}}}return removed};array.findReplace=function(__property,__value,__object,__limit){var t=0;if(typeof __limit==="undefined"){__limit=0}var replaced=new OGX.List;var ev,prop;for(var i=0;i<this.length;i++){prop=null;ev="this["+i+"]."+__property;try{prop=eval(ev)}catch(error){debug(2,"Property does not exist",__property)}if(prop===null||typeof prop==="undefined"){}else{if(prop===__value){replaced.push(this[i]);t++;this[i]=__object;if(__limit>0&&__limit===t){if(__limit===1){return replaced[0]}else{return replaced}}}}}if(!replaced.length){return false}else{return replaced}};array.findUpdate=function(__property,__value,__object,__strict,__limit){var t=0;if(typeof __strict==="undefined"){__strict=false}if(typeof __limit==="undefined"){__limit=0}var updated=new OGX.List;var upd,ev,prop;for(var i=0;i<this.length;i++){prop=null;ev="this["+i+"]."+__property;try{prop=eval(ev)}catch(error){debug(2,"Property does not exist",__property)}if(prop===null||typeof prop==="undefined"){}else{if(prop===__value){upd=false;for(var a in __object){if(__strict){if(this[i].hasOwnProperty(a)){this[i][a]=__object[a];upd=true}}else{this[i][a]=__object[a];upd=true}}if(upd){t++;updated.push(this[i]);if(__limit>0&&__limit===t){if(__limit===1){return updated[0]}else{return updated}}}}}}if(!updated.length){return false}else{return updated}};array.findIndex=function(__property,__value){var isReg=false;if(__value instanceof RegExp){isReg=true}var ev,prop;for(var i=0;i<this.length;i++){prop=null;ev="this["+i+"]."+__property;try{prop=eval(ev)}catch(error){debug(2,"Property does not exist",__property)}if(prop!==null){if(isReg){if(prop.match(__value)){return{index:i}}}else{if(prop===__value){return{index:i}}}}}return false};array.find=function(e,t,i){var r=0;var a=new OGX.List;var s=false;if(t instanceof RegExp){s=true}if(typeof i==="undefined"){i=0}for(var f=0;f<this.length;f++){if(s){if(this[f][e].match(t)){a.push(this[f]);r++}}else{if(this[f][e]===t){a.push(this[f]);r++}}if(i>0&&r===i){if(i===1){return a[0]}else{return a}}}if(!a.length){return false}else{return a}};array.swap=function(e,t){var i=this[e];this[e]=this[t];this[t]=i};array.cache=function(){if(!indexes){indexes={}}var e=[];var t=[];var i;if(arguments.length>1){for(var r=0;r<arguments.length;r++){e.push(arguments[r])}e=e.join("|")}else{e=arguments[0]}if(!indexes.hasOwnProperty(e)){indexes[e]={}}for(var a=0;a<this.length;a++){t=[];for(var r=0;r<arguments.length;r++){t.push(this[a][arguments[r]])}t=t.join("|");if(!indexes[e].hasOwnProperty(t)){indexes[e][t]=[]}indexes[e][t].push(this[a])}return true};array.uncache=function(e){if(!indexes){return false}var t=e;if(arguments.length>1){t=arguments.join("|")}if(indexes.hasOwnProperty(t)){delete indexes[t];return true}return false};array.deleteProperty=function(e){var t=0;for(var i=0;i<this.length;i++){if(this[i].hasOwnProperty(e)){delete this[i][e];t++}}if(indexes){this.rebuild()}return t};function doEval(e,t){var i=new Function("try{return eval("+e+");}catch(error){}");return i(t)}function addToCache(e){var t;for(var i in indexes){if(i.indexOf("|")!==-1){i=i.split("|");t=[];for(var r=0;r<i.length;r++){if(e.hasOwnProperty(i[r])){t.push(e[i[r]])}}t=t.join("|");i=i.join("|")}else{if(e.hasOwnProperty(i)){t=e[i]}}if(!indexes.hasOwnProperty(i)){indexes[i]={}}indexes[i][t]=e}}array.rebuild=function(e){if(Array.isArray(e)){for(var t=0;t<e.length;t++){delete indexes[e[t]];this.cache(e[t])}}else{if(indexes){for(var i in indexes){delete indexes[i];this.cache.apply(this,i.split("|"))}}}};function matchesCache(e){var t=[];var i=[];var r;for(var a in indexes){r=true;if(a.indexOf("|")!==-1){t=a.split("|")}else{t=[a]}for(var s=0;s<t.length;s++){if(!e.hasOwnProperty(t[s])){r=false;break}}if(r){i.push(a)}}if(i.length){return i}return false}array.read=function(e,t,i){var r=arguments;if(!indexes){return false}if(!r.length){return indexes}var a,s;if(typeof e==="object"){i=r[1];a=[];s=[];for(var f in e){a.push(f);s.push(e[f])}a=a.join("|");s=s.join("|")}else{a=e;s=t}if(typeof i==="undefined"){i=0}if(indexes.hasOwnProperty(a)&&indexes[a].hasOwnProperty(s)){if(i){if(i>1){return indexes[a][s].splice(0,i)}else{return indexes[a][s][0]}}return indexes[a][s]}return false};array.clear=function(){indexes=false;filterCount=0;this.length=0};array.unique=function(__prop,__as_object,__new_props){if(typeof __as_object==="undefined"){__as_object=true}if(typeof __new_props==="undefined"){__new_props=[__prop]}else{if(!Array.isArray(__new_props)){__new_props=[__new_props]}}var q={};q[__prop]={regex:/(.){1,}/};var l=this.get();var ar=[];var final=new OGX.List;var o;var ev;for(var i=0;i<l.length;i++){ev=null;try{ev=eval("l[i]."+__prop)}catch(error){}if(ev&&!ar.includes(ev)){ar.push(ev);if(__as_object){o={};for(var j=0;j<__new_props.length;j++){o[__new_props[j]]=ev}final.push(o)}}}if(__as_object){return final}return ar};array.order=function(e,t){array.sort(dynamicSort(e,t))};function dynamicSort(__prop,__way){return function(a,b){return eval("a."+__prop)<eval("b."+__prop)?__way*-1:eval("a."+__prop)>eval("b."+__prop)?__way:0}}array.group=function(e,t,i){if(Array.isArray(e)){return array.groupm(e,t,i)}var r;var a={};for(var s=0;s<this.length;s++){r=extract(this[s],e,t,i);if(r){r=r.split(".").pop();if(!a.hasOwnProperty(r)){a[r]=new OGX.List}a[r].push(this[s])}}return a};array.groupm=function(n,l,o){function h(e,t){var i=new OGX.List(e);var r=l;var a=o;if(typeof l!=="undefined"){Array.isArray(l)?r=l[t]:null}if(typeof o!=="undefined"){Array.isArray(o)?a=o[t]:null}var s=i.group(n[t],r,a);if(t+1<=n.length-1){for(var f in s){s[f]=h(s[f],t+1)}}return s}var e=h(this,0);return e};function extract(__item,__by,__mode,__param){var data;try{data=eval("__item."+__by)}catch(error){}if(data){switch(__mode){case"substr":data=data.substr(__param[0],__param[1]);return data;case"numrange":data=Number(data);if(!Array.isArray(__param)){data=Math.floor(data/__param);data=data*__param+" - "+(data+1)*__param}else{data=Math.floor(data/__param[0]);if(__param[2]==="pre"){data=__param[1]+data*__param[0]+" - "+__param[1]+(data+1)*__param[0]}else{data=data*__param[0]+__param[1]+" - "+(data+1)*__param[0]+__param[1]}}return data;case"date":if(!Array.isArray(__param)){if(__param==="year"){data=data.substr(0,4);return data}else{if(__param==="month"){data=data.substr(0,7);return data}else{if(__param==="week"){data=data.substr(0,4)+" week "+moment(data).week();return data}else{if(__param==="day"){data=data.substr(0,10);return data}}}}}data=moment(data).unix();data=Math.floor(data/(__param[0]*60*60*24));data=data*(__param[0]*60*60*24);data=moment(data*1e3).format(__param[1])+" - "+moment(data*1e3+__param[0]*1e3*60*60*24).format(__param[1]);return data;default:return data}}return false}array.filters={};array.addFilter=function(e,t,i){if(!this.filters.hasOwnProperty(e)){this.filters[e]=[]}var r={};r[t]=i;this.filters[e].push(r);filterCount++;return true};array.removeFilter=function(e){if(this.filters.hasOwnProperty(e)){filterCount-=this.filters[e].length;delete this.filters[e];return true}return false};array.getFilters=function(){return this.filters};array.resetFilters=function(){this.filters={};filterCount=0};array.setFilters=function(e){this.resetFilters();for(var t in e){for(var i=0;i<e[t].length;i++){for(var r in e[t][i]){array.addFilter(t,r,e[t][i][r])}}}};array.setFilteringMode=function(e){filterMode=e};array.get=function(e,t,i,r,a){if(typeof t==="undefined"){t=false}if(typeof i==="undefined"){i=false}if(typeof i==="number"){r=i;i=false}if(typeof r==="undefined"){r=0}if(typeof a==="undefined"){a=false}var s=this.getFilters();this.resetFilters();for(var f in e){if(typeof e[f]!=="object"){var n={};n={eq:e[f]};e[f]=n}for(var l in e[f]){this.addFilter(f,l,e[f][l])}}var o=this.filter(r,a);this.resetFilters();this.setFilters(s);if(a){return o}if(r===1){if(o.length){return o[0]}return false}if(t){for(var f in t){o.order(f,t[f]);break}}if(i){!Array.isArray(i)?i=[i]:null;return o.group.apply(o,i)}return o};array.unset=function(e,t){if(typeof t==="undefined"){t=0}var i=new OGX.List;var r=this.get(e,null,null,t,true);if(r){for(var a=0;a<r.length;a++){i.push(this.delete(r[a]-i.length))}}if(t===1&&i.length){return i[0]}return i};array.update=function(__filterobj,__updateobj,__strict,__limit){if(typeof __strict==="undefined"){__strict=false}if(typeof __limit==="undefined"){__limit=0}var filtered=this.get(__filterobj,null,__limit);if(filtered){if(!Array.isArray(filtered)){filtered=[filtered]}if(filtered.length){var affected_indexes=[];var idx;var ev;for(var i=0;i<filtered.length;i++){if(typeof __updateobj==="function"){__updateobj(filtered[i])}else{for(var a in __updateobj){ev=doEval("__o."+a,filtered[i]);if(!__strict||typeof ev!=="undefined"){eval("filtered[i]."+a+"=__updateobj[a]")}}}idx=matchesCache(filtered[i]);if(idx){if(affected_indexes.indexOf(idx)===-1){affected_indexes.push(idx)}}}if(indexes&&affected_indexes.length){setTimeout(()=>{that.rebuild(affected_indexes)},0)}return filtered}}return[]};array.filter=function(__limit,__as_position){if(typeof __limit==="undefined"){__limit=0}if(typeof __as_position==="undefined"){__as_position=false}var ar=[];var positions=[];var isDate,isString,isNum,isArray,isObject;var hasFilters=false;var reg=/[0-9]{4}\-[0-9]{2}\-[0-9]{2}( [0-9]{2}\:[0-9]{2}\:[0-9]{2})?/g;var isMatch=false;var matches=0;var str,o,err;var counter,total;for(var i=0;i<this.length;i++){isMatch=true;matches=0;for(var a in this.filters){err=false;hasFilters=true;try{o=null;str="this["+i+"]."+a;o=eval(str)}catch(error){err=true}var exist_filter=false;for(var j in this.filters[a]){for(var b in this.filters[a][j]){if(b==="exist"){exist_filter=true;isMatch=o==="undefined"&&!this.filters[a][j][b]||o&&this.filters[a][j][b];isMatch?matches++:null;break}}}if(!exist_filter){if(!err&&typeof o!=="undefined"){isDate=false;isString=false;isNum=false;isArray=Array.isArray(o);isObject=!isArray&&typeof o==="object";if(!isArray){if(typeof o==="string"){isString=true;if(o.match(reg)){isDate=true}}else{isNum=typeof o==="number"}}else{if(typeof o[0]==="string"){isString=true;if(o[0].match(reg)){isDate=true}}else{isNum=typeof o[0]==="number"}}for(var j=0;j<this.filters[a].length;j++){for(var b in this.filters[a][j]){if(!String(this.filters[a][j][b]).length){matches++}else{if(b==="function"){isMatch=this.filters[a][j][b](o);if(isMatch){matches++}}else{switch(b){case"eq":if(isDate){if(typeof this.filters[a][j][b]!=="string"||!this.filters[a][j][b].match(reg)){isMatch=false;break}if(moment(o).format("YYYY-MM-DD")!==moment(this.filters[a][j][b]).format("YYYY-MM-DD")){isMatch=false}else{matches++}}else{if(isString){if(typeof this.filters[a][j][b]!=="string"){isMatch=false;break}if(o.toLowerCase()!==String(this.filters[a][j][b]).toLowerCase()){isMatch=false}else{matches++}}else{if(o!==this.filters[a][j][b]){isMatch=false}else{matches++}}}break;case"eqjson":if(JSON.stringify(o)!==JSON.stringify(this.filters[a][j][b])){isMatch=false}else{matches++}break;case"neq":if(isDate){if(typeof this.filters[a][j][b]!=="string"||!this.filters[a][j][b].match(reg)){isMatch=true;matches++;break}if(moment(o).format("YYYY-MM-DD")===moment(this.filters[a][j][b]).format("YYYY-MM-DD")){isMatch=false}else{matches++}}else{if(isString){if(typeof this.filters[a][j][b]!=="string"){isMatch=true;matches++;break}if(o.toLowerCase()===this.filters[a][j][b].toLowerCase()){isMatch=false}else{matches++}}else{if(o===this.filters[a][j][b]){isMatch=false}else{matches++}}}break;case"in":if((isString||isNum)&&Array.isArray(this.filters[a][j][b])){isMatch=false;for(var k=0;k<this.filters[a][j][b].length;k++){if(isString){if(o.indexOf(String(this.filters[a][j][b][k]))!==-1){isMatch=true;break}}else{if(isNum&&o===this.filters[a][j][b][k]){isMatch=true;break}}}if(isMatch){matches++}}else{if(isString||isNum){if(isArray){if(isString&&o.indexOf(String(this.filters[a][j][b]))===-1){isMatch=false}else{if(isNum&&o!==this.filters[a][j][b]){isMatch=false}else{matches++}}}else{if(isString&&(typeof o!=="string"||typeof this.filters[a][j][b]!=="string")){isMatch=false;break}if(isString&&o.toLowerCase().indexOf(this.filters[a][j][b].toLowerCase())===-1){isMatch=false}else{if(isNum&&o!==this.filters[a][j][b]){isMatch=false}else{matches++}}}}else{if(isArray){if(o.indexOf(this.filters[a][j][b])===-1){isMatch=false}else{matches++}}else{if(isObject){counter=0;total=0;for(var p in this.filters[a][j][b]){total++;if(o.hasOwnProperty(p)&&String(o[p]).toLocaleLowerCase().indexOf(String(this.filters[a][j][b][p]).toLowerCase())!==-1){counter++}}isMatch=total===counter;if(isMatch){matches++}}}}}break;case"nin":if(Array.isArray(this.filters[a][j][b])){for(var k=0;k<this.filters[a][j][b].length;k++){if(isString){if(typeof this.filters[a][j][b][k]!=="string"){isMatch=false;break}if(o.indexOf(this.filters[a][j][b][k])!==-1){isMatch=false;break}}else{if(isNum&&o===this.filters[a][j][b][k]){isMatch=false;break}}}if(isMatch){matches++}}else{if(isString&&typeof this.filters[a][j][b]==="string"){if(o.toLowerCase().indexOf(this.filters[a][j][b].toLowerCase())!==-1){isMatch=false}}else{if(isNum){if(o===this.filters[a][j][b]){isMatch=false}}else{if(o.indexOf(this.filters[a][j][b])!==-1){isMatch=false}}}if(isMatch){matches++}}break;case"gt":if(isDate){if(typeof this.filters[a][j][b]!=="string"||!this.filters[a][j][b].match(reg)){isMatch=false;break}if(moment(o).unix()<=moment(this.filters[a][j][b]).unix()){isMatch=false}else{matches++}}else{if(typeof this.filters[a][j][b]!=="number"){isMatch=false;break}if(o<=this.filters[a][j][b]){isMatch=false}else{matches++}}break;case"gte":if(isDate){if(typeof this.filters[a][j][b]!=="string"||!this.filters[a][j][b].match(reg)){isMatch=false;break}if(moment(o).unix()<moment(this.filters[a][j][b]).unix()){isMatch=false}else{matches++}}else{if(typeof this.filters[a][j][b]!=="number"){isMatch=false;break}if(o<this.filters[a][j][b]){isMatch=false}else{matches++}}break;case"lt":if(isDate){if(typeof this.filters[a][j][b]!=="string"||!this.filters[a][j][b].match(reg)){isMatch=false;break}if(moment(o).unix()>=moment(this.filters[a][j][b]).unix()){isMatch=false}else{matches++}}else{if(typeof this.filters[a][j][b]!=="number"){isMatch=false;break}if(o>=this.filters[a][j][b]){isMatch=false}else{matches++}}break;case"lte":if(isDate){if(typeof this.filters[a][j][b]!=="string"||!this.filters[a][j][b].match(reg)){isMatch=false;break}if(moment(o).unix()>moment(this.filters[a][j][b]).unix()){isMatch=false}else{matches++}}else{if(typeof this.filters[a][j][b]!=="number"){isMatch=false;break}if(o>this.filters[a][j][b]){isMatch=false}else{matches++}}break;case"btw":if(isDate){if(typeof this.filters[a][j][b]!=="string"||!this.filters[a][j][b].match(reg)){isMatch=false;break}if(moment(this.filters[a][j][b][0]).unix()>moment(o).unix()||moment(this.filters[a][j][b][1]).unix()<moment(o).unix()){isMatch=false}else{matches++}}else{if(typeof this.filters[a][j][b]!=="number"){isMatch=false;break}if(this.filters[a][j][b][0]>o||this.filters[a][j][b][1]<o){isMatch=false}else{matches++}}break;case"substr":if(o.toLowerCase().substr(this.filters[a][j][b][0],this.filters[a][j][b][1]).toLowerCase()!==this.filters[a][j][b][2].toLowerCase()){isMatch=false}else{matches++}break;case"regex":if(!o.toString().match(this.filters[a][j][b])){isMatch=false}else{matches++}break}}}if(!isMatch){break}}}}else{isMatch=false}}if(filteringMode==="or"){if(isMatch){ar.push(this[i]);positions.push(i)}}else{if(matches===filterCount){ar.push(this[i]);positions.push(i)}}if(__limit&&ar.length===__limit){if(__as_position){return positions}return OGX.List.apply(null,ar)}}}if(__as_position){return positions}if(hasFilters){return OGX.List.apply(null,ar)}return array};return array};