-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
291 lines (258 loc) · 6.81 KB
/
index.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright IBM Corp. 2015,2016. All Rights Reserved.
// Node module: loopback-filters
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
var debug = require('debug')('loopback:filter');
var geo = require('./lib/geo');
module.exports = function filterNodes(nodes, filter) {
if (filter) {
debug('filter %j', filter);
// do we need some sorting?
if (filter.order) {
nodes = nodes.sort(sorting.bind(normalizeOrder(filter)));
}
var nearFilter = geo.nearFilter(filter.where);
// geo sorting
if (nearFilter) {
nodes = geo.filter(nodes, nearFilter);
}
// do we need some filtration?
if (filter.where) {
nodes = nodes ? nodes.filter(applyFilter(filter)) : nodes;
}
// field selection
if (filter.fields) {
nodes = nodes.map(selectFields(filter.fields));
}
// limit/skip
var skip = filter.skip || filter.offset || 0;
var limit = filter.limit || nodes.length;
nodes = nodes.slice(skip, skip + limit);
}
return nodes;
};
function applyFilter(filter) {
var where = filter.where;
if (typeof where === 'function') {
return where;
}
return function(obj) {
return matchesFilter(obj, filter);
};
}
function matchesFilter(obj, filter) {
var where = filter.where;
var pass = true;
var keys = Object.keys(where);
keys.forEach(function(key) {
if (key === 'and' || key === 'or') {
if (Array.isArray(where[key])) {
if (key === 'and') {
pass = where[key].every(function(cond) {
return applyFilter({ where: cond })(obj);
});
return pass;
}
if (key === 'or') {
pass = where[key].some(function(cond) {
return applyFilter({ where: cond })(obj);
});
return pass;
}
}
}
if (!test(where[key], getValue(obj, key))) {
pass = false;
}
});
return pass;
}
function toRegExp(pattern) {
if (pattern instanceof RegExp) {
return pattern;
}
var regex = '';
// Escaping user input to be treated as a literal string within a regex
// https://developer.mozilla.org
// /en-US/docs/Web/JavaScript/Guide/Regular_Expressions
// #Writing_a_Regular_Expression_Pattern
pattern = pattern.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1');
for (var i = 0, n = pattern.length; i < n; i++) {
var char = pattern.charAt(i);
if (char === '\\') {
i++; // Skip to next char
if (i < n) {
regex += pattern.charAt(i);
}
continue;
} else if (char === '%') {
regex += '.*';
} else if (char === '_') {
regex += '.';
} else if (char === '.') {
regex += '\\.';
} else if (char === '*') {
regex += '\\*';
} else {
regex += char;
}
}
return regex;
}
function test(example, value) {
if (typeof value === 'string' && (example instanceof RegExp)) {
return value.match(example);
}
if (example === undefined) {
return undefined;
}
if (typeof example === 'object' && example !== null) {
// ignore geo near filter
if (example.near) {
return true;
}
if (example.inq) {
// if (!value) return false;
for (var i = 0; i < example.inq.length; i++) {
if (example.inq[i] == value) {
return true;
}
}
return false;
}
if ('neq' in example) {
return compare(example.neq, value) !== 0;
}
if ('between' in example) {
return (testInEquality({ gte: example.between[0] }, value) &&
testInEquality({ lte: example.between[1] }, value));
}
if (example.like || example.nlike) {
var like = example.like || example.nlike;
if (typeof like === 'string') {
like = toRegExp(like);
}
if (example.like) {
return !!new RegExp(like).test(value);
}
if (example.nlike) {
return !new RegExp(like).test(value);
}
}
if (testInEquality(example, value)) {
return true;
}
}
// not strict equality
return (example !== null ? example.toString() : example) == (value != null ?
value.toString() : value);
}
/**
* Compare two values
* @param {*} val1 The 1st value
* @param {*} val2 The 2nd value
* @returns {number} 0: =, positive: >, negative <
* @private
*/
function compare(val1, val2) {
if (val1 == null || val2 == null) {
// Either val1 or val2 is null or undefined
return val1 == val2 ? 0 : NaN;
}
if (typeof val1 === 'number') {
return val1 - val2;
}
if (typeof val1 === 'string') {
return (val1 > val2) ? 1 : ((val1 < val2) ? -1 : (val1 == val2) ? 0 : NaN);
}
if (typeof val1 === 'boolean') {
return val1 - val2;
}
if (val1 instanceof Date) {
var result = val1 - val2;
return result;
}
// Return NaN if we don't know how to compare
return (val1 == val2) ? 0 : NaN;
}
function testInEquality(example, val) {
if ('gt' in example) {
return compare(val, example.gt) > 0;
}
if ('gte' in example) {
return compare(val, example.gte) >= 0;
}
if ('lt' in example) {
return compare(val, example.lt) < 0;
}
if ('lte' in example) {
return compare(val, example.lte) <= 0;
}
return false;
}
function getValue(obj, path) {
if (obj == null) {
return undefined;
}
var keys = path.split('.');
var val = obj;
for (var i = 0, n = keys.length; i < n; i++) {
val = val[keys[i]];
if (val == null) {
return val;
}
}
return val;
}
function selectFields(fields) {
// map function
return function(obj) {
var result = {};
var key;
for (var i = 0; i < fields.length; i++) {
key = fields[i];
result[key] = obj[key];
}
return result;
};
}
function sorting(a, b) {
var undefinedA, undefinedB;
for (var i = 0, l = this.length; i < l; i++) {
var aVal = getValue(a, this[i].key);
var bVal = getValue(b, this[i].key);
undefinedB = bVal === undefined && aVal !== undefined;
undefinedA = aVal === undefined && bVal !== undefined;
if (undefinedB || aVal > bVal) {
return 1 * this[i].reverse;
} else if (undefinedA || aVal < bVal) {
return -1 * this[i].reverse;
}
}
return 0;
}
function normalizeOrder(filter) {
var orders = filter.order;
// transform into an array
if (typeof orders === 'string') {
if (filter.order.indexOf(',') > -1) {
orders = filter.order.split(/,\s*/);
} else {
orders = [filter.order];
}
}
orders.forEach(function(key, i) {
var reverse = 1;
console.log(key);
var m = key.match(/\s+(A|DE)SC$/i);
if (m) {
key = key.replace(/\s+(A|DE)SC/i, '');
if (m[1].toLowerCase() === 'de') reverse = -1;
} else {
var Ctor = SyntaxError || Error;
throw new Ctor('filter.order must includ ASC of DESC');
}
orders[i] = { 'key': key, 'reverse': reverse };
});
return (filter.orders = orders);
}