An open API service indexing awesome lists of open source software.

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.

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.