Skip to content

Commit 78acbdb

Browse files
committed
feat(wip): working compare
1 parent ec776e3 commit 78acbdb

2 files changed

Lines changed: 175 additions & 183 deletions

File tree

src/helpers/sort.test.ts

Lines changed: 140 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it, vi } from "vitest";
2-
import { swap, defaultCompare, sortProducts, ProductTuple, newSortProducts, compareStrings, Compare } from "./sort";
2+
import { swap, ProductTuple, sortProducts, compareStrings, Compare } from "./sort";
33

44
describe("swap", () => {
55
it("should swap two items in an array", () => {
@@ -9,138 +9,138 @@ describe("swap", () => {
99
});
1010
});
1111

12-
describe("defaultCompare", () => {
13-
it("should return -1 if a < b", () => {
14-
const result = defaultCompare(1, 2);
15-
expect(result).toBe(-1);
16-
});
17-
it("should return 1 if a > b", () => {
18-
const result = defaultCompare(2, 1);
19-
expect(result).toBe(1);
20-
});
21-
it("should return 0 if a === b", () => {
22-
const result = defaultCompare(1, 1);
23-
expect(result).toBe(0);
24-
});
25-
});
26-
27-
describe("array sort", () => {
28-
it("should sort an array of numbers", () => {
29-
const array = [4, 2, 3, 1, 5];
30-
const result = array.sort((a, b) => defaultCompare(a, b));
31-
expect(result).toEqual([1, 2, 3, 4, 5]);
32-
});
33-
34-
it("should sort an array of strings", () => {
35-
const array = ["a", "z", "ag", "ac", "ab", "ae", "ba"];
36-
const result = array.sort((a, b) => defaultCompare(a, b));
37-
expect(result).toEqual(["a", "ab", "ac", "ae", "ag", "ba", "z"]);
38-
});
39-
40-
it("should sort an array of strings that are numbers", () => {
41-
const array = ["5", "4", "3", "2", "1"];
42-
const result = array.sort((a, b) => defaultCompare(a, b));
43-
expect(result).toEqual(["1", "2", "3", "4", "5"]);
44-
});
45-
});
12+
// describe("defaultCompare", () => {
13+
// it("should return -1 if a < b", () => {
14+
// const result = defaultCompare(1, 2);
15+
// expect(result).toBe(-1);
16+
// });
17+
// it("should return 1 if a > b", () => {
18+
// const result = defaultCompare(2, 1);
19+
// expect(result).toBe(1);
20+
// });
21+
// it("should return 0 if a === b", () => {
22+
// const result = defaultCompare(1, 1);
23+
// expect(result).toBe(0);
24+
// });
25+
// });
26+
27+
// describe("array sort", () => {
28+
// it("should sort an array of numbers", () => {
29+
// const array = [4, 2, 3, 1, 5];
30+
// const result = array.sort((a, b) => defaultCompare(a, b));
31+
// expect(result).toEqual([1, 2, 3, 4, 5]);
32+
// });
33+
34+
// it("should sort an array of strings", () => {
35+
// const array = ["a", "z", "ag", "ac", "ab", "ae", "ba"];
36+
// const result = array.sort((a, b) => defaultCompare(a, b));
37+
// expect(result).toEqual(["a", "ab", "ac", "ae", "ag", "ba", "z"]);
38+
// });
39+
40+
// it("should sort an array of strings that are numbers", () => {
41+
// const array = ["5", "4", "3", "2", "1"];
42+
// const result = array.sort((a, b) => defaultCompare(a, b));
43+
// expect(result).toEqual(["1", "2", "3", "4", "5"]);
44+
// });
45+
// });
46+
47+
// describe("sortProducts", () => {
48+
// it("should sort an array of products by pick location in ascending order from A 1 to A 10", () => {
49+
// const data: ProductTuple[] = [
50+
// ["product_code", "quantity", "pick_location"],
51+
// ["B1237", "2", "A 10"],
52+
// ["B1234", "2", "A 3"],
53+
// ["B1235", "3", "A 2"],
54+
// ["B1236", "4", "A 1"]
55+
// ];
56+
// const [columns, ...rows] = data;
57+
58+
// const sortedRows = sortProducts(rows);
59+
// const result = [columns, ...sortedRows];
60+
61+
// expect(result).toEqual([
62+
// ["product_code", "quantity", "pick_location"],
63+
// ["B1236", "4", "A 1"],
64+
// ["B1235", "3", "A 2"],
65+
// ["B1234", "2", "A 3"],
66+
// ["B1237", "2", "A 10"]
67+
// ]);
68+
// });
69+
// it("should sort an array of products by pick location (bay and shelf height) in ascending order", () => {
70+
// const data: ProductTuple[] = [
71+
// ["product_code", "quantity", "pick_location"],
72+
// ["A", "10", "Z 1"],
73+
// ["B", "5", "Z 10"],
74+
// ["C", "10", "A 1"],
75+
// ["G", "1", "A 7"]
76+
// ];
77+
// const [columns, ...rows] = data;
78+
79+
// const sortedRows = sortProducts(rows);
80+
// const result = [columns, ...sortedRows];
81+
82+
// expect(result).toEqual([
83+
// ["product_code", "quantity", "pick_location"],
84+
// ["C", "10", "A 1"],
85+
// ["G", "1", "A 7"],
86+
// ["A", "10", "Z 1"],
87+
// ["B", "5", "Z 10"]
88+
// ]);
89+
// });
90+
// it("should sort an array of products by bay and shelf height in ascending order WHEN bays are the same FROM lowest to highest shelf", () => {
91+
// const data: ProductTuple[] = [
92+
// ["product_code", "quantity", "pick_location"],
93+
// ["E", "1", "AB 1"],
94+
// ["F", "1", "AB 10"],
95+
// ["H", "1", "AB 9"],
96+
// ["H", "1", "AB 7"]
97+
// ];
98+
// const [columns, ...rows] = data;
99+
100+
// const sortedRows = sortProducts(rows);
101+
// const result = [columns, ...sortedRows];
102+
103+
// expect(result).toEqual([
104+
// ["product_code", "quantity", "pick_location"],
105+
// ["E", "1", "AB 1"],
106+
// ["H", "1", "AB 7"],
107+
// ["H", "1", "AB 9"],
108+
// ["F", "1", "AB 10"]
109+
// ]);
110+
// });
111+
112+
// it("should sort by shelf height if the bays are not the same", () => {
113+
// const data: ProductTuple[] = [
114+
// ["product_code", "quantity", "pick_location"],
115+
// ["B1237", "2", "A 10"],
116+
// ["B1237", "2", "Z 10"],
117+
// ["B1234", "2", "Z 3"],
118+
// ["B1234", "2", "A 3"],
119+
// ["B1235", "3", "Z 2"],
120+
// ["B1236", "4", "Z 1"],
121+
// ["B1235", "3", "A 2"],
122+
// ["B1236", "4", "A 1"]
123+
// ];
124+
125+
// const [columns, ...rows] = data;
126+
127+
// const sortedRows = sortProducts(rows);
128+
// const result = [columns, ...sortedRows];
129+
// expect(result).toEqual([
130+
// ["product_code", "quantity", "pick_location"],
131+
// ["B1236", "4", "A 1"],
132+
// ["B1235", "3", "A 2"],
133+
// ["B1234", "2", "A 3"],
134+
// ["B1237", "2", "A 10"],
135+
// ["B1236", "4", "Z 1"],
136+
// ["B1235", "3", "Z 2"],
137+
// ["B1234", "2", "Z 3"],
138+
// ["B1237", "2", "Z 10"]
139+
// ]);
140+
// });
141+
// });
46142

47143
describe("sortProducts", () => {
48-
it("should sort an array of products by pick location in ascending order from A 1 to A 10", () => {
49-
const data: ProductTuple[] = [
50-
["product_code", "quantity", "pick_location"],
51-
["B1237", "2", "A 10"],
52-
["B1234", "2", "A 3"],
53-
["B1235", "3", "A 2"],
54-
["B1236", "4", "A 1"]
55-
];
56-
const [columns, ...rows] = data;
57-
58-
const sortedRows = sortProducts(rows);
59-
const result = [columns, ...sortedRows];
60-
61-
expect(result).toEqual([
62-
["product_code", "quantity", "pick_location"],
63-
["B1236", "4", "A 1"],
64-
["B1235", "3", "A 2"],
65-
["B1234", "2", "A 3"],
66-
["B1237", "2", "A 10"]
67-
]);
68-
});
69-
it("should sort an array of products by pick location (bay and shelf height) in ascending order", () => {
70-
const data: ProductTuple[] = [
71-
["product_code", "quantity", "pick_location"],
72-
["A", "10", "Z 1"],
73-
["B", "5", "Z 10"],
74-
["C", "10", "A 1"],
75-
["G", "1", "A 7"]
76-
];
77-
const [columns, ...rows] = data;
78-
79-
const sortedRows = sortProducts(rows);
80-
const result = [columns, ...sortedRows];
81-
82-
expect(result).toEqual([
83-
["product_code", "quantity", "pick_location"],
84-
["C", "10", "A 1"],
85-
["G", "1", "A 7"],
86-
["A", "10", "Z 1"],
87-
["B", "5", "Z 10"]
88-
]);
89-
});
90-
it("should sort an array of products by bay and shelf height in ascending order WHEN bays are the same FROM lowest to highest shelf", () => {
91-
const data: ProductTuple[] = [
92-
["product_code", "quantity", "pick_location"],
93-
["E", "1", "AB 1"],
94-
["F", "1", "AB 10"],
95-
["H", "1", "AB 9"],
96-
["H", "1", "AB 7"]
97-
];
98-
const [columns, ...rows] = data;
99-
100-
const sortedRows = sortProducts(rows);
101-
const result = [columns, ...sortedRows];
102-
103-
expect(result).toEqual([
104-
["product_code", "quantity", "pick_location"],
105-
["E", "1", "AB 1"],
106-
["H", "1", "AB 7"],
107-
["H", "1", "AB 9"],
108-
["F", "1", "AB 10"]
109-
]);
110-
});
111-
112-
it("should sort by shelf height if the bays are not the same", () => {
113-
const data: ProductTuple[] = [
114-
["product_code", "quantity", "pick_location"],
115-
["B1237", "2", "A 10"],
116-
["B1237", "2", "Z 10"],
117-
["B1234", "2", "Z 3"],
118-
["B1234", "2", "A 3"],
119-
["B1235", "3", "Z 2"],
120-
["B1236", "4", "Z 1"],
121-
["B1235", "3", "A 2"],
122-
["B1236", "4", "A 1"]
123-
];
124-
125-
const [columns, ...rows] = data;
126-
127-
const sortedRows = sortProducts(rows);
128-
const result = [columns, ...sortedRows];
129-
expect(result).toEqual([
130-
["product_code", "quantity", "pick_location"],
131-
["B1236", "4", "A 1"],
132-
["B1235", "3", "A 2"],
133-
["B1234", "2", "A 3"],
134-
["B1237", "2", "A 10"],
135-
["B1236", "4", "Z 1"],
136-
["B1235", "3", "Z 2"],
137-
["B1234", "2", "Z 3"],
138-
["B1237", "2", "Z 10"]
139-
]);
140-
});
141-
});
142-
143-
describe("new sort products", () => {
144144
// TODO a to az
145145
const data: ProductTuple[] = [
146146
["product_code", "quantity", "pick_location"],
@@ -163,7 +163,7 @@ describe("new sort products", () => {
163163

164164
it("should sort products with the same bay by shelf height", () => {
165165
const [, ...rows] = data;
166-
const result = newSortProducts(rows, "ascending");
166+
const result = sortProducts(rows, "ascending");
167167
expect(result).toEqual([
168168
["1", "1", "AB 1"],
169169
["4", "1", "AB 7"],
@@ -174,7 +174,7 @@ describe("new sort products", () => {
174174

175175
it('should reverse the results if the method is "descending"', () => {
176176
const [, ...rows] = data;
177-
const result = newSortProducts(rows, "descending");
177+
const result = sortProducts(rows, "descending");
178178
expect(result).toEqual([
179179
["2", "1", "AB 10"],
180180
["3", "1", "AB 9"],
@@ -193,7 +193,7 @@ describe("new sort products", () => {
193193
["4", "1", "AB 7"]
194194
];
195195
const [, ...rows] = data;
196-
const result = newSortProducts(rows, "descending");
196+
const result = sortProducts(rows, "descending");
197197
expect(result).toEqual([
198198
["2", "1", "AB 10"],
199199
["3", "1", "AB 9"],
@@ -216,7 +216,7 @@ describe("new sort products", () => {
216216
["9", "1", "AZ 7"]
217217
];
218218
const [, ...rows] = data;
219-
const result = newSortProducts(rows, "ascending");
219+
const result = sortProducts(rows, "ascending");
220220
expect(result).toEqual([
221221
["5", "1", "AB 7"],
222222
["4", "1", "AB 9"],
@@ -237,8 +237,7 @@ describe("new sort products", () => {
237237
["9", "1", "AZ 7"]
238238
];
239239
const [, ...rows] = data;
240-
const result = newSortProducts(rows, "ascending");
241-
console.log("result", result);
240+
const result = sortProducts(rows, "ascending");
242241
expect(result).toEqual([
243242
["1", "1", "A 1"],
244243
["2", "1", "Z 1"],
@@ -332,17 +331,17 @@ describe("compareStrings", () => {
332331
});
333332

334333
// Case 7: AA, Z - multiple chars, and single char
335-
it("should return LESS THAN if the first string is multiple chars and the second is a single char", () => {
334+
it("should return BIGGER THAN if the first string is multiple chars and the second is a single char", () => {
336335
const data = ["AA", "Z"];
337336
const result = compareStrings(data[0], data[1]);
338-
expect(result).toEqual(Compare.LESS_THAN);
337+
expect(result).toEqual(Compare.BIGGER_THAN);
339338
});
340339

341340
// Case 8: Z, AA - single char, and multiple chars
342-
it("should return BIGGER THAN if the first string is a single char and second is multiple", () => {
341+
it("should return LESS THAN if the first string is a single char and second is multiple", () => {
343342
const data = ["Z", "AA"];
344343
const result = compareStrings(data[0], data[1]);
345-
expect(result).toEqual(Compare.BIGGER_THAN);
344+
expect(result).toEqual(Compare.LESS_THAN);
346345
});
347346
});
348347

0 commit comments

Comments
 (0)