-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathwhere.js
58 lines (43 loc) · 1.52 KB
/
where.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
'use strict';
const values = require('../helpers/values');
const nestedValue = require('../helpers/nestedValue');
module.exports = function where(key, operator, value) {
let comparisonOperator = operator;
let comparisonValue = value;
const items = values(this.items);
if (operator === undefined || operator === true) {
return new this.constructor(items.filter(item => nestedValue(item, key)));
}
if (operator === false) {
return new this.constructor(items.filter(item => !nestedValue(item, key)));
}
if (value === undefined) {
comparisonValue = operator;
comparisonOperator = '===';
}
const collection = items.filter((item) => {
switch (comparisonOperator) {
case '==':
return nestedValue(item, key) === Number(comparisonValue)
|| nestedValue(item, key) === comparisonValue.toString();
default:
case '===':
return nestedValue(item, key) === comparisonValue;
case '!=':
case '<>':
return nestedValue(item, key) !== Number(comparisonValue)
&& nestedValue(item, key) !== comparisonValue.toString();
case '!==':
return nestedValue(item, key) !== comparisonValue;
case '<':
return nestedValue(item, key) < comparisonValue;
case '<=':
return nestedValue(item, key) <= comparisonValue;
case '>':
return nestedValue(item, key) > comparisonValue;
case '>=':
return nestedValue(item, key) >= comparisonValue;
}
});
return new this.constructor(collection);
};