Created
February 7, 2018 03:35
-
-
Save koreapyj/90992ec75352256188c2719a7c06eb76 to your computer and use it in GitHub Desktop.
nope
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.tokenList = function(arr) { | |
if($.isset(arr) && arr instanceof Array) { | |
var _this = this; | |
arr.forEach(function(val,idx) { | |
_this[idx] = val; | |
}); | |
_this.length = arr.length; | |
return _this; | |
} | |
}; | |
$.tokenList.prototype = new Array; | |
$.tokenList.prototype.add = function(a) { | |
if(!this.contains(a)) | |
return this.push(a); | |
return false; | |
}; | |
$.tokenList.prototype.remove = function (a) { | |
var idx = this.indexOf(a); | |
if(idx === -1) | |
return; | |
this.splice(idx, 1); | |
}; | |
$.tokenList.prototype.toggle = function (a) { | |
if(!this.contains(a)) | |
return this.add(a); | |
return this.remove(a); | |
}; | |
$.tokenList.prototype.contains = function(a) { | |
return this.indexOf(a) !== -1; | |
}; | |
$.tokenList.prototype.toString = function() { | |
return this.join(' '); | |
}; | |
$.tokenList.prototype.toJSON = function() { | |
return this.slice(0); | |
}; | |
$.dataObject = function() { | |
var _this = this; | |
var data = {}; | |
var indexes = {}; | |
var indexed_columns = new $.tokenList(); | |
var primary_column = null; | |
var criteria = {}; | |
var orderby = []; | |
_this.auto_increment = null; | |
_this.initialized = false; | |
_this.initialize = function(obj, key_column) { | |
data = {}; | |
indexes = {}; | |
indexed_columns.forEach(function(column) { | |
indexes[column] = {}; | |
}); | |
if(key_column) primary_column = key_column; | |
else _this.auto_increment = 0; | |
obj.forEach(function(row) { | |
var key; | |
if(!key_column) | |
key = row[_this.auto_increment++]; | |
else | |
key = row[key_column]; | |
data[row[key_column]] = row; | |
indexed_columns.forEach(function(column) { | |
if(!$.isset(indexes[column][row[column]])) indexes[column][row[column]] = []; | |
indexes[column][row[column]].push(key); | |
}); | |
}); | |
_this.initialized = true; | |
return _this; | |
}; | |
_this.setIndex = function(column) { | |
indexed_columns.add(column); | |
return _this; | |
} | |
_this.removeIndex = function(column) { | |
indexed_columns.remove(column); | |
return _this; | |
} | |
_this.setACriteria = function(key, value) { | |
criteria[key] = value; | |
return _this; | |
} | |
_this.reset = function(key, value) { | |
criteria = {}; | |
orderby = []; | |
return _this; | |
} | |
_this.setOrderBy = function(col, order) { | |
orderby.push([col, order]); | |
return _this; | |
} | |
_this.getList = function(limit) { | |
var start = Date.now(); | |
var jobtime = 0; | |
var use_index = null; | |
var use_primary = false; | |
var max_item = 0; | |
var temp_table = []; | |
var out_table = []; | |
var idx_length = 0; | |
Object.keys(criteria).forEach(function(col) { | |
var cs_val = col.split(':'); | |
if(!$.isset(cs_val[1])) { | |
if(col == primary_column) | |
use_primary = true; | |
else if(indexed_columns.contains(col) && (idx_length = Object.keys(indexes[col]).length) > max_item) { | |
use_index = col; | |
max_item = idx_length; | |
} | |
} | |
}); | |
if(use_primary) { | |
temp_table = []; | |
temp_table.push(criteria[primary_column]); | |
} | |
else if(use_index) { | |
if($.isset(indexes[use_index][criteria[use_index]])) | |
temp_table = indexes[use_index][criteria[use_index]]; | |
else | |
temp_table = []; | |
} | |
else | |
temp_table = Object.keys(data); | |
temp_table.forEach(function(key) { | |
if(limit && out_table.length>=limit) return; | |
var use = true; | |
Object.keys(criteria).forEach(function(col) { | |
var cs_val = col.split(':'); | |
switch(cs_val[1]){ | |
case 'search': | |
if(data[key][cs_val[0]].length>0 && data[key][cs_val[0]].indexOf(0)==-1 && data[key][cs_val[0]].indexOf(criteria[col])==-1) | |
use = false; | |
break; | |
case 'startswith': | |
if(!data[key][cs_val[0]].startsWith(criteria[col])) | |
use = false; | |
break; | |
case 'callback': | |
if(!criteria[col](data[key][cs_val[0]])) | |
use = false; | |
break; | |
default: | |
if(data[key][cs_val[0]] != criteria[col]) | |
use = false; | |
} | |
}); | |
if(!use) | |
return; | |
out_table.push(data[key]); | |
}); | |
if(orderby.length>0) { | |
var tmp_orderby = orderby.slice().reverse(); | |
out_table.sort(function(a,b) { | |
var r,ob,row; | |
for(ob in tmp_orderby) { | |
row=tmp_orderby[ob]; | |
if(row[1]) | |
r=b[row[0]] - a[row[0]]; | |
else | |
r=a[row[0]] - b[row[0]]; | |
if(r!=0) | |
return r; | |
} | |
return 0; | |
}); | |
} | |
jobtime = (jobtime = (Date.now() - start))<1?'<1':jobtime; | |
console.log('<dataObject> Used index: ', use_index, ', use primary: ', use_primary?'Yes':'No', ', index size: ', max_item, ', temp table length: ', temp_table.length, ', executed in ', jobtime, 'ms'); | |
return out_table; | |
} | |
_this.getOne = function(key) { | |
return data[key]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment