-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.test.js
114 lines (102 loc) · 2.97 KB
/
index.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
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
const supick = require("./index.js");
test("picks 'a' from object with a and b properties", () => {
const input = { a: 1, b: 2 };
const schema = "a";
const output = { a: 1 };
expect(supick(input, schema)).toStrictEqual(output);
});
test("picks 'a' and 'b' from object with 'a','b','c' properties", () => {
const input = { a: 1, b: 2, c: 3 };
const schema = "a, b";
const output = { a: 1, b: 2 };
expect(supick(input, schema)).toStrictEqual(output);
});
test("picks 'a' and 'b' from array of objects with 'a','b','c' properties", () => {
const input = [
{ a: 1, b: 2, c: 3 },
{ a: 4, b: 5, c: 6 },
{ a: 7, b: 8, c: 9 }
];
const schema = "a, b";
const output = [{ a: 1, b: 2 }, { a: 4, b: 5 }, { a: 7, b: 8 }];
expect(supick(input, schema)).toStrictEqual(output);
});
test("picks 'a' and nested 'c' and 'e' properties in 'b'", () => {
const input = { a: 1, b: { c: 2, d: 3, e: 4 } };
const schema = "a, b: {c, e}";
const output = { a: 1, b: { c: 2, e: 4 } };
expect(supick(input, schema)).toStrictEqual(output);
});
const personInput = {
name: "John Doe",
age: 35,
hobby: "cars",
family: {
wife: { name: "Susan", age: 32, hobby: "baseball" },
children: [
{ name: "Alex", age: 5, hobby: "dancing" },
{ name: "Alice", age: 3, hobby: "music" }
]
},
cars: [
{ model: "BMW X5", year: 2015 },
{ model: "Tesla Model S", year: 2018 }
]
};
const personOutput = {
name: "John Doe",
age: 35,
family: {
wife: { name: "Susan", age: 32 },
children: [{ name: "Alex", age: 5 }, { name: "Alice", age: 3 }]
},
cars: [{ model: "BMW X5" }, { model: "Tesla Model S" }]
};
test("picks properties from nested objects and arrays using string schema", () => {
const schema = `
{
name,
age,
family: {
wife: { name, age },
children: { name, age }
},
cars: { model }
}`;
expect(supick(personInput, schema)).toStrictEqual(personOutput);
});
test("picks properties from nested objects and arrays using array schema (lodash style)", () => {
const schema = [
"name",
"age",
"family:{wife: {name, age}, children: {name, age}}",
"cars:{model}"
];
expect(supick(personInput, schema)).toStrictEqual(personOutput);
});
test("picks properties from nested objects and arrays using object schema (mongodb projection-style)", () => {
const schema = {
name: true,
age: true,
family: {
wife: {
name: true,
age: true
},
children: {
name: 1,
age: 1
}
},
cars: { model: 1 }
};
expect(supick(personInput, schema)).toStrictEqual(personOutput);
});
test("picks properties from nested objects and arrays using customizer function", () => {
const customizer = function (key, value, path, depth) {
if (['name', 'age'].includes(key)) return true;
if (path === 'cars.model') return true;
return false;
}
expect(supick(personInput, customizer)).toStrictEqual(personOutput);
});