-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharray_helpers.test.ts
More file actions
319 lines (261 loc) · 11.4 KB
/
array_helpers.test.ts
File metadata and controls
319 lines (261 loc) · 11.4 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
import { describe, expect, it } from 'vitest';
import { tt } from '@noeldemartin/testing';
import type { Expect } from '@noeldemartin/testing';
import { toString } from '@noeldemartin/utils/helpers/object_helpers';
import type { Equals } from '@noeldemartin/utils/types';
import {
arrayDiff,
arrayEquals,
arrayFilter,
arrayFirst,
arrayFrom,
arrayGroupBy,
arrayProject,
arrayRandomItem,
arrayRemove,
arrayReplace,
arraySorted,
arrayUnique,
arrayWhere,
arrayWithout,
arrayWithoutIndexes,
arrayZip,
reduceBy,
} from './array_helpers';
describe('Array helpers', () => {
it('compares arrays', () => {
expect(arrayEquals(['one'], ['one'])).toBe(true);
expect(arrayEquals(['one'], ['one', 'two'])).toBe(false);
expect(arrayEquals(['one', 'two'], ['two', 'one'])).toBe(false);
});
it('diffs arrays', () => {
expect(arrayDiff([], [])).toEqual({ added: [], removed: [] });
expect(arrayDiff(['foo', 'bar'], ['foo', 'baz'])).toEqual({ added: ['baz'], removed: ['bar'] });
expect(arrayDiff(['foo', 'bar'], ['bar', 'foo', 'bar'])).toEqual({ added: ['bar'], removed: [] });
});
it('diffs arrays using custom search function', () => {
const compareIds = (a: { id: number }, b: { id: number }) => a.id === b.id;
const item = (id: number) => ({ id });
expect(arrayDiff([item(0), item(42)], [item(42), item(23)], compareIds)).toEqual({
added: [item(23)],
removed: [item(0)],
});
});
it('finds item matching filter', () => {
expect(arrayFirst([0, 10, 42], (n) => n > 10)).toBe(42);
expect(arrayFirst([0, 10, 42], (n) => n > 100)).toBeNull();
});
it('gets random items', () => {
expect(arrayRandomItem([])).toBeNull();
expect(arrayRandomItem([42])).toBe(42);
expect([23, 42].includes(arrayRandomItem([23, 42]) as number)).toBe(true);
});
it('removes items', () => {
const items = ['foo', 'bar'];
expect(arrayRemove(items, 'bar')).toBe(true);
expect(arrayRemove(items, 'bar')).toBe(false);
expect(items).toEqual(['foo']);
});
it('replaces items', () => {
// Arrange
const items = ['foo', 'bar', 'baz'];
// Act
const result = arrayReplace(items, 'bar', 'qux');
// Assert
expect(result).toBe(true);
expect(items).toEqual(['foo', 'qux', 'baz']);
});
it('gets unique items', () => {
const items = ['foo', 'bar', 'baz', 'foo', 'bar'];
expect(arrayUnique(items)).toEqual(['foo', 'bar', 'baz']);
expect(items).toEqual(['foo', 'bar', 'baz', 'foo', 'bar']);
expect(arrayUnique([4, 1, 2, 1, 3, 4, 2])).toEqual([4, 1, 2, 3]);
expect(arrayUnique([4, 1, 2, 1, 3, 4, 2], (n) => toString(n % 2))).toEqual([4, 1]);
});
it('sorts items', () => {
expect(arraySorted([1, 3, 2])).toEqual([1, 2, 3]);
expect(arraySorted([1, 3, 2], 'asc')).toEqual([1, 2, 3]);
expect(arraySorted([1, 3, 2], 'desc')).toEqual([3, 2, 1]);
});
it('sorts items by field', () => {
const items = [{ name: 'Son Goku' }, { name: 'Astroboy' }, { name: 'Zetman' }];
expect(arraySorted(items, 'name').map((item) => item.name)).toEqual(['Astroboy', 'Son Goku', 'Zetman']);
expect(arraySorted(items, 'name', 'asc').map((item) => item.name)).toEqual(['Astroboy', 'Son Goku', 'Zetman']);
expect(arraySorted(items, 'name', 'desc').map((item) => item.name)).toEqual(['Zetman', 'Son Goku', 'Astroboy']);
});
it('sorts items by nested field', () => {
const items = [
{ author: { name: 'Brandom Sanderson' } },
{ author: { name: 'Arthur Conan Doyle' } },
{ author: { name: 'J.R.R. Tolkien' } },
];
expect(arraySorted(items, 'author.name').map((item) => item.author.name)).toEqual([
'Arthur Conan Doyle',
'Brandom Sanderson',
'J.R.R. Tolkien',
]);
expect(arraySorted(items, 'author.name', 'desc').map((item) => item.author.name)).toEqual([
'J.R.R. Tolkien',
'Brandom Sanderson',
'Arthur Conan Doyle',
]);
});
it('sorts items by field with undefined value', () => {
const items = [{ completed: true }, { completed: undefined }, { completed: true }];
expect(arraySorted(items, 'completed').map((item) => item.completed)).toEqual([undefined, true, true]);
expect(arraySorted(items, 'completed', 'asc').map((item) => item.completed)).toEqual([undefined, true, true]);
expect(arraySorted(items, 'completed', 'desc').map((item) => item.completed)).toEqual([true, true, undefined]);
});
it('sorts items by multiple fields', () => {
const sonGokuKid = { name: 'Son Goku', age: 11 };
const sonGokuAdult = { name: 'Son Goku', age: 31 };
const astroboy = { name: 'Astroboy', age: 18 };
const zetman = { name: 'Zetman', age: 16 };
const items = [sonGokuAdult, sonGokuKid, astroboy, zetman];
expect(arraySorted(items, ['name'])).toEqual([astroboy, sonGokuAdult, sonGokuKid, zetman]);
expect(arraySorted(items, ['name'], 'desc')).toEqual([zetman, sonGokuAdult, sonGokuKid, astroboy]);
expect(arraySorted(items, ['name', 'age'])).toEqual([astroboy, sonGokuKid, sonGokuAdult, zetman]);
expect(arraySorted(items, ['name', 'age'], 'desc')).toEqual([zetman, sonGokuAdult, sonGokuKid, astroboy]);
expect(arraySorted(items, ['age'])).toEqual([sonGokuKid, zetman, astroboy, sonGokuAdult]);
});
it('groups items', () => {
const pirates = [
{ name: 'Monkey D. Luffy', band: 'mugiwara' },
{ name: 'Roronoa Zoro', band: 'mugiwara' },
{ name: 'Nami', band: 'mugiwara' },
{ name: 'Marshall D. Teach', band: 'blackbeard' },
{ name: 'Jesus Burgess', band: 'blackbeard' },
{ name: 'Shiryu', band: 'blackbeard' },
];
expect(arrayGroupBy(pirates, 'band')).toEqual({
mugiwara: [
{ name: 'Monkey D. Luffy', band: 'mugiwara' },
{ name: 'Roronoa Zoro', band: 'mugiwara' },
{ name: 'Nami', band: 'mugiwara' },
],
blackbeard: [
{ name: 'Marshall D. Teach', band: 'blackbeard' },
{ name: 'Jesus Burgess', band: 'blackbeard' },
{ name: 'Shiryu', band: 'blackbeard' },
],
});
expect(arrayGroupBy(pirates, (pirate) => (pirate.band === 'mugiwara' ? 'good' : 'bad'))).toEqual({
good: [
{ name: 'Monkey D. Luffy', band: 'mugiwara' },
{ name: 'Roronoa Zoro', band: 'mugiwara' },
{ name: 'Nami', band: 'mugiwara' },
],
bad: [
{ name: 'Marshall D. Teach', band: 'blackbeard' },
{ name: 'Jesus Burgess', band: 'blackbeard' },
{ name: 'Shiryu', band: 'blackbeard' },
],
});
});
it('gets items without specified items', () => {
const items = ['foo', 'bar', 'baz'];
expect(arrayWithout(items, 'bar')).toEqual(['foo', 'baz']);
expect(arrayWithout(items, ['bar'])).toEqual(['foo', 'baz']);
expect(arrayWithout(items, ['foo', 'baz'])).toEqual(['bar']);
expect(items).toEqual(['foo', 'bar', 'baz']);
});
it('gets items without specified indexes', () => {
const items = ['foo', 'bar', 'baz'];
expect(arrayWithoutIndexes(items, [1])).toEqual(['foo', 'baz']);
expect(arrayWithoutIndexes(items, [0, 2])).toEqual(['bar']);
expect(items).toEqual(['foo', 'bar', 'baz']);
});
it('projects properties', () => {
// Arrange.
class User {
constructor(public role: string) {}
public isAdmin(): boolean {
return this.role === 'admin';
}
}
const admin = new User('admin');
const guest = new User('guest');
// Act & Assert
expect(arrayProject([admin, guest], 'role')).toEqual(['admin', 'guest']);
});
it('filters by methods and values', () => {
// Arrange.
class User {
constructor(public role: string) {}
public isAdmin(): boolean {
return this.role === 'admin';
}
}
const admin = new User('admin');
const guest = new User('guest');
// Act & Assert
expect(arrayWhere([admin, guest], 'isAdmin')).toEqual([admin]);
expect(arrayWhere([admin, guest], 'role', 'guest')).toEqual([guest]);
});
it('zips arrays', () => {
expect(arrayZip([1, 2, 3], [4, 5, 6])).toEqual([
[1, 4],
[2, 5],
[3, 6],
]);
});
it('creates arrays from values', () => {
class MyString extends String {}
expect(arrayFrom('foobar')).toEqual(['foobar']);
expect(arrayFrom(new String('foobar'))).toEqual([new String('foobar')]);
expect(arrayFrom(new MyString('foobar'))).toEqual([new MyString('foobar')]);
expect(arrayFrom(['foo', 'bar'])).toEqual(['foo', 'bar']);
expect(arrayFrom(new Set(['foo', 'bar']))).toEqual(['foo', 'bar']);
expect(arrayFrom(null)).toEqual([null]);
expect(arrayFrom(null, { ignoreEmptyValues: true })).toEqual([]);
});
it('reduces by key', () => {
const items = [
{ id: '1', name: 'Alice' },
{ id: '2', name: 'Bob' },
];
const result = reduceBy(items, 'id');
expect(result).toEqual({
1: { id: '1', name: 'Alice' },
2: { id: '2', name: 'Bob' },
});
});
it('reduces by key with projection', () => {
const items = [
{ id: '1', name: 'Alice' },
{ id: '2', name: 'Bob' },
];
const result = reduceBy(items, 'id', (item) => item.name);
expect(result).toEqual({
1: 'Alice',
2: 'Bob',
});
});
});
let enabled: boolean | undefined;
const filteredItems = arrayFilter(['foo' as string, null, 'bar', undefined]);
const filteredConditionalItems = arrayFilter(['foo' as string, null, enabled && 'bar', undefined]);
const filteredConstItems = arrayFilter(['foo' as const, null, enabled && 'bar', undefined]);
const arrayFromNumber = arrayFrom(42);
const arrayFromSet = arrayFrom(new Set(['foo']));
const arrayFromArray = arrayFrom([new Date()]);
const arrayFromConditional = arrayFrom(Date.now() ? 42 : [42]);
const groupedByKey = arrayGroupBy([{ id: 'Foo Bar' }], 'id');
const groupedByFunction = arrayGroupBy([{ id: 'Foo Bar' }], (item) => (item.id ? 'one' : 'two'));
describe('Array helpers types', () => {
it(
'has correct types',
tt<
| Expect<Equals<typeof filteredItems, string[]>>
| Expect<Equals<typeof filteredConditionalItems, string[]>>
| Expect<Equals<typeof filteredConstItems, ('foo' | 'bar')[]>>
| Expect<Equals<typeof arrayFromNumber, number[]>>
| Expect<Equals<typeof arrayFromSet, string[]>>
| Expect<Equals<typeof arrayFromArray, Date[]>>
| Expect<Equals<typeof arrayFromConditional, number[]>>
| Expect<Equals<keyof typeof groupedByKey, string>>
| Expect<Equals<keyof typeof groupedByFunction, 'one' | 'two'>>
| true
>(),
);
});