basic-test.js
5.97 KB
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
import * as assert from 'assert';
import sift, {indexOf as siftIndexOf} from '..';
describe(__filename + '#', function() {
it('doesn\'t sort arrays', function () {
var values = sift({
$or: [3, 2, 1]
}, [9,8,7,6,5,4,3,2,1]);
assert.equal(values.length, 3);
assert.equal(values[0], 3);
assert.equal(values[1], 2);
assert.equal(values[2], 1);
});
it('can create a custom selector, and use it', function () {
var sifter = sift({ age: { $gt: 5}}, function (item) {
return item.person;
});
var people = [{ person: { age: 6 }}],
filtered = people.filter(sifter);
assert.equal(filtered.length, 1);
assert.equal(filtered[0], people[0]);
});
it('throws an error if the operation is invalid', function () {
var err;
try {
sift({$aaa:1})('b');
} catch (e) {
err = e;
}
assert.equal(err.message, 'Unknown operation $aaa');
});
it('can use a custom selector as the 3rd param', function () {
var people = [{ person: { age: 6 }}];
var filtered = sift({ age: { $gt: 5}}, people, function (item) {
return item.person;
});
assert.equal(filtered.length, 1);
assert.equal(filtered[0], people[0]);
});
it('can get the first index of a matching element', function () {
var index = siftIndexOf({ val: { $gt: 5}}, [{val: 4}, {val: 3}, {val: 6}, {val: 7}]);
assert.equal(index, 2);
});
it('returns -1 as index if no matching element is found', function () {
var index = siftIndexOf({ val: { $gt: 7}}, [{val: 4}, {val: 3}, {val: 6}, {val: 7}]);
assert.equal(index, -1);
});
it('can match empty arrays', function () {
var statusQuery = {$or: [{status: {$exists: false}},
{status: []},
{status: {$in: ['urgent', 'completed', 'today']}}
]};
var filtered = sift(statusQuery, [{ status: [] },
{ status: ['urgent'] },
{ status: ['nope'] }
]);
assert.equal(filtered.length, 2);
});
it('$ne: null does not hit when field is present', function(){
var sifter = sift({age: {$ne: null}});
var people = [
{age: 'matched'},
{missed: 1}
];
var filtered = people.filter(sifter);
assert.equal(filtered.length, 1);
assert.equal(filtered[0].age, 'matched');
});
it('$ne does not hit when field is different', function () {
var sifter = sift({ age: { $ne: 5 }});
var people = [{ age: 5 }],
filtered = people.filter(sifter);
assert.equal(filtered.length, 0);
});
it('$ne does hit when field exists with different value', function () {
var sifter = sift({ age: { $ne: 4 }});
var people = [{ age: 5 }],
filtered = people.filter(sifter);
assert.equal(filtered.length, 1);
});
it('$ne does hit when field does not exist', function(){
var sifter = sift({ age: { $ne: 5 }});
var people = [{}],
filtered = people.filter(sifter);
assert.equal(filtered.length, 1);
});
it('$eq matches objects that serialize to the same value', function() {
var counter = 0;
function Book(name) {
this.name = name;
this.copyNumber = counter;
this.toJSON = function() {
return this.name; // discard the copy when serializing.
};
counter += 1;
}
var warAndPeace = new Book('War and Peace');
var sifter = sift({ $eq: warAndPeace});
var books = [ new Book('War and Peace')];
var filtered = books.filter(sifter);
assert.equal(filtered.length, 1);
});
it('$neq does not match objects that serialize to the same value', function() {
var counter = 0;
function Book(name) {
this.name = name;
this.copyNumber = counter;
this.toJSON = function() {
return this.name; // discard the copy when serializing.
};
counter += 1;
}
var warAndPeace = new Book('War and Peace');
var sifter = sift({ $ne: warAndPeace});
var books = [ new Book('War and Peace')];
var filtered = books.filter(sifter);
assert.equal(filtered.length, 0);
});
// https://gist.github.com/jdnichollsc/00ea8cf1204b17d9fb9a991fbd1dfee6
it('returns a period between start and end dates', function() {
var product = {
'productTypeCode': 'productTypeEnergy',
'quantities': [
{
'period': {
'startDate': new Date('2017-01-13T05:00:00.000Z'),
'endDate': new Date('2017-01-31T05:00:00.000Z'),
'dayType': {
'normal': true,
'holiday': true
},
'specificDays': [
'monday',
'wednesday',
'friday'
],
'loadType': {
'high': true,
'medium': false,
'low': false
}
},
'type': 'DemandPercentage',
'quantityValue': '44'
},
{
'period': {
'startDate': new Date('2017-01-13T05:00:00.000Z'),
'endDate': new Date('2017-01-31T05:00:00.000Z'),
'dayType': {
'normal': true,
'holiday': true
},
'loadType': {
'high': false,
'medium': true,
'low': false
}
},
'type': 'Value',
'quantityValue': '22'
}
]
};
var period = {
'startDate': new Date('2017-01-08T05:00:00.000Z'),
'endDate': new Date('2017-01-29T05:00:00.000Z'),
'dayType': {
'normal': true,
'holiday': true
},
'loadType': {
'high': true,
'medium': false,
'low': true
},
specificPeriods : ['3', '4', '5-10']
};
var results = sift({
$and: [
{ 'period.startDate': { $lte : period.endDate } },
{ 'period.endDate': { $gte : period.startDate } }
]
}, product.quantities);
assert.equal(results.length, 2);
});
});