-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilter.test.ts
More file actions
324 lines (239 loc) 路 10.2 KB
/
Copy pathfilter.test.ts
File metadata and controls
324 lines (239 loc) 路 10.2 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
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
import filter, { find } from '../src/index';
import data from './data.json';
describe('filter array', () => {
it('filters an array based on single a word', () => {
const city = 'Berlin';
const input = filter(data, city);
const output = [{ name: 'Alfreds Futterkiste', city: 'Berlin' }];
expect(input).toEqual(output);
});
it('filters an array with wildcard % based on single name', () => {
const city = '%erlin';
const input = filter(data, city);
const output = [{ name: 'Alfreds Futterkiste', city: 'Berlin' }];
expect(input).toEqual(output);
});
it('filters an array with wildcard % based on single name', () => {
const city = '%erli%';
const input = filter(data, city);
const output = [{ name: 'Alfreds Futterkiste', city: 'Berlin' }];
expect(input).toEqual(output);
});
it('filters an array with wildcard _ based on single name', () => {
const city = '_erlin';
const input = filter(data, city);
const output = [{ name: 'Alfreds Futterkiste', city: 'Berlin' }];
expect(input).toEqual(output);
});
it('filters an array with wildcard _ based on single name', () => {
const city = '_e_li_';
const input = filter(data, city);
const output = [{ name: 'Alfreds Futterkiste', city: 'Berlin' }];
expect(input).toEqual(output);
});
it('filters an array with wildcard _ based on single name', () => {
const city = 'B__lin';
const input = filter(data, city);
const output = [{ name: 'Alfreds Futterkiste', city: 'Berlin' }];
expect(input).toEqual(output);
});
it('filters an array based on object key value', () => {
const city = { city: 'Berlin' };
const input = filter(data, city);
const output = [{ name: 'Alfreds Futterkiste', city: 'Berlin' }];
expect(input).toEqual(output);
});
it('filters an array with wildcard % based on object key value', () => {
const city = { city: '%erlin' };
const input = filter(data, city);
const output = [{ name: 'Alfreds Futterkiste', city: 'Berlin' }];
expect(input).toEqual(output);
});
it('filters an array with wildcard % based on object key value', () => {
const obj = { city: '%erli%' };
const input = filter(data, obj);
const output = [{ name: 'Alfreds Futterkiste', city: 'Berlin' }];
expect(input).toEqual(output);
});
it('filters an array with wildcard based on object key value', () => {
const obj = { city: '_erlin' };
const input = filter(data, obj);
const output = [{ name: 'Alfreds Futterkiste', city: 'Berlin' }];
expect(input).toEqual(output);
});
it('filters an array with wildcard based on object key value', () => {
const obj = { city: '_e_li_' };
const input = filter(data, obj);
const output = [{ name: 'Alfreds Futterkiste', city: 'Berlin' }];
expect(input).toEqual(output);
});
it('filters an array with wildcard based on object key value', () => {
const obj = { city: 'B__lin' };
const input = filter(data, obj);
const output = [{ name: 'Alfreds Futterkiste', city: 'Berlin' }];
expect(input).toEqual(output);
});
it('filters an array based on a predicate function', () => {
const predicate = ({ city }: { city: string }) => city === 'Berlin';
const input = filter(data, predicate);
const output = [{ name: 'Alfreds Futterkiste', city: 'Berlin' }];
expect(input).toEqual(output);
});
it('filters an array based on two cities', () => {
const predicate = ({ city }: { city: string }) => city === 'Berlin' || city === 'London';
const input = filter(data, predicate);
const output = [
{ name: 'Alfreds Futterkiste', city: 'Berlin' },
{ name: 'Around the Horn', city: 'London' },
{ name: 'Bs Beverages', city: 'London' },
];
expect(input).toEqual(output);
});
it('filters an array based on two cities if exists', () => {
const predicate = ({ city }: { city: string }) => {
// @ts-expect-error - intentionally testing impossible condition
return city === 'Berlin' && city === 'Caracas';
};
const input = filter(data, predicate);
const output: typeof data = [];
expect(input).toEqual(output);
});
it('filters an array based on negation', () => {
const words = ['Apple', 'Banana', 'Cherry'];
const input = filter(words, '!Apple');
const output = ['Banana', 'Cherry'];
expect(input).toEqual(output);
});
it('filters an array of objects based on negation', () => {
const people = [{ name: 'Michael' }, { name: 'Sarah' }, { name: 'Jessica' }];
const input = filter(people, { name: '!Michael' });
const output = [{ name: 'Sarah' }, { name: 'Jessica' }];
expect(input).toEqual(output);
});
});
describe('Array values with OR logic (syntactic sugar for $in)', () => {
const users = [
{ name: 'Alice', email: 'alice@example.com', age: 30, city: 'Berlin' },
{ name: 'Bob', email: 'bob@example.com', age: 25, city: 'London' },
{ name: 'Charlie', email: 'charlie@example.com', age: 35, city: 'Berlin' },
{ name: 'David', email: 'david@example.com', age: 30, city: 'Paris' },
];
it('filters with OR logic when property value is an array', () => {
const result = filter(users, { city: ['Berlin', 'London'] });
expect(result).toHaveLength(3);
expect(result).toEqual([
{ name: 'Alice', email: 'alice@example.com', age: 30, city: 'Berlin' },
{ name: 'Bob', email: 'bob@example.com', age: 25, city: 'London' },
{ name: 'Charlie', email: 'charlie@example.com', age: 35, city: 'Berlin' },
]);
});
it('combines array OR with other AND conditions', () => {
const result = filter(users, { city: ['Berlin', 'London'], age: 30 });
expect(result).toHaveLength(1);
expect(result).toEqual([
{ name: 'Alice', email: 'alice@example.com', age: 30, city: 'Berlin' },
]);
});
it('supports wildcards within array values', () => {
const result = filter(users, { city: ['%erlin', 'Paris'] });
expect(result).toHaveLength(3);
expect(result.map((u) => u.city)).toEqual(['Berlin', 'Berlin', 'Paris']);
});
it('works with multiple array properties', () => {
const result = filter(users, {
city: ['Berlin', 'Paris'],
age: [30, 35],
});
// Should match: Alice (Berlin, 30), Charlie (Berlin, 35), David (Paris, 30)
// Logic: (city IN ['Berlin', 'Paris']) AND (age IN [30, 35])
expect(result).toHaveLength(3);
expect(result).toEqual([
{ name: 'Alice', email: 'alice@example.com', age: 30, city: 'Berlin' },
{ name: 'Charlie', email: 'charlie@example.com', age: 35, city: 'Berlin' },
{ name: 'David', email: 'david@example.com', age: 30, city: 'Paris' },
]);
});
it('returns empty array when no values in array match', () => {
const result = filter(users, { city: ['Tokyo', 'Madrid'] });
expect(result).toHaveLength(0);
});
it('works with single-element arrays', () => {
const result = filter(users, { city: ['Berlin'] });
expect(result).toHaveLength(2);
expect(result).toEqual([
{ name: 'Alice', email: 'alice@example.com', age: 30, city: 'Berlin' },
{ name: 'Charlie', email: 'charlie@example.com', age: 35, city: 'Berlin' },
]);
});
it('handles empty arrays by matching nothing', () => {
const result = filter(users, { city: [] });
expect(result).toHaveLength(0);
});
it('array syntax is equivalent to explicit $in operator', () => {
const resultWithArray = filter(users, { city: ['Berlin', 'London'] });
const resultWithOperator = filter(users, { city: { $in: ['Berlin', 'London'] } });
expect(resultWithArray).toEqual(resultWithOperator);
});
it('explicit $in operator takes precedence over array syntax', () => {
const resultWithOperator = filter(users, { city: { $in: ['Berlin'] } });
expect(resultWithOperator).toHaveLength(2);
expect(resultWithOperator.every((u) => u.city === 'Berlin')).toBe(true);
});
it('works with number arrays', () => {
const result = filter(users, { age: [25, 30] });
// Should match: Alice (30), Bob (25), David (30)
expect(result).toHaveLength(3);
expect(result).toEqual([
{ name: 'Alice', email: 'alice@example.com', age: 30, city: 'Berlin' },
{ name: 'Bob', email: 'bob@example.com', age: 25, city: 'London' },
{ name: 'David', email: 'david@example.com', age: 30, city: 'Paris' },
]);
});
it('works with string arrays and exact matches', () => {
const result = filter(users, { name: ['Alice', 'Bob'] });
expect(result).toHaveLength(2);
expect(result.map((u) => u.name)).toEqual(['Alice', 'Bob']);
});
it('combines multiple conditions with different types', () => {
const result = filter(users, {
city: ['Berlin', 'Paris'],
age: 30,
name: ['Alice', 'David'],
});
// Should match: Alice (Berlin, 30, Alice) and David (Paris, 30, David)
// Logic: (city IN ['Berlin', 'Paris']) AND (age === 30) AND (name IN ['Alice', 'David'])
expect(result).toHaveLength(2);
expect(result).toEqual([
{ name: 'Alice', email: 'alice@example.com', age: 30, city: 'Berlin' },
{ name: 'David', email: 'david@example.com', age: 30, city: 'Paris' },
]);
});
it('works with wildcards in array for partial matching', () => {
const result = filter(users, { city: ['%ondon', '%aris'] });
expect(result).toHaveLength(2);
expect(result.map((u) => u.city).sort()).toEqual(['London', 'Paris']);
});
it('works with underscore wildcard in arrays', () => {
const result = filter(users, { city: ['_erlin', 'L_ndon'] });
expect(result).toHaveLength(3);
expect(result.map((u) => u.city).sort()).toEqual(['Berlin', 'Berlin', 'London']);
});
});
describe('find alias', () => {
it('returns same results as filter', () => {
expect(find(data, 'Berlin')).toEqual(filter(data, 'Berlin'));
});
it('works with object expression', () => {
const city = 'Berlin';
expect(find(data, { city })).toEqual(filter(data, { city }));
});
it('works with predicate function', () => {
const pred = (item: (typeof data)[0]) => item.city === 'Berlin';
expect(find(data, pred)).toEqual(filter(data, pred));
});
it('works with options', () => {
expect(find(data, 'berlin', { caseSensitive: false })).toEqual(
filter(data, 'berlin', { caseSensitive: false }),
);
});
});