Skip to content

Commit c65072a

Browse files
committed
feat(wip): filter duplicates and add qty with map function
1 parent 6e7194c commit c65072a

2 files changed

Lines changed: 71 additions & 18 deletions

File tree

src/helpers/sort.test.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,36 @@ describe("new sort products", () => {
172172
]);
173173
});
174174

175+
it('should reverse the results if the method is "descending"', () => {
176+
const [, ...rows] = data;
177+
const result = newSortProducts(rows, "descending");
178+
expect(result).toEqual([
179+
["2", "1", "AB 10"],
180+
["3", "1", "AB 9"],
181+
["4", "1", "AB 7"],
182+
["1", "1", "AB 1"]
183+
]);
184+
});
185+
186+
it("should deduplicate products by id and add their qty", () => {
187+
const data: ProductTuple[] = [
188+
["product_code", "quantity", "pick_location"],
189+
["1", "1", "AB 1"],
190+
["1", "1", "AB 1"],
191+
["2", "1", "AB 10"],
192+
["3", "1", "AB 9"],
193+
["4", "1", "AB 7"]
194+
];
195+
const [, ...rows] = data;
196+
const result = newSortProducts(rows, "descending");
197+
expect(result).toEqual([
198+
["2", "1", "AB 10"],
199+
["3", "1", "AB 9"],
200+
["4", "1", "AB 7"],
201+
["1", "2", "AB 1"]
202+
]);
203+
});
204+
175205
it("should sort products that do not have the same bay by bay and then by shelf height", () => {
176206
const data: ProductTuple[] = [
177207
["product_code", "quantity", "pick_location"],
@@ -202,15 +232,4 @@ describe("new sort products", () => {
202232
it("should sort an array of products by pick location in ascending order from A 1 to AZ 10", () => {
203233
//
204234
});
205-
206-
it('should reverse the results if the method is "descending"', () => {
207-
const [, ...rows] = data;
208-
const result = newSortProducts(rows, "descending");
209-
expect(result).toEqual([
210-
["2", "1", "AB 10"],
211-
["3", "1", "AB 9"],
212-
["4", "1", "AB 7"],
213-
["1", "1", "AB 1"]
214-
]);
215-
});
216235
});

src/helpers/sort.ts

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,55 @@ export const sortProducts = (products: ProductTuple[]) => {
4040

4141
type SortMethod = "ascending" | "descending";
4242

43+
const filterProductsById = (products: ProductTuple[]) => {
44+
const productsMap = new Map<string, ProductTuple>();
45+
46+
for (let p = 0; p < products.length; p++) {
47+
const product = products[p];
48+
const [id] = product;
49+
50+
if (productsMap.has(id)) {
51+
const existingProduct = productsMap.get(id);
52+
53+
if (existingProduct?.length === 3) {
54+
const [, existingQty] = existingProduct;
55+
const [, qty] = product;
56+
existingProduct[1] = String(Number(existingQty) + Number(qty));
57+
productsMap.set(id, existingProduct);
58+
}
59+
continue;
60+
}
61+
productsMap.set(id, product);
62+
}
63+
64+
return [...productsMap.values()];
65+
};
66+
4367
export const newSortProducts = (products: ProductTuple[], method: SortMethod) => {
44-
const result = [...products];
45-
console.log("method", method);
68+
const filtered = filterProductsById(products);
69+
const result = [...filtered];
70+
71+
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;
4675

47-
for (let p = 0; p < result.length; p++) {
48-
// iterate over products
49-
console.log("product", result[p]);
5076
for (let i = 0; i < result.length; i++) {
5177
// sort each product by pick location
52-
// console.log("i", i);
5378
const currentProduct = result[i];
5479
const nextProduct = result[i + 1];
5580
console.log(currentProduct, nextProduct);
81+
5682
if (!nextProduct) {
5783
break;
5884
}
85+
5986
const [, , currentPickLocation] = currentProduct;
6087
const [, , nextPickLocation] = nextProduct;
6188
const [currentBay, currentShelf] = currentPickLocation.split(" ");
6289
const [nextBay, nextShelf] = nextPickLocation.split(" ");
6390

6491
if (currentBay === nextBay) {
65-
// console.log("same bay");
6692
if (Number(currentShelf) > Number(nextShelf)) {
6793
swap(result, i, i + 1);
6894
}
@@ -71,6 +97,14 @@ export const newSortProducts = (products: ProductTuple[], method: SortMethod) =>
7197
if (currentBay > nextBay) {
7298
swap(result, i, i + 1);
7399
}
100+
101+
if (currentBay < nextBay) {
102+
continue;
103+
}
104+
105+
// if currentBay is more than one letter eg AZ not A
106+
// then sort by the first letter
107+
// then sort by the second letter
74108
}
75109
}
76110

0 commit comments

Comments
 (0)