-
Notifications
You must be signed in to change notification settings - Fork 83
/
SimpleQuery.js
233 lines (208 loc) · 6.25 KB
/
SimpleQuery.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
define([
'dojo/_base/declare',
'dojo/_base/array'
], function (declare, arrayUtil) {
// module:
// dstore/SimpleQuery
function makeGetter(property, queryAccessors) {
if (property.indexOf('.') > -1) {
var propertyPath = property.split('.');
var pathLength = propertyPath.length;
return function (object) {
for (var i = 0; i < pathLength; i++) {
object = object && (queryAccessors && object.get ? object.get(propertyPath[i]) : object[propertyPath[i]]);
}
return object;
};
}
// else
return function (object) {
return object.get ? object.get(property) : object[property];
};
}
var comparators = {
eq: function eq(value, required) {
return value === required;
},
'in': function inComparator(value, required) {
// allow for a collection of data
return arrayUtil.indexOf(required.data || required, value) > -1;
},
ne: function ne(value, required) {
return value !== required;
},
lt: function lt(value, required) {
return value < required;
},
lte: function lte(value, required) {
return value <= required;
},
gt: function gt(value, required) {
return value > required;
},
gte: function gte(value, required) {
return value >= required;
},
match: function match(value, required, object) {
return required.test(value, object);
},
contains: function contains(value, required, object, key) {
var collection = this;
var data;
if (required.data) {
data = required.data;
} else if(Array.isArray(required)) {
data = required;
} else {
data = [required];
}
return arrayUtil.every(data, function (requiredValue) {
if (typeof requiredValue === 'object' && requiredValue.type) {
var comparator = collection._getFilterComparator(requiredValue.type);
return arrayUtil.some(value, function (item) {
return comparator.call(collection, item, requiredValue.args[1], object, key);
});
}
return arrayUtil.indexOf(value, requiredValue) > -1;
});
}
};
return declare(null, {
// summary:
// Mixin providing querier factories for core query types
_createFilterQuerier: function (filter) {
// create our matching filter function
var queryAccessors = this.queryAccessors;
var collection = this;
var querier = getQuerier(filter);
function getQuerier(filter) {
var currentQuerier;
var nextQuerier;
var type = filter.type;
var args = filter.args;
var comparatorFunction = collection._getFilterComparator(type);
if (comparatorFunction) {
// it is a comparator
var firstArg = args[0];
var getProperty = makeGetter(firstArg, queryAccessors);
var secondArg = args[1];
if (secondArg && secondArg.fetchSync) {
// if it is a collection, fetch the contents (for `in` and `contains` operators)
secondArg = secondArg.fetchSync();
}
return function comparator(object) {
// get the value for the property and compare to expected value
return comparatorFunction.call(collection, getProperty(object), secondArg, object, firstArg);
};
}
switch (type) {
case 'and': case 'or':
for (var i = 0, l = args.length; i < l; i++) {
// combine filters, using and or or
nextQuerier = getQuerier(args[i]);
if (currentQuerier) {
// combine the last querier with a new one
currentQuerier = (function(a, b) {
return type === 'and' ?
function and(object) {
return a(object) && b(object);
} :
function or(object) {
return a(object) || b(object);
};
})(currentQuerier, nextQuerier);
} else {
currentQuerier = nextQuerier;
}
}
return currentQuerier;
case 'function':
return args[0];
case 'string':
// named filter
var filterFunction = collection[args[0]];
if (!filterFunction) {
throw new Error('No filter function ' + args[0] + ' was found in the collection');
}
return filterFunction;
case undefined:
return function () {
return true;
};
default:
throw new Error('Unknown filter operation "' + type + '"');
}
}
return function (data) {
return arrayUtil.filter(data, querier);
};
},
_getFilterComparator: function (type) {
// summary:
// Get the comparator for the specified type
// returns: Function?
return comparators[type] || this.inherited(arguments);
},
_createSelectQuerier: function (properties) {
return function (data) {
var l = properties.length;
return arrayUtil.map(data, properties instanceof Array ?
// array of properties
function (object) {
var selectedObject = {};
for (var i = 0; i < l; i++) {
var property = properties[i];
selectedObject[property] = object[property];
}
return selectedObject;
} :
// single property
function (object) {
return object[properties];
});
};
},
_createSortQuerier: function (sortOptions) {
var queryAccessors = this.queryAccessors;
return function (data) {
data = data.slice();
if (typeof sortOptions === 'function') {
data.sort(sortOptions);
}
else if (sortOptions && sortOptions.length) {
data.sort(function (a, b) {
for (var i = 0; i < sortOptions.length; i++) {
var comparison;
var sorter = sortOptions[i];
if (typeof sorter == 'function') {
comparison = sorter(a, b);
} else {
var getProperty = sorter.get || (sorter.get = makeGetter(sorter.property, queryAccessors));
var descending = sorter.descending;
var aValue = getProperty(a);
var bValue = getProperty(b);
aValue != null && (aValue = aValue.valueOf());
bValue != null && (bValue = bValue.valueOf());
if (aValue === bValue) {
comparison = 0;
}
else {
// Prioritize undefined > null > defined
var isALessThanB = typeof bValue === 'undefined' ||
bValue === null && typeof aValue !== 'undefined' ||
aValue != null && aValue < bValue;
comparison = Boolean(descending) === isALessThanB ? 1 : -1;
}
}
if (comparison !== 0) {
return comparison;
}
}
return 0;
});
}
return data;
};
}
});
});