mini JQuery e data processor
Mini copy of jquery to be light.
Class to filter data like mongodb
HTML
<div id="log"></div>
CSS
#log {
border : solid 1px;
height : 37px;
}
JavaScript
var d = document;
_query = function(){
this.elements;
var self = this;
this.check = function(value){
if(!self.elements) return;
var _value = value?true:false;
[].map.call(self.elements, function(e){
e.checked = _value;
});
};
this.map = function(f){
[].map.call(self.elements, f);
};
this.text = function(text){
if(text == undefined){
self.map(function(e){ return e.innerText});
}else{
self.map(function(e){ e.innerText = text; });
}
}
};
$ = function(selector, f){
var items = d.querySelectorAll(selector);
var _q = new _query();
_q.elements = items;
return _q;
}
DataQuery = function(){
}
Array.prototype.Select = function(command){
var result = new Array(this.length);
result = this.filter(function(e){
var keys = Object.keys(eval("x = " + command));
var condition = true;
for(i in keys)
condition+=e[keys[i]] === command[keys[i]];
if(condition) return e;
});
return result;
}
function log(text){
$("#log").text(text);
}
log("teste");
var data = [{nome : "Maria"},
{nome : "Jhon"},
{nome : "Fred"},
{nome : "Antonia"}];
result = data.Select("{'nome' : 'Fred'}");
log(result[2].nome);