forked from crcn/sift.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsift.browser.js
579 lines (396 loc) · 11.1 KB
/
sift.browser.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
/*
* Sift
*
* Copryright 2011, Craig Condon
* Licensed under MIT
*
* Inspired by mongodb's query language
*/
(function() {
/**
*/
var _convertDotToSubObject = function(keyParts, value) {
var subObject = {},
currentValue = subObject;
for(var i = 0, n = keyParts.length - 1; i < n; i++) {
currentValue = currentValue[keyParts[i]] = {};
}
currentValue[keyParts[i]] = value;
return subObject;
}
/**
*/
var _queryParser = new (function() {
/**
* tests against data
*/
var priority = this.priority = function(statement, data) {
var exprs = statement.exprs,
priority = 0;
//generally, expressions are ordered from least efficient, to most efficient.
for(var i = 0, n = exprs.length; i < n; i++) {
var expr = exprs[i],
p;
if(!~(p = expr.e(expr.v, _comparable(data), data))) return -1;
priority += p;
}
return priority;
}
/**
* parses a statement into something evaluable
*/
var parse = this.parse = function(statement, key) {
//fixes sift(null, []) issue
if(!statement) statement = { $eq: statement };
var testers = [];
//if the statement is an object, then we're looking at something like: { key: match }
if(Object.prototype.toString.call(statement) === "[object Object]") {
for(var k in statement) {
//find the apropriate operator. If one doesn't exist and the key does not start
//with a $ character, then it's a property, which means we create a new statement
//(traversing)
var operator;
if (!!_testers[k]) {
operator = k;
} else if (k.substr(0, 1) !== '$') {
operator = '$trav';
} else {
throw new Error('Unknown operator.');
}
//value of given statement (the match)
var value = statement[k];
//default = match
var exprValue = value;
//if we're working with a traversable operator, then set the expr value
if(TRAV_OP[operator]) {
//using dot notation? convert into a sub-object
if(~k.indexOf(".")) {
var keyParts = k.split(".");
k = keyParts.shift(); //we're using the first key, so remove it
exprValue = value = _convertDotToSubObject(keyParts, value);
}
//*if* the value is an array, then we're dealing with something like: $or, $and
if(value instanceof Array) {
exprValue = [];
for(var i = value.length; i--;) {
exprValue.push(parse(value[i]));
}
//otherwise we're dealing with $trav
} else {
exprValue = parse(value, k);
}
}
testers.push(_getExpr(operator, k, exprValue));
}
//otherwise we're comparing a particular value, so set to eq
} else {
testers.push(_getExpr('$eq', k, statement));
}
var stmt = {
exprs: testers,
k: key,
test: function(value) {
return !!~stmt.priority(value);
},
priority: function(value) {
return priority(stmt, value);
}
};
return stmt;
}
//traversable statements
var TRAV_OP = this.traversable = {
$and: true,
$or: true,
$nor: true,
$trav: true,
$not: true
};
function _comparable(value) {
if(value instanceof Date) {
return value.getTime();
} else {
return value;
}
}
function btop(value) {
return value ? 0 : -1;
}
var _testers = this.testers = {
/**
*/
$eq: function(a, b) {
return btop(a.test(b));
},
/**
*/
$ne: function(a, b) {
return btop(!a.test(b));
},
/**
*/
$lt: function(a, b) {
return btop(a > b);
},
/**
*/
$gt: function(a, b) {
return btop(a < b);
},
/**
*/
$lte: function(a, b) {
return btop(a >= b);
},
/**
*/
$gte: function(a, b) {
return btop(a <= b);
},
/**
*/
$exists: function(a, b) {
return btop(a === (b != null))
},
/**
*/
$in: function(a, b) {
//intersecting an array
if(b instanceof Array) {
for(var i = b.length; i--;) {
if(~a.indexOf(b[i])) return i;
}
} else {
return btop(~a.indexOf(b));
}
return -1;
},
/**
*/
$not: function(a, b) {
if(!a.test) throw new Error("$not test should include an expression, not a value. Use $ne instead.");
return btop(!a.test(b));
},
/**
*/
$type: function(a, b, org) {
//instanceof doesn't work for strings / boolean. instanceof works with inheritance
return org ? btop(org instanceof a || org.constructor == a) : -1;
},
/**
*/
$nin: function(a, b) {
return ~_testers.$in(a, b) ? -1 : 0;
},
/**
*/
$mod: function(a, b) {
return b % a[0] == a[1] ? 0 : -1;
},
/**
*/
$all: function(a, b) {
b = b || (b = [])
for(var i = a.length; i--;) {
var a1 = a[i];
var indexInB = ~b.indexOf(a1);
if(!indexInB) return -1;
}
return 0;
},
/**
*/
$size: function(a, b) {
return b ? btop(a == b.length) : -1;
},
/**
*/
$or: function(a, b) {
var i = a.length, p, n = i;
for(; i--;) {
if(~priority(a[i], b)) {
return i;
}
}
return btop(n == 0);
},
/**
*/
$nor: function(a, b) {
var i = a.length, n = i;
for(; i--;) {
if(~priority(a[i], b)) {
return -1;
}
}
return 0;
},
/**
*/
$and: function(a, b) {
for(var i = a.length; i--;) {
if(!~priority(a[i], b)) {
return -1;
}
}
return 0;
},
/**
*/
$trav: function(a, b) {
if(b instanceof Array) {
for(var i = b.length; i--;) {
var subb = b[i];
if(subb[a.k] && ~priority(a, subb[a.k])) return i;
}
return -1;
}
//continue to traverse even if there isn't a value - this is needed for
//something like name:{$exists:false}
return priority(a, b ? b[a.k] : undefined);
},
/**
*/
$regex: function(a, b) {
var aRE = new RegExp(a);
return aRE.test(b) ? 0 : -1;
}
}
var _prepare = {
/**
*/
$eq: function(a) {
var fn;
if(a instanceof RegExp) {
return a;
} else if (a instanceof Function) {
fn = a;
} else {
fn = function(b) {
if(b instanceof Array) {
return ~b.indexOf(a);
} else {
return a == b;
}
}
}
return {
test: fn
}
},
/**
*/
$ne: function(a) {
return _prepare.$eq(a);
}
};
var _getExpr = function(type, key, value) {
var v = _comparable(value);
return {
//k key
k: key,
//v value
v: _prepare[type] ? _prepare[type](v) : v,
//e eval
e: _testers[type]
};
}
})();
var getSelector = function(selector) {
if(!selector) {
return function(value) {
return value;
};
} else
if(typeof selector == 'function') {
return selector;
}
throw new Error("Unknown sift selector " + selector);
}
var sifter = function(query, selector) {
//build the filter for the sifter
var filter = _queryParser.parse( query );
//the function used to sift through the given array
var self = function(target) {
var sifted = [], results = [], testValue, value, priority;
//I'll typically start from the end, but in this case we need to keep the order
//of the array the same.
for(var i = 0, n = target.length; i < n; i++) {
value = target[i];
testValue = selector(value);
//priority = -1? it's not something we can use.
if(!~(priority = filter.priority( testValue ))) continue;
//push all the sifted values to be sorted later. This is important particularly for statements
//such as $or
sifted.push({
value: value,
priority: priority
});
}
//sort the values
sifted.sort(function(a, b) {
return a.priority > b.priority ? -1 : 1;
});
var values = Array(sifted.length);
//finally, fetch the values & return them.
for(var i = sifted.length; i--;) {
values[i] = sifted[i].value;
}
return values;
}
//set the test function incase the sifter isn't needed
self.test = filter.test;
self.score = filter.priority;
self.query = query;
return self;
}
/**
* sifts the given function
* @param query the mongodb query
* @param target the target array
* @param rawSelector the selector for plucking data from the given target
*/
var sift = function(query, target, rawSelector) {
//must be an array
if(typeof target != "object") {
rawSelector = target;
target = undefined;
}
var sft = sifter(query, getSelector(rawSelector));
//target given? sift through it and return the filtered result
if(target) return sft(target);
//otherwise return the sifter func
return sft;
}
sift.use = function(options) {
if(options.operators) sift.useOperators(options.operators);
}
sift.useOperators = function(operators) {
for(var key in operators) {
sift.useOperator(key, operators[key]);
}
}
sift.useOperator = function(operator, optionsOrFn) {
var options = {};
if(typeof optionsOrFn == "object") {
options = optionsOrFn;
} else {
options = { test: optionsOrFn };
}
var key = "$" + operator;
_queryParser.testers[key] = options.test;
if(options.traversable || options.traverse) {
_queryParser.traversable[key] = true;
}
}
//node.js?
if((typeof module != 'undefined') && (typeof module.exports != 'undefined')) {
module.exports = sift;
} else
//browser?
if(typeof window != 'undefined') {
window.sift = sift;
}
})();
},{}]},{},[1])