-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchancleta.js
70 lines (63 loc) · 1.79 KB
/
chancleta.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function sortJsonArrayByProperty(objArray, prop, direction){
if (arguments.length<2) throw new Error("sortJsonArrayByProp requires 2 arguments");
var direct = arguments.length>2 ? arguments[2] : 1; //Default to ascending
if (objArray && objArray.constructor===Array){
var propPath = (prop.constructor===Array) ? prop : prop.split(".");
objArray.sort(function(a,b){
for (var p in propPath){
if (a[propPath[p]] && b[propPath[p]]){
a = a[propPath[p]];
b = b[propPath[p]];
}
}
if(typeof(a) == "string"){
a = a.match(/^\d+$/) ? +a : a;
b = b.match(/^\d+$/) ? +b : b;
}
return ((a < b) ? -1 * direct: ((a > b) ? 1 * direct: 0));
});
}
}
function Chancleta(data_){
this._data = data_;
}
function _all(){
return this._data;
}
function _many(filter_dict){
if(filter_dict === undefined){
filter_dict = {};
}
var _elements = Array();
for(var i in this._data){
var this_element = this._data[i];
var match = true;
for(var key in filter_dict){
if(key != "sorted" && this_element[key] != filter_dict[key]){
match = false;
}
}
if(match){
_elements.push(this_element);
}
}
if(filter_dict.sorted !== undefined){
sortJsonArrayByProperty(_elements, filter_dict.sorted);
}
return _elements;
}
function _one(filter_dict){
var objects = this.many(filter_dict);
if(objects.length){
return objects[0];
}
}
function _first(filter_dict){
return this.many(filter_dict)[0];
}
Chancleta.prototype = {
all: _all,
one: _one,
first: _first,
many: _many
};