forked from SitePen/dstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
objectQueryEngine.js
86 lines (79 loc) · 2.02 KB
/
objectQueryEngine.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
define([
'dojo/_base/array'
], function (arrayUtil) {
// module:
// dstore/objectQueryEngine
return {
filter: function (query) {
// create our matching query function
var queryer = query;
switch (typeof query) {
default:
throw new Error('Can not query with a ' + typeof query);
case 'object':
case 'undefined':
var queryObject = query;
queryer = function (object) {
for (var key in queryObject) {
var required = queryObject[key];
if (required && required.test) {
// an object can provide a test method, which makes it work with regex
if (!required.test(object[key], object)) {
return false;
}
} else if (required !== object[key]) {
return false;
}
}
return true;
};
break;
case 'string':
// named query
if (!this[query]) {
throw new Error('No filter function ' + query + ' was found in store');
}
queryer = this[query];
/* falls through */
case 'function':
/* falls through */
}
return function (data) {
return arrayUtil.filter(data, queryer);
};
},
/* jshint ignore:start */
sort: function (sorted) {
return function (data) {
data = data.slice();
data.sort(typeof sorted == 'function' ? sorted : function (a, b) {
for (var i = 0; i < sorted.length; i++) {
var comparison;
if (typeof sorted[i] == 'function') {
comparison = sorted[i](a, b);
} else {
var property = sorted[i].property;
var descending = sorted[i].descending;
var aValue = a[property];
var bValue = b[property];
comparison = aValue === bValue
? 0
: (!!descending === (aValue === null || aValue > bValue) ? -1 : 1);
}
if (comparison !== 0) {
return comparison;
}
}
return 0;
});
return data;
};
},
/* jshint ignore:end */
range: function (range) {
return function (data) {
return data.slice(range.start, range.end || Infinity);
};
}
};
});