-
Notifications
You must be signed in to change notification settings - Fork 0
/
plain.test.js
77 lines (69 loc) · 1.65 KB
/
plain.test.js
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
const { plain } = require("../src/datamix");
describe("plain", () => {
it("returns empty objects as is", function () {
expect(plain({})).toStrictEqual({});
});
it("returns an entry for empty objects", function () {
expect(plain({o: {}})).toStrictEqual({o: {}});
});
it("returns an entry for empty arrays", function () {
expect(plain({l: []})).toStrictEqual({l: []});
});
it("returns shallow object as is", function () {
expect(plain({a: 1, b: 2})).toStrictEqual({a: 1, b: 2});
});
it("returns paths of deep objects", function () {
expect(plain({
a: {x: 1, y: 2},
b: ['foo', "bar"],
c: [{active: false}],
})).toStrictEqual({
'a.x': 1,
'a.y': 2,
'b': ['foo', "bar"],
'c': [{active: false}],
});
});
it("returns paths of deep objects by traversing arrays", function () {
expect(plain({
a: {x: 1, y: 2},
b: 'foo',
c: [{bar: 'baz'}],
}, true)).toStrictEqual({
'a.x': 1,
'a.y': 2,
'b': 'foo',
'c.0.bar': 'baz',
});
});
it("returns paths of arrays", function () {
// NOTE: result === input in this case because there's
// only one array that can't be traversed
expect(plain({
list: [
{a: 1},
{b: 2},
{d: 4},
]
}, false)).toStrictEqual({
list: [
{a: 1},
{b: 2},
{d: 4},
]
});
});
it("returns paths of arrays by traversing arrays", function () {
expect(plain({
list: [
{a: 1},
{b: 2},
{d: 4},
]
}, true)).toStrictEqual({
'list.0.a': 1,
'list.1.b': 2,
'list.2.d': 4,
});
});
});