-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
158 lines (136 loc) · 5.49 KB
/
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
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
const sortSheet = require("./index");
it("should throw an error if first argument is not an array", () => {
expect(() => sortSheet(1)).toThrowError("Expected an array");
});
it("should throw an error if second argument is not an array", () => {
expect(() => sortSheet([], 1)).toThrowError("Expected an array");
});
it("should throw an error if there is not at least one sortBy key", () => {
expect(() => sortSheet([], [{}])).toThrowError("sortBy key is required");
});
it("should order by 'asc' by default", () => {
expect(sortSheet([{ k1: 5 }, { k1: 2 }], [{ sortBy: "k1" }])).toEqual([{ k1: 2 }, { k1: 5 }]);
});
it("should copy the initial array", () => {
const arrayToSorted = [{ k1: 5 }, { k1: 2 }];
expect(sortSheet(arrayToSorted, [{ orderBy: "asc", sortBy: "k1" }])).toEqual([{ k1: 2 }, { k1: 5 }]);
expect(arrayToSorted).toEqual([{ k1: 5 }, { k1: 2 }]);
});
it("should keep indice with following values: undefined, null, empty string", () => {
const arrayToSorted = [{ k1: undefined }, { k1: null }, { k1: undefined }, { k1: "" }, { k1: null }];
expect(sortSheet(arrayToSorted, [{ orderBy: "asc", sortBy: "k1" }])).toEqual(arrayToSorted);
});
it("should sort boolean", () => {
const arrayToSorted = [{ k1: false }, { k1: true }, { k1: false }];
expect(sortSheet(arrayToSorted, [{ orderBy: "asc", sortBy: "k1" }])).toEqual([
{ k1: false },
{ k1: false },
{ k1: true },
]);
expect(sortSheet(arrayToSorted, [{ orderBy: "desc", sortBy: "k1" }])).toEqual([
{ k1: true },
{ k1: false },
{ k1: false },
]);
});
it("should sort number", () => {
const arrayToSorted = [{ k1: 1 }, { k1: 3 }, { k1: 2 }];
expect(sortSheet(arrayToSorted, [{ orderBy: "asc", sortBy: "k1" }])).toEqual([
{ k1: 1 },
{ k1: 2 },
{ k1: 3 },
]);
expect(sortSheet(arrayToSorted, [{ orderBy: "desc", sortBy: "k1" }])).toEqual([
{ k1: 3 },
{ k1: 2 },
{ k1: 1 },
]);
});
it("should sort string", () => {
const arrayToSorted = [{ k1: "foo" }, { k1: "bar" }, { k1: "baz" }];
expect(sortSheet(arrayToSorted, [{ orderBy: "asc", sortBy: "k1" }])).toEqual([
{ k1: "bar" },
{ k1: "baz" },
{ k1: "foo" },
]);
expect(sortSheet(arrayToSorted, [{ orderBy: "desc", sortBy: "k1" }])).toEqual([
{ k1: "foo" },
{ k1: "baz" },
{ k1: "bar" },
]);
});
it("should sort date", () => {
const arrayToSorted = [
{ k1: new Date(2020, 10, 8) },
{ k1: new Date(2020, 10, 6) },
{ k1: new Date(2020, 10, 9) },
];
expect(sortSheet(arrayToSorted, [{ orderBy: "asc", sortBy: "k1" }])).toEqual([
{ k1: new Date(2020, 10, 6) },
{ k1: new Date(2020, 10, 8) },
{ k1: new Date(2020, 10, 9) },
]);
expect(sortSheet(arrayToSorted, [{ orderBy: "desc", sortBy: "k1" }])).toEqual([
{ k1: new Date(2020, 10, 9) },
{ k1: new Date(2020, 10, 8) },
{ k1: new Date(2020, 10, 6) },
]);
});
it("should sort with compare function", () => {
const arrayToSorted = [{ k1: [0] }, { k1: [0, 0, 0, 0] }, { k1: [0, 0] }, { k1: [0, 0, 0, 0, 0] }];
const arrayToSorted2 = [{ k1: "c" }, { k1: "d" }, { k1: "b" }];
expect(sortSheet(arrayToSorted, [{ orderBy: (a, b) => a.length - b.length, sortBy: "k1" }])).toEqual([
{ k1: [0] },
{ k1: [0, 0] },
{ k1: [0, 0, 0, 0] },
{ k1: [0, 0, 0, 0, 0] },
]);
expect(sortSheet(arrayToSorted2, [{ orderBy: (a, b) => -a.localeCompare(b), sortBy: "k1" }])).toEqual([
{ k1: "d" },
{ k1: "c" },
{ k1: "b" },
]);
});
it("should sort multiple columns", () => {
const arrayToSorted = [
{ k1: "a", k2: 1, k3: { k4: { k5: new Date(2020, 10, 8, 22) } } },
{ k1: "w", k2: 40, k3: { k4: null } },
{ k1: "a", k2: 4, k3: { k4: { k5: new Date(2020, 10, 7, 22) } } },
{ k1: "w", k2: 100, k3: { k4: { k5: new Date(2020, 9, 8, 22) } } },
{ k1: "z", k2: 1, k3: { k4: { k5: new Date(2020, 9, 8, 22) } } },
{ k1: "j", k2: 5, k3: { k4: { k5: new Date(2020, 9, 8, 22) } } },
{ k1: "a", k2: 4, k3: { k4: { k5: new Date(2020, 8, 8, 22) } } },
{ k1: "a", k2: 2, k3: { k4: { k5: new Date(2020, 8, 8, 22) } } },
{ k1: "a", k2: 1, k3: { k4: { k5: new Date(2020, 9, 8, 22) } } },
{ k1: "w", k2: 40, k3: { k4: { k5: new Date(2020, 10, 8, 22) } } },
{ k1: "w", k2: 41, k3: { k4: { k5: new Date(2020, 10, 8, 22) } } },
{ k1: "w", k2: 41, k3: { k4: undefined } },
];
const arraySorted = sortSheet(arrayToSorted, [
{ orderBy: "asc", sortBy: "k1" },
{ orderBy: "desc", sortBy: "k3.k4.k5" },
{ orderBy: (a, b) => a - b, sortBy: "k2" },
]);
const fooBar = [
{ k1: "a", k2: 6, k3: "foo" },
{ k1: "b", k2: 7, k3: "bar" },
{ k1: "a", k2: 6, k3: "baz" },
{ k1: "a", k2: 9, k3: "rab" },
{ k1: "b", k2: 5, k3: "zab" },
{ k1: "a", k2: 6, k3: "foo" },
];
expect(arraySorted).toEqual([
{ k1: "a", k2: 1, k3: { k4: { k5: new Date(2020, 10, 8, 22) } } },
{ k1: "a", k2: 4, k3: { k4: { k5: new Date(2020, 10, 7, 22) } } },
{ k1: "a", k2: 1, k3: { k4: { k5: new Date(2020, 9, 8, 22) } } },
{ k1: "a", k2: 2, k3: { k4: { k5: new Date(2020, 8, 8, 22) } } },
{ k1: "a", k2: 4, k3: { k4: { k5: new Date(2020, 8, 8, 22) } } },
{ k1: "j", k2: 5, k3: { k4: { k5: new Date(2020, 9, 8, 22) } } },
{ k1: "w", k2: 40, k3: { k4: null } },
{ k1: "w", k2: 40, k3: { k4: { k5: new Date(2020, 10, 8, 22) } } },
{ k1: "w", k2: 41, k3: { k4: { k5: new Date(2020, 10, 8, 22) } } },
{ k1: "w", k2: 41, k3: { k4: undefined } },
{ k1: "w", k2: 100, k3: { k4: { k5: new Date(2020, 9, 8, 22) } } },
{ k1: "z", k2: 1, k3: { k4: { k5: new Date(2020, 9, 8, 22) } } },
]);
});