Skip to content

Commit

Permalink
add getCartSize()
Browse files Browse the repository at this point in the history
  • Loading branch information
creme332 committed Jul 17, 2024
1 parent 57002ef commit 10290a3
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions public/js_original/models/Cart.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* A function for managing the cart in localStorage.
* @returns {{getItems: (function(): CartItem[]), removeItem: (function(CartItem): void), isEmpty: (function(): boolean),
* clear: (function(): void), addItem: (function(CartItem): void)}}
* clear: (function(): void), addItem: (function(CartItem): void)}, getCartSize: (function(): number)}}
*/
function Cart() {
/**
Expand Down Expand Up @@ -82,7 +82,18 @@ function Cart() {
localStorage.setItem("cart", "[]");
}

return { addItem, isEmpty, getItems, removeItem, clear };
/**
*
* @returns {number} Total number of items in cart
*/
function getCartSize() {
return getItems().reduce(
(accumulator, item) => accumulator + item.quantity,
0,
);
}

return { addItem, isEmpty, getItems, removeItem, clear, getCartSize };
}

export default Cart;

0 comments on commit 10290a3

Please sign in to comment.