Skip to content

Commit ec776e3

Browse files
committed
feat(wip): added a new string compare function
1 parent c65072a commit ec776e3

2 files changed

Lines changed: 202 additions & 19 deletions

File tree

src/helpers/sort.test.ts

Lines changed: 120 additions & 3 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 } from "./sort";
2+
import { swap, defaultCompare, sortProducts, ProductTuple, newSortProducts, compareStrings, Compare } from "./sort";
33

44
describe("swap", () => {
55
it("should swap two items in an array", () => {
@@ -217,7 +217,6 @@ describe("new sort products", () => {
217217
];
218218
const [, ...rows] = data;
219219
const result = newSortProducts(rows, "ascending");
220-
console.log("result", result);
221220
expect(result).toEqual([
222221
["5", "1", "AB 7"],
223222
["4", "1", "AB 9"],
@@ -230,6 +229,124 @@ describe("new sort products", () => {
230229
});
231230

232231
it("should sort an array of products by pick location in ascending order from A 1 to AZ 10", () => {
233-
//
232+
const data: ProductTuple[] = [
233+
["product_code", "quantity", "pick_location"],
234+
["1", "1", "A 1"],
235+
["2", "1", "Z 1"],
236+
["3", "1", "AB 10"],
237+
["9", "1", "AZ 7"]
238+
];
239+
const [, ...rows] = data;
240+
const result = newSortProducts(rows, "ascending");
241+
console.log("result", result);
242+
expect(result).toEqual([
243+
["1", "1", "A 1"],
244+
["2", "1", "Z 1"],
245+
["3", "1", "AB 10"],
246+
["9", "1", "AZ 7"]
247+
]);
234248
});
235249
});
250+
251+
describe("compareStrings", () => {
252+
// it("should return undefined if nothing matches", () => {
253+
// // TODO empty case
254+
// });
255+
describe("single char", () => {
256+
// Case 1: A, A - single chars, both match
257+
it("should return EQUALS if single chars, both match", () => {
258+
const data = ["A", "A"];
259+
const result = compareStrings(data[0], data[1]);
260+
expect(result).toEqual(Compare.EQUALS);
261+
});
262+
263+
// Case 2: A, Z || Z, A - single chars, no match
264+
it("should return BIGGER THAN if single chars, no match", () => {
265+
const data = ["Z", "A"];
266+
const result = compareStrings(data[0], data[1]);
267+
expect(result).toEqual(Compare.BIGGER_THAN);
268+
});
269+
270+
it("should return LESS THAN if single chars, no match", () => {
271+
const data = ["A", "Z"];
272+
const result = compareStrings(data[0], data[1]);
273+
expect(result).toEqual(Compare.LESS_THAN);
274+
});
275+
});
276+
277+
describe("multiple chars", () => {
278+
// Case 3: AA, AA - multiple chars, both match
279+
it("should return EQUALS if the first and second letters are the same", () => {
280+
const data = ["AA", "AA"];
281+
const result = compareStrings(data[0], data[1]);
282+
expect(result).toEqual(Compare.EQUALS);
283+
});
284+
285+
// Case 4: AA, AZ - multiple chars, only first letters match
286+
it("should return LESS THAN if the first letters match but the second char is lower", () => {
287+
const data = ["AA", "AB"];
288+
const result = compareStrings(data[0], data[1]);
289+
expect(result).toEqual(Compare.LESS_THAN);
290+
});
291+
292+
it("should return BIGGER THAN if the first letters match but the second char is higher", () => {
293+
const data = ["AB", "AA"];
294+
const result = compareStrings(data[0], data[1]);
295+
expect(result).toEqual(Compare.BIGGER_THAN);
296+
});
297+
298+
// Case 5: ZA, AA - multiple chars, only second letters match
299+
it("should return LESS THAN if the first letters dont match and only second letters match", () => {
300+
const data = ["AA", "BA"];
301+
const result = compareStrings(data[0], data[1]);
302+
expect(result).toEqual(Compare.LESS_THAN);
303+
});
304+
305+
it("should return BIGGER THAN if the first letters dont match and only second letters match", () => {
306+
const data = ["BA", "AA"];
307+
const result = compareStrings(data[0], data[1]);
308+
expect(result).toEqual(Compare.BIGGER_THAN);
309+
});
310+
311+
// Case 6: ZA, AZ - multiple chars, no match
312+
it("should return BIGGER THAN if no chars match and the first char is lower", () => {
313+
const data = [
314+
["ZA", "AZ"],
315+
["XY", "AK"]
316+
];
317+
data.forEach(([a, b]) => {
318+
const result = compareStrings(a, b);
319+
expect(result).toEqual(Compare.BIGGER_THAN);
320+
});
321+
});
322+
323+
it("should return LESS THAN if no chars match and the first char is higher", () => {
324+
const data = [
325+
["AZ", "ZA"],
326+
["KA", "XY"]
327+
];
328+
data.forEach(([a, b]) => {
329+
const result = compareStrings(a, b);
330+
expect(result).toEqual(Compare.LESS_THAN);
331+
});
332+
});
333+
334+
// 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", () => {
336+
const data = ["AA", "Z"];
337+
const result = compareStrings(data[0], data[1]);
338+
expect(result).toEqual(Compare.LESS_THAN);
339+
});
340+
341+
// 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", () => {
343+
const data = ["Z", "AA"];
344+
const result = compareStrings(data[0], data[1]);
345+
expect(result).toEqual(Compare.BIGGER_THAN);
346+
});
347+
});
348+
349+
// describe("numeric shelf height", () => {
350+
// //
351+
// });
352+
});

src/helpers/sort.ts

Lines changed: 82 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const Compare = {
1+
export const Compare = {
22
LESS_THAN: -1,
33
BIGGER_THAN: 1,
44
EQUALS: 0
@@ -64,20 +64,78 @@ const filterProductsById = (products: ProductTuple[]) => {
6464
return [...productsMap.values()];
6565
};
6666

67+
export const compareStrings = (a: string, b: string) => {
68+
// Always comparing a to b
69+
70+
// Single chars
71+
// Case 1: A, A - single chars, both match
72+
// Case 2: A, Z || Z, A - single chars, no match
73+
74+
// Multiple chars
75+
// Case 3: AA, AA - multiple chars, both match
76+
// Case 4: AA, AZ - multiple chars, only first letters match
77+
// Case 5: ZA, AA - multiple chars, only second letters match
78+
// Case 6: ZA, AZ - multiple chars, no match
79+
80+
// Case 7: AA, Z - multiple chars, and single char
81+
// Case 8: Z, AA - single char, and multiple chars
82+
83+
if (a.length === 1 && b.length === 1) {
84+
// Case 1: A, A - single chars, both match
85+
if (a === b) {
86+
return Compare.EQUALS;
87+
}
88+
// Case 2: A, Z || Z, A - single chars, no match
89+
return a < b ? Compare.LESS_THAN : Compare.BIGGER_THAN;
90+
}
91+
92+
if (a.length > 1 && b.length > 1) {
93+
const [firstLetterA, secondLetterA] = a.split("");
94+
const [firstLetterB, secondLetterB] = b.split("");
95+
96+
if (firstLetterA === firstLetterB) {
97+
if (secondLetterA === secondLetterB) {
98+
// Case 3: AA, AA - multiple chars, both match
99+
return Compare.EQUALS;
100+
}
101+
102+
// Case 4: AA, AZ - multiple chars, only first letters match
103+
return secondLetterA < secondLetterB ? Compare.LESS_THAN : Compare.BIGGER_THAN;
104+
}
105+
106+
// Case 6: ZA, AZ - multiple chars, no match
107+
if (firstLetterA !== firstLetterB && secondLetterA !== secondLetterB) {
108+
return firstLetterA < firstLetterB ? Compare.LESS_THAN : Compare.BIGGER_THAN;
109+
}
110+
111+
// Case 5: ZA, AA - multiple chars, only second letters match
112+
return firstLetterA < firstLetterB ? Compare.LESS_THAN : Compare.BIGGER_THAN;
113+
}
114+
115+
// Case 7: AA, Z - multiple chars, and single char
116+
if (a.length > 1 && b.length === 1) {
117+
return Compare.LESS_THAN;
118+
}
119+
120+
// Case 8: Z, AA - single char, and multiple chars
121+
if (a.length === 1 && b.length > 1) {
122+
return Compare.BIGGER_THAN;
123+
}
124+
125+
// TODO default case and return value
126+
return undefined;
127+
};
128+
67129
export const newSortProducts = (products: ProductTuple[], method: SortMethod) => {
68-
const filtered = filterProductsById(products);
69-
const result = [...filtered];
130+
const result = filterProductsById(products);
70131

71132
for (let p = 0; p < products.length; p++) {
72-
// iterate over products and check for uniques
73-
const product = products[p];
74-
const [id] = product;
133+
// iterate over products
75134

76135
for (let i = 0; i < result.length; i++) {
77136
// sort each product by pick location
78137
const currentProduct = result[i];
79138
const nextProduct = result[i + 1];
80-
console.log(currentProduct, nextProduct);
81139

82140
if (!nextProduct) {
83141
break;
@@ -88,18 +146,26 @@ export const newSortProducts = (products: ProductTuple[], method: SortMethod) =>
88146
const [currentBay, currentShelf] = currentPickLocation.split(" ");
89147
const [nextBay, nextShelf] = nextPickLocation.split(" ");
90148

91-
if (currentBay === nextBay) {
92-
if (Number(currentShelf) > Number(nextShelf)) {
93-
swap(result, i, i + 1);
149+
// TODO refactor this for multiple letters
150+
if (currentBay.length === 1 || currentBay.length > 1) {
151+
console.log("equal", currentBay, nextBay);
152+
153+
// top of the sort order
154+
if (currentBay < nextBay) {
155+
continue;
94156
}
95-
}
96157

97-
if (currentBay > nextBay) {
98-
swap(result, i, i + 1);
99-
}
158+
// swap based on shelf
159+
if (currentBay === nextBay) {
160+
if (Number(currentShelf) > Number(nextShelf)) {
161+
swap(result, i, i + 1);
162+
}
163+
}
100164

101-
if (currentBay < nextBay) {
102-
continue;
165+
// swap based on bay
166+
if (currentBay > nextBay) {
167+
swap(result, i, i + 1);
168+
}
103169
}
104170

105171
// if currentBay is more than one letter eg AZ not A

0 commit comments

Comments
 (0)