From 10290a36f23e77674e27783d169111c1961d5eb0 Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Wed, 17 Jul 2024 16:24:11 +0400 Subject: [PATCH] add getCartSize() --- public/js_original/models/Cart.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/public/js_original/models/Cart.js b/public/js_original/models/Cart.js index 9a504dc1..14491230 100644 --- a/public/js_original/models/Cart.js +++ b/public/js_original/models/Cart.js @@ -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() { /** @@ -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;