forked from rocicorp/mono
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.test.ts
More file actions
68 lines (60 loc) · 1.29 KB
/
objects.test.ts
File metadata and controls
68 lines (60 loc) · 1.29 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
import {expect, test} from 'vitest';
import {mapAllEntries, mapEntries, mapValues} from './objects.ts';
// Use JSON.stringify in expectations to preserve / verify key order.
const stringify = (o: unknown) => JSON.stringify(o, null, 2);
test('mapValues', () => {
const obj = {
foo: 'bar',
bar: 'baz',
boo: 'doo',
};
expect(stringify(mapValues(obj, v => v.toUpperCase())))
.toMatchInlineSnapshot(`
"{
"foo": "BAR",
"bar": "BAZ",
"boo": "DOO"
}"
`);
});
test('mapEntries', () => {
const obj = {
boo: 'doo',
foo: 'bar',
bar: 'baz',
};
expect(stringify(mapEntries(obj, (k, v) => [v, k]))).toMatchInlineSnapshot(`
"{
"doo": "boo",
"bar": "foo",
"baz": "bar"
}"
`);
});
test('mapAllEntries', () => {
const obj = {
foo: 'bar',
bar: 'baz',
boo: 'doo',
};
const sorted = mapAllEntries(obj, e =>
e.sort(([a], [b]) => a.localeCompare(b)),
);
expect(stringify(sorted)).toMatchInlineSnapshot(`
"{
"bar": "baz",
"boo": "doo",
"foo": "bar"
}"
`);
const reversed = mapAllEntries(obj, e =>
e.sort(([a], [b]) => a.localeCompare(b) * -1),
);
expect(stringify(reversed)).toMatchInlineSnapshot(`
"{
"foo": "bar",
"boo": "doo",
"bar": "baz"
}"
`);
});