https://github.com/marcmartinezdev/storagecart
A simple, type-safe shopping cart utility class that persists its data in localStorage.
https://github.com/marcmartinezdev/storagecart
cart npm shop shopping-cart typescript
Last synced: about 1 month ago
JSON representation
A simple, type-safe shopping cart utility class that persists its data in localStorage.
- Host: GitHub
- URL: https://github.com/marcmartinezdev/storagecart
- Owner: MarcMartinezDev
- License: mit
- Created: 2025-04-08T09:53:27.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-08T10:07:49.000Z (about 1 year ago)
- Last Synced: 2025-04-10T05:39:49.881Z (about 1 year ago)
- Topics: cart, npm, shop, shopping-cart, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/storagecart
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ๐ StorageCart
A simple, type-safe shopping cart utility class that persists its data in `localStorage`. Ideal for frontend applications where you need to manage a cart without a backend.
[https://github.com/MarcMartinezDev/storagecart](https://github.com/MarcMartinezDev/storagecart).
---
## โจ Features
- ๐ง Fully typed with generics
- ๐ Persists cart data in `localStorage`
- โ Add, remove, and update items
- ๐ธ Apply discounts and taxes
- ๐งน Clear cart, check item existence, and get totals
- โก๏ธ Lightweight and dependency-free
---
## ๐ฆ Installation
```bash
npm install storagecart
```
## ๐ API Reference
### `new Cart()`
Initializes the cart and loads any saved items from `localStorage`.
---
### `add(item: T): void`
Adds a new item to the cart or increases its quantity if it already exists.
- **item**: Object of type `T` with at least `id`, `price`, and `quantity` properties.
---
### `remove(id: string | number): void`
Removes an item from the cart by its ID.
- **id**: The ID of the item to remove.
---
### `less(id: string | number): void`
Decreases the quantity of an item by 1. Removes it if the quantity reaches 0.
- **id**: The ID of the item to decrement.
---
### `more(id: string | number): void`
Increases the quantity of an item by 1.
- **id**: The ID of the item to increment.
---
### `clear(): void`
Empties the entire cart and clears the saved data from `localStorage`.
---
### `discount(rate: number): void`
Applies a discount to all items in the cart.
- **rate**: A number between `0` and `1` (e.g., `0.2` for a 20% discount).
---
### `applyTax(rate: number): void`
Applies tax to all item prices in the cart.
- **rate**: A number between `0` and `1` (e.g., `0.21` for 21% VAT).
---
### `hasItem(id: string | number): boolean`
Checks whether an item exists in the cart.
- **id**: The ID of the item.
---
### `isEmpty(): boolean`
Returns `true` if the cart has no items, otherwise `false`.
---
### `getItems(): T[]`
Returns all current items in the cart.
---
### `getProductAmount(id: string | number): number`
Returns the total amount for a specific item (`quantity ร price`).
- **id**: The ID of the item.
---
### `getProductQuantity(id: string | number): number`
Returns the quantity of a specific item in the cart.
- **id**: The ID of the item.
---
### `getTotalAmount(): number`
Returns the total cost of all items in the cart.
---
### `getTotalItemCount(): number`
Returns the total number of items (sum of all quantities) in the cart.
## โ
Requirements
Modern browser environment (uses localStorage)
TypeScript support
## Contributing
- Fork the repository.
- Create a new branch (git checkout -b feature-xyz).
- Make your changes.
- Commit your changes (git commit -am 'Add feature XYZ').
- Push to the branch (git push origin feature-xyz).
- Create a new Pull Request.
You can find the repository on GitHub at [https://github.com/MarcMartinezDev/storagecart](https://github.com/MarcMartinezDev/storagecart).
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.