
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
cart-manager
Advanced tools
A lightweight ES module for managing shopping cart operations using localStorage. This library provides a simple and intuitive API for adding, removing, updating, and managing cart items in browser-based applications.
Include the module in your HTML file:
import {
addToCart,
removeFromCart,
getCart,
updateQuantity,
getPrice,
clearCart
} from "./cart-utils.js";
addToCart(data, id)Adds a new item to the cart. If an item with the same ID already exists, the operation is skipped.
Parameters:
data (Object): The item object containing product details
id (Number|String): Unique identifier for the itemname (String): Product nameprice (Number): Product pricequantity (Number): Quantity to addid (Number|String): The ID of the item (must match data.id)Example:
addToCart(
{
id: 1,
name: "Ergonomic Chair",
price: 1200,
quantity: 1
},
1
);
Important Notes:
id propertyremoveFromCart(id)Removes an item from the cart by its unique identifier.
Parameters:
id (Number|String): The ID of the item to removeExample:
removeFromCart(1);
Important Notes:
getCart()Retrieves the current cart contents.
Returns:
Array: Cart items, or null if the cart is emptyExample:
const cart = getCart();
if (cart) {
console.log("Current cart:", cart);
} else {
console.log("Cart is empty");
}
Important Notes:
null (not an empty array) when cart is emptyupdateQuantity(id, quantity)Updates the quantity of a specific item in the cart.
Parameters:
id (Number|String): The ID of the item to updatequantity (Number): The new quantity valueExample:
updateQuantity(1, 3);
Important Notes:
getPrice(tax = 0, deliveryCharges = 0)Calculates the total cart price including optional tax and delivery charges.
Parameters:
tax (Number, optional): Tax amount to add (default: 0)deliveryCharges (Number, optional): Delivery fee to add (default: 0)Returns:
Number: Total cart valueExample:
const subtotal = getPrice();
const total = getPrice(50, 30);
console.log("Subtotal:", subtotal);
console.log("Total with tax and delivery:", total);
Important Notes:
quantity property default to 1clearCart()Removes all items from the cart and clears localStorage.
Example:
clearCart();
Important Notes:
Cart data is stored in localStorage with the following structure:
[
{
"id": 1,
"name": "Ergonomic Chair",
"price": 1200,
"quantity": 2
},
{
"id": 2,
"name": "Standing Desk",
"price": 3500,
"quantity": 1
}
]
Always ensure each item has a unique identifier:
// Good
addToCart({ id: Date.now(), name: "Product", price: 100, quantity: 1 }, Date.now());
// Avoid duplicate IDs
Use updateQuantity() instead of adding the same item multiple times:
// Recommended
updateQuantity(1, 3);
// Not recommended
addToCart(item, 1);
addToCart(item, 1);
addToCart(item, 1);
Ensure numeric values are properly typed:
const price = Number(item.price);
const quantity = parseInt(item.quantity, 10);
Always validate cart data before operations:
const cart = getCart();
if (cart && Array.isArray(cart)) {
// Safe to process cart
cart.forEach(item => {
console.log(item.name, item.price);
});
}
| Issue | Solution |
|---|---|
Cart returns null | Check for null before accessing cart data |
| Quantities not updating | Ensure ID exists and quantity is valid |
| Price calculation errors | Verify all prices are numbers, not strings |
| LocalStorage quota exceeded | Clear cart periodically or limit items |
| Duplicate items | Use updateQuantity() instead of addToCart() |
This library uses localStorage and ES6 modules, which are supported in:
MIT License - feel free to use in personal and commercial projects.
Contributions are welcome! Please ensure all changes maintain backward compatibility and include appropriate documentation.
Need help? Open an issue or submit a pull request on GitHub.
FAQs
a simple package to maintain e-commerce cart through localstorage
The npm package cart-manager receives a total of 0 weekly downloads. As such, cart-manager popularity was classified as not popular.
We found that cart-manager demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.