Created
June 6, 2015 19:07
-
-
Save MarkArts/cba35e307731f8173893 to your computer and use it in GitHub Desktop.
Wrapper around a array with multiy filter and sort
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
var Sort = function() | |
{ | |
this.data = []; // end point of the data | |
this._initOrder = []; // Objects in order of initialization for cache optimazation | |
this._filters = {}; | |
this._sorts = {}; | |
} | |
Sort.prototype.add = function(item){ | |
this._initOrder.push(item); | |
} | |
Sort.prototype.remove = function(item){ | |
this._initOrder.splice(index, 1) | |
} | |
// each that loops over the active elements | |
Sort.prototype.each = function(f){ | |
this.data.map(f); | |
} | |
// each that loops over all elements; | |
Sort.prototype._each = function (f) { | |
this._initOrder.map(f); | |
} | |
Sort.prototype._resolve = function(){ | |
//todo: optimize the shit out of this | |
var data = []; | |
this._each(function(i){ | |
if(this._shouldInclude(i)){ | |
data.push(i); | |
} | |
}.bind(this)) // shame we have to bind here | |
this.data = this._sort(data); | |
} | |
Sort.prototype.addFilter = function(name, f){ | |
this._filters[name] = f; | |
} | |
Sort.prototype.removeFilter = function(name){ | |
delete this._filters[name]; | |
} | |
// only filter the current data | |
// maybe we should use the array.prototype.filter filter? | |
Sort.prototype.filter = function(){ | |
if(Object.keys(this._filters).length > 0){ | |
var data; | |
this.each(function(i){ | |
if(this._shouldInclude(i)){ | |
data.push(i); | |
} | |
}); | |
this.data = data; | |
} | |
} | |
Sort.prototype._shouldInclude = function(i){ | |
if(Object.keys(this._filters).length > 0){ | |
return ifAny(i, this._filters); | |
}else{ | |
return true; | |
} | |
} | |
Sort.prototype.addSort = function(name, wheight, f){ | |
this._sorts[name] = {wheight:wheight, func:f}; | |
} | |
Sort.prototype.removeSort = function(name){ | |
delete this._sorts[name]; | |
} | |
Sort.prototype._sort = function(data){ | |
// we could probaply heavenly optmise this by extrating only the values that the sorts functions use | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Sorting_with_map | |
if(Object.keys(this._sorts).length > 0){ | |
var sorts = Object.keys(this._sorts).map(function (key) {return this._sorts[key]}.bind(this)); // bind bind binds :( | |
sorts.sort(function(a,b){ | |
return b.wheight - a.wheight | |
}); | |
data.sort(function(a,b){ | |
var r = 0; | |
for(i in sorts){ | |
r = sorts[i].func(a, b); | |
if(r != 0){ | |
break | |
} | |
} | |
return r; | |
}.bind(this)); // i don't like binds :( | |
} | |
return data; | |
} | |
// only sort the current data | |
Sort.prototype.sort = function(){ | |
this.data = this._sort(this.data); | |
} | |
// checks if any of the functions returns true | |
function ifAny(x, fs){ | |
for( f in fs){ | |
if(fs[f](x)){ | |
return true; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment