-
Notifications
You must be signed in to change notification settings - Fork 0
/
js-array-query.js
155 lines (130 loc) · 3.4 KB
/
js-array-query.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
var ArrayQuery = function(array) {
/**
* removeAcento
*/
var removeAcento = function(str) {
var varString = new String(str);
var stringAcentos = new String('àâêôûãõáéíóúçüÀÂÊÔÛÃÕÁÉÍÓÚÇÜ');
var stringSemAcento = new String('aaeouaoaeioucuAAEOUAOAEIOUCU');
var i = new Number();
var j = new Number();
var cString = new String();
var varRes = '';
for (i = 0; i < varString.length; i++) {
cString = varString.substring(i, i + 1);
for (j = 0; j < stringAcentos.length; j++) {
if (stringAcentos.substring(j, j + 1) == cString){
cString = stringSemAcento.substring(j, j + 1);
}
}
varRes += cString;
}
return varRes;
};
/**
* filter
*/
var filter = function(by, value, callback) {
var newArray = [];
var navegacao = by.split(".");
var isRegex = (Object.prototype.toString.call(value).indexOf("RegEx")>=0) ? true : false;
var iteration = function(item) {
var itemValido = false;
var profundidade = 0;
var navega = function(profundidade, navItem) {
var continuarNavegando = (profundidade+1 < navegacao.length) ? true : false;
if(continuarNavegando) {
var newItem = eval("navItem." + navegacao[profundidade]);
var isArray = (Object.prototype.toString.call(newItem).indexOf("Array")>=0) ? true : false;
if(!isArray) {
arguments.callee(profundidade+1, newItem);
} else {
for(var x in newItem) {
arguments.callee(profundidade+1, eval("newItem[" + x + "]"));
}
}
} else {
if(itemValido) return;
newItem = eval("navItem." + navegacao[profundidade]);
if(isRegex) itemValido = value.test(newItem);
else itemValido = (newItem==value);
}
}(0, item);
if(itemValido) newArray.push(item);
}
if(callback && typeof callback=='function') {
window.setTimeout(function() {
array.map(iteration);
callback.call(this, newArray);
}, 0);
} else {
array.map(iteration);
return newArray;
}
};
/**
* group
*/
var group = function(by, callback) {
var newArr = {};
var finalArr = {};
var doGroup = function() {
array.map(function(item) {
var type = eval("typeof(item." + by+")");
var chaves = eval("item." + by);
if(type!="object") {
chaves = [chaves];
}
for(var x in chaves) {
var chave = chaves[x];
if(typeof chave == 'function' || typeof chave == 'object') {
break;
}
if(newArr[chave] == undefined) {
newArr[chave] = [];
}
newArr[chave].push(item);
}
});
// Cria um array de chaves para ordenar
var Sortable = [];
for(var chave in newArr) {
if(typeof newArr[chave] != "object") {
break;
}
Sortable.push(chave);
}
// Ordena as chaves
Sortable.sort(function(a, b) {
var newA = a.toLowerCase();
var newB = b.toLowerCase();
newA = removeAcento(newA);
newB = removeAcento(newB);
if (newA < newB) //sort string ascending
return -1
if (newA > newB)
return 1
return 0
});
// Une as chaves e dados num novo array
for(var i in Sortable) {
var chave = Sortable[i];
finalArr[chave] = newArr[chave];
newArr[chave] = null;
}
}
if(callback && typeof callback=='function') {
window.setTimeout(function() {
doGroup();
callback.call(this, finalArr);
}, 0);
} else {
doGroup();
return finalArr;
}
};
return {
group: group,
filter: filter
}
}