forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.array.test.js
More file actions
27 lines (21 loc) · 972 Bytes
/
Copy pathutils.array.test.js
File metadata and controls
27 lines (21 loc) · 972 Bytes
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
const { unique, groupBy, chunk, sortBy, flatten } = require('../src/utils/array');
describe('array utils', () => {
it('unique removes duplicates', () => expect(unique([1, 2, 2, 3])).toEqual([1, 2, 3]));
it('groupBy groups by key', () => {
const result = groupBy([{ type: 'a' }, { type: 'b' }, { type: 'a' }], (x) => x.type);
expect(result.a).toHaveLength(2);
expect(result.b).toHaveLength(1);
});
it('chunk splits array', () => {
expect(chunk([1, 2, 3, 4, 5], 2)).toEqual([[1, 2], [3, 4], [5]]);
});
it('sortBy sorts ascending', () => {
const sorted = sortBy([{ n: 3 }, { n: 1 }, { n: 2 }], 'n');
expect(sorted.map((x) => x.n)).toEqual([1, 2, 3]);
});
it('sortBy sorts descending', () => {
const sorted = sortBy([{ n: 1 }, { n: 3 }, { n: 2 }], 'n', 'desc');
expect(sorted.map((x) => x.n)).toEqual([3, 2, 1]);
});
it('flatten flattens one level', () => expect(flatten([[1, 2], [3, 4]])).toEqual([1, 2, 3, 4]));
});