@ravishranjan/cart 2.0.8 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,14 +1,199 @@
1
1
  # @ravishranjan/cart
2
2
 
3
- A **native frontend cart system** with optional React hooks.
4
- - Works in **any JavaScript app** (vanilla JS, Vue, Svelte, etc.)
5
- - Provides **React hooks** for easy integration in React apps
6
- - Stores cart data in **localStorage** or **sessionStorage**
7
- - **No backend required**
3
+ A **lightweight, native frontend cart system** with optional **React hooks**.
4
+
5
+ - 🧠 Works in **any JavaScript framework** Vanilla JS, React, Vue, Svelte, etc.
6
+ - ⚛️ Includes **React hooks** and `<CartProvider>` for easy React integration.
7
+ - 💾 Uses **localStorage** or **sessionStorage** for persistence.
8
+ - 🔌 Works both via **npm install** and **CDN** — no backend required.
8
9
 
9
10
  ---
10
11
 
11
12
  ## 📦 Installation
12
13
 
14
+ ### Option 1 — NPM
15
+
13
16
  ```bash
14
17
  npm install @ravishranjan/cart
18
+ ```
19
+
20
+ ### Option 2 — CDN (no install)
21
+
22
+ Use directly in your webpage:
23
+
24
+ ```html
25
+ <script src="https://cdn.jsdelivr.net/npm/@ravishranjan/cart/dist/cart.min.js"></script>
26
+ ```
27
+ ---
28
+ ## Cart item type
29
+ ```ts
30
+ interface CartItem {
31
+ id: string | number;
32
+ name: string;
33
+ price?: number;
34
+ quantity?: number;
35
+ category?: string;
36
+ [key: string]: any;
37
+ }
38
+ ```
39
+
40
+ ---
41
+ ## 🧩 Usage Examples
42
+
43
+ ### **1️⃣ Basic JavaScript (no framework)**
44
+
45
+ ```html
46
+ <script src="https://cdn.jsdelivr.net/npm/@ravishranjan/cart/dist/cart.min.js"></script>
47
+ <script>
48
+ const cart = new Cart({ storage: "localStorage" });
49
+
50
+ cart.addItem({ id: 1, name: "Apple", price: 30, quantity: 2 });
51
+ cart.addItem({ id: 2, name: "Orange", price: 25 });
52
+
53
+ cart.removeItem(2);
54
+
55
+ console.log("Total price:", cart.getTotal());
56
+ console.log(cart.getItems());
57
+
58
+ cart.clear();
59
+ </script>
60
+ ```
61
+
62
+ ---
63
+
64
+ ### **2️⃣ Node or Framework Projects (NPM install)**
65
+
66
+ ```js
67
+ import Cart from "@ravishranjan/cart";
68
+
69
+ const cart = new Cart({ storage: "localStorage" });
70
+
71
+ cart.addItem({ id: 101, name: "Book", price: 199 });
72
+ cart.addItem({ id: 102, name: "Pen", price: 49, quantity: 2 });
73
+
74
+ console.log(cart.getItems());
75
+ console.log("Total Price:", cart.getTotal());
76
+ ```
77
+
78
+ **Available methods:**
79
+ | Method | Description | Example |
80
+ |---------|--------------|----------|
81
+ | `addItem(item)` | Adds an item object | `cart.addItem({ id: 1, name: 'Apple', quantity: 2 })` |
82
+ | `removeItem(id)` | Removes an item by id | `cart.removeItem(1)` |
83
+ | `getItems()` | Returns all items | `cart.getItems()` |
84
+ | `getItems(category)` | Returns items by given catefory | `cart.getItems("<category>")` |
85
+ | `getTotal()` | Sum of all item prices × qty | `cart.getTotal()` |
86
+ | `clear()` | Clears the entire cart | `cart.clear()` |
87
+
88
+ ---
89
+
90
+ ### **3️⃣ React Usage (with hooks + context)**
91
+
92
+ #### Setup the Provider
93
+
94
+ ```tsx
95
+ import React from "react";
96
+ import { CartProvider } from "@ravishranjan/cart/react";
97
+
98
+ import App from "./App";
99
+
100
+ export default function Root() {
101
+ return (
102
+ <CartProvider storage="localStorage">
103
+ <App />
104
+ </CartProvider>
105
+ );
106
+ }
107
+ ```
108
+
109
+ #### Use the Hook
110
+
111
+ ```tsx
112
+ import { useCart } from "@ravishranjan/cart/react";
113
+
114
+ export default function Shop() {
115
+ const { items, addItem, removeItem, getTotal, clear } = useCart();
116
+
117
+ return (
118
+ <div>
119
+ <button
120
+ onClick={() => addItem({ id: 1, name: "Apple", price: 30 })}
121
+ >
122
+ Add Apple
123
+ </button>
124
+ <button onClick={() => clear()}>Clear Cart</button>
125
+
126
+ <ul>
127
+ {items.map((item) => (
128
+ <li key={item.id}>
129
+ {item.name} - ₹{item.price}
130
+ <button onClick={() => removeItem(item.id)}>❌</button>
131
+ </li>
132
+ ))}
133
+ </ul>
134
+
135
+ <h3>Total: ₹{getTotal()}</h3>
136
+ </div>
137
+ );
138
+ }
139
+ ```
140
+
141
+ ---
142
+
143
+ ## ⚙️ Storage Options
144
+
145
+ | Option | Description |
146
+ | ----------- | -------------------------------------------------------- |
147
+ | `"localStorage"` | Persists cart across browser sessions (via localStorage) |
148
+ | `"sessionStorage"` | Clears cart when the tab is closed (via sessionStorage) |
149
+
150
+ Usage:
151
+
152
+ ```js
153
+ const cart = new Cart({ storage: "sessionStorage" });
154
+ ```
155
+
156
+ or in React:
157
+
158
+ ```tsx
159
+ <CartProvider storage="sessionStorage">
160
+ <App />
161
+ </CartProvider>
162
+ ```
163
+
164
+ ---
165
+
166
+ ## 🌍 CDN Quick Demo
167
+
168
+ ```html
169
+ <!DOCTYPE html>
170
+ <html>
171
+ <head>
172
+ <title>Cart Demo</title>
173
+ </head>
174
+ <body>
175
+ <script src="https://cdn.jsdelivr.net/npm/@ravishranjan/cart/dist/cart.min.js"></script>
176
+ <script>
177
+ const cart = new Cart();
178
+ cart.addItem({ id: 1, name: "Coffee", price: 120 });
179
+ console.log(cart.getTotal()); // 120
180
+ </script>
181
+ </body>
182
+ </html>
183
+ ```
184
+
185
+ ---
186
+
187
+ ## 🧱 API Summary
188
+
189
+ | API | Type | Description |
190
+ | -------------- | --------------- | ------------------------------- |
191
+ | `Cart` | Class | Core cart logic |
192
+ | `CartProvider` | React Component | Context provider for cart state |
193
+ | `useCart()` | React Hook | Access cart in React components |
194
+
195
+ ---
196
+
197
+ ## 🧾 License
198
+
199
+ ISC © [Ravish Ranjan](https://npmjs.com/~ravishranjan)
@@ -0,0 +1 @@
1
+ "use strict";var Cart=(()=>{var s=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var u=Object.prototype.hasOwnProperty;var l=(i,t)=>{for(var e in t)s(i,e,{get:t[e],enumerable:!0})},y=(i,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of c(t))!u.call(i,a)&&a!==e&&s(i,a,{get:()=>t[a],enumerable:!(r=g(t,a))||r.enumerable});return i};var d=i=>y(s({},"__esModule",{value:!0}),i);var h={};l(h,{Cart:()=>o});function n(i){if(typeof window>"u")throw new Error("No window object found");return i=="localStorage"?window.localStorage:window.sessionStorage}var o=class{constructor({storage:t="localStorage",key:e="cart"}={}){this.storage=n(t),this.key=e}getCart(){let t=this.storage.getItem(this.key);return t?JSON.parse(t):[]}save(t){this.storage.setItem(this.key,JSON.stringify(t))}addItem(t){let e=this.getCart(),r=e.find(a=>a.id===t.id);r?r.quantity=(r.quantity||1)+(t.quantity||1):e.push({...t,quantity:t.quantity||1}),this.save(e)}removeItem(t){this.save(this.getCart().filter(e=>e.id!==t))}clear(){this.save([])}getItems(t){let e=this.getCart();return t?e.filter(r=>r.category===t):e}getTotal(t){let e=this.getCart();return t&&(e=e.filter(r=>r.category===t)),e.reduce((r,a)=>r+(a.price||0)*(a.quantity||1),0)}};return d(h);})();
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  CartContext
3
- } from "./chunk-3AB6KTDB.mjs";
3
+ } from "./chunk-XPRNR6EK.mjs";
4
4
 
5
5
  // src/react/useCart.tsx
6
6
  import { useContext } from "react";
@@ -18,12 +18,12 @@ var Cart = class {
18
18
  save(cart) {
19
19
  this.storage.setItem(this.key, JSON.stringify(cart));
20
20
  }
21
- addItem(item) {
21
+ addItem(newItem) {
22
22
  const cart = this.getCart();
23
- const exists = cart.find((item2) => item2.id === item2.id);
23
+ const exists = cart.find((item) => item.id === newItem.id);
24
24
  if (exists)
25
- exists.quantity = (exists.quantity || 1) + (item.quantity || 1);
26
- else cart.push({ ...item, quantity: item.quantity || 1 });
25
+ exists.quantity = (exists.quantity || 1) + (newItem.quantity || 1);
26
+ else cart.push({ ...newItem, quantity: newItem.quantity || 1 });
27
27
  this.save(cart);
28
28
  }
29
29
  removeItem(id) {
@@ -1,13 +1,17 @@
1
1
  import {
2
2
  Cart
3
- } from "./chunk-YTW5MCYO.mjs";
3
+ } from "./chunk-TVKKRH3O.mjs";
4
4
 
5
5
  // src/react/CartContext.tsx
6
6
  import { createContext, useEffect, useState } from "react";
7
7
  import { jsx } from "react/jsx-runtime";
8
8
  var CartContext = createContext(null);
9
- var CartProvider = ({ children }) => {
10
- const cartInstance = new Cart();
9
+ var CartProvider = ({
10
+ children,
11
+ storage = "localStorage",
12
+ key = "cart"
13
+ }) => {
14
+ const cartInstance = new Cart({ key, storage });
11
15
  const [items, setItems] = useState(cartInstance.getItems());
12
16
  const sync = () => setItems(cartInstance.getItems());
13
17
  const api = {
@@ -15,7 +15,7 @@ declare class Cart {
15
15
  });
16
16
  private getCart;
17
17
  private save;
18
- addItem(item: CartItem): void;
18
+ addItem(newItem: CartItem): void;
19
19
  removeItem(id: string | number): void;
20
20
  clear(): void;
21
21
  getItems(category?: string): CartItem[];
@@ -15,7 +15,7 @@ declare class Cart {
15
15
  });
16
16
  private getCart;
17
17
  private save;
18
- addItem(item: CartItem): void;
18
+ addItem(newItem: CartItem): void;
19
19
  removeItem(id: string | number): void;
20
20
  clear(): void;
21
21
  getItems(category?: string): CartItem[];
package/dist/core/cart.js CHANGED
@@ -46,12 +46,12 @@ var Cart = class {
46
46
  save(cart) {
47
47
  this.storage.setItem(this.key, JSON.stringify(cart));
48
48
  }
49
- addItem(item) {
49
+ addItem(newItem) {
50
50
  const cart = this.getCart();
51
- const exists = cart.find((item2) => item2.id === item2.id);
51
+ const exists = cart.find((item) => item.id === newItem.id);
52
52
  if (exists)
53
- exists.quantity = (exists.quantity || 1) + (item.quantity || 1);
54
- else cart.push({ ...item, quantity: item.quantity || 1 });
53
+ exists.quantity = (exists.quantity || 1) + (newItem.quantity || 1);
54
+ else cart.push({ ...newItem, quantity: newItem.quantity || 1 });
55
55
  this.save(cart);
56
56
  }
57
57
  removeItem(id) {
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  Cart
3
- } from "../chunk-YTW5MCYO.mjs";
3
+ } from "../chunk-TVKKRH3O.mjs";
4
4
  import "../chunk-7RBGBF5M.mjs";
5
5
  export {
6
6
  Cart
package/dist/index.js CHANGED
@@ -47,12 +47,12 @@ var Cart = class {
47
47
  save(cart) {
48
48
  this.storage.setItem(this.key, JSON.stringify(cart));
49
49
  }
50
- addItem(item) {
50
+ addItem(newItem) {
51
51
  const cart = this.getCart();
52
- const exists = cart.find((item2) => item2.id === item2.id);
52
+ const exists = cart.find((item) => item.id === newItem.id);
53
53
  if (exists)
54
- exists.quantity = (exists.quantity || 1) + (item.quantity || 1);
55
- else cart.push({ ...item, quantity: item.quantity || 1 });
54
+ exists.quantity = (exists.quantity || 1) + (newItem.quantity || 1);
55
+ else cart.push({ ...newItem, quantity: newItem.quantity || 1 });
56
56
  this.save(cart);
57
57
  }
58
58
  removeItem(id) {
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  Cart
3
- } from "./chunk-YTW5MCYO.mjs";
3
+ } from "./chunk-TVKKRH3O.mjs";
4
4
  import {
5
5
  getStorage
6
6
  } from "./chunk-7RBGBF5M.mjs";
@@ -10,8 +10,10 @@ type CartContextType = {
10
10
  total: number;
11
11
  };
12
12
  declare const CartContext: react.Context<CartContextType | null>;
13
- declare const CartProvider: ({ children }: {
13
+ declare const CartProvider: ({ children, storage, key, }: {
14
14
  children: React.ReactNode;
15
+ storage?: "localStorage" | "sessionStorage";
16
+ key?: string;
15
17
  }) => react_jsx_runtime.JSX.Element;
16
18
 
17
19
  export { CartContext, type CartContextType, CartProvider };
@@ -10,8 +10,10 @@ type CartContextType = {
10
10
  total: number;
11
11
  };
12
12
  declare const CartContext: react.Context<CartContextType | null>;
13
- declare const CartProvider: ({ children }: {
13
+ declare const CartProvider: ({ children, storage, key, }: {
14
14
  children: React.ReactNode;
15
+ storage?: "localStorage" | "sessionStorage";
16
+ key?: string;
15
17
  }) => react_jsx_runtime.JSX.Element;
16
18
 
17
19
  export { CartContext, type CartContextType, CartProvider };
@@ -48,12 +48,12 @@ var Cart = class {
48
48
  save(cart) {
49
49
  this.storage.setItem(this.key, JSON.stringify(cart));
50
50
  }
51
- addItem(item) {
51
+ addItem(newItem) {
52
52
  const cart = this.getCart();
53
- const exists = cart.find((item2) => item2.id === item2.id);
53
+ const exists = cart.find((item) => item.id === newItem.id);
54
54
  if (exists)
55
- exists.quantity = (exists.quantity || 1) + (item.quantity || 1);
56
- else cart.push({ ...item, quantity: item.quantity || 1 });
55
+ exists.quantity = (exists.quantity || 1) + (newItem.quantity || 1);
56
+ else cart.push({ ...newItem, quantity: newItem.quantity || 1 });
57
57
  this.save(cart);
58
58
  }
59
59
  removeItem(id) {
@@ -81,8 +81,12 @@ var Cart = class {
81
81
  // src/react/CartContext.tsx
82
82
  var import_jsx_runtime = require("react/jsx-runtime");
83
83
  var CartContext = (0, import_react.createContext)(null);
84
- var CartProvider = ({ children }) => {
85
- const cartInstance = new Cart();
84
+ var CartProvider = ({
85
+ children,
86
+ storage = "localStorage",
87
+ key = "cart"
88
+ }) => {
89
+ const cartInstance = new Cart({ key, storage });
86
90
  const [items, setItems] = (0, import_react.useState)(cartInstance.getItems());
87
91
  const sync = () => setItems(cartInstance.getItems());
88
92
  const api = {
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  CartContext,
3
3
  CartProvider
4
- } from "../chunk-3AB6KTDB.mjs";
5
- import "../chunk-YTW5MCYO.mjs";
4
+ } from "../chunk-XPRNR6EK.mjs";
5
+ import "../chunk-TVKKRH3O.mjs";
6
6
  import "../chunk-7RBGBF5M.mjs";
7
7
  export {
8
8
  CartContext,
@@ -53,12 +53,12 @@ var Cart = class {
53
53
  save(cart) {
54
54
  this.storage.setItem(this.key, JSON.stringify(cart));
55
55
  }
56
- addItem(item) {
56
+ addItem(newItem) {
57
57
  const cart = this.getCart();
58
- const exists = cart.find((item2) => item2.id === item2.id);
58
+ const exists = cart.find((item) => item.id === newItem.id);
59
59
  if (exists)
60
- exists.quantity = (exists.quantity || 1) + (item.quantity || 1);
61
- else cart.push({ ...item, quantity: item.quantity || 1 });
60
+ exists.quantity = (exists.quantity || 1) + (newItem.quantity || 1);
61
+ else cart.push({ ...newItem, quantity: newItem.quantity || 1 });
62
62
  this.save(cart);
63
63
  }
64
64
  removeItem(id) {
@@ -86,8 +86,12 @@ var Cart = class {
86
86
  // src/react/CartContext.tsx
87
87
  var import_jsx_runtime = require("react/jsx-runtime");
88
88
  var CartContext = (0, import_react.createContext)(null);
89
- var CartProvider = ({ children }) => {
90
- const cartInstance = new Cart();
89
+ var CartProvider = ({
90
+ children,
91
+ storage = "localStorage",
92
+ key = "cart"
93
+ }) => {
94
+ const cartInstance = new Cart({ key, storage });
91
95
  const [items, setItems] = (0, import_react.useState)(cartInstance.getItems());
92
96
  const sync = () => setItems(cartInstance.getItems());
93
97
  const api = {
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  useCart
3
- } from "../chunk-K3ENNZKF.mjs";
3
+ } from "../chunk-SR5T5KWM.mjs";
4
4
  import {
5
5
  CartProvider
6
- } from "../chunk-3AB6KTDB.mjs";
7
- import "../chunk-YTW5MCYO.mjs";
6
+ } from "../chunk-XPRNR6EK.mjs";
7
+ import "../chunk-TVKKRH3O.mjs";
8
8
  import "../chunk-7RBGBF5M.mjs";
9
9
  export {
10
10
  CartProvider,
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  useCart
3
- } from "../chunk-K3ENNZKF.mjs";
4
- import "../chunk-3AB6KTDB.mjs";
5
- import "../chunk-YTW5MCYO.mjs";
3
+ } from "../chunk-SR5T5KWM.mjs";
4
+ import "../chunk-XPRNR6EK.mjs";
5
+ import "../chunk-TVKKRH3O.mjs";
6
6
  import "../chunk-7RBGBF5M.mjs";
7
7
  export {
8
8
  useCart
package/package.json CHANGED
@@ -1,56 +1,62 @@
1
- {
2
- "name": "@ravishranjan/cart",
3
- "version": "2.0.8",
4
- "description": "A native cart system for web apps with optional React hooks",
5
- "main": "dist/index.js",
6
- "module": "dist/index.mjs",
7
- "types": "dist/types.d.ts",
8
- "files": [
9
- "dist"
10
- ],
11
- "scripts": {
12
- "build": "tsup src --format esm,cjs --dts",
13
- "dev": "tsup src --watch"
14
- },
15
- "keywords": [
16
- "cart",
17
- "react",
18
- "localstorage",
19
- "frontend",
20
- "native"
21
- ],
22
- "exports": {
23
- ".": {
24
- "types": "./dist/index.d.ts",
25
- "import": "./dist/index.mjs",
26
- "require": "./dist/index.js"
27
- },
28
- "./react": {
29
- "types": "./dist/react/index.d.ts",
30
- "import": "./dist/react/index.mjs",
31
- "require": "./dist/react/index.js"
32
- }
33
- },
34
- "author": "Ravish Ranjan",
35
- "license": "ISC",
36
- "devDependencies": {
37
- "@types/react": "^19.2.2",
38
- "tsup": "^8.5.0",
39
- "typescript": "^5.9.3"
40
- },
41
- "peerDependencies": {
42
- "react": "^18.0.0 || ^19.0.0",
43
- "react-dom": "^18.0.0 || ^19.0.0"
44
- },
45
- "peerDependenciesMeta": {
46
- "react": {
47
- "optional": true
48
- },
49
- "react-dom": {
50
- "optional": true
51
- },
52
- "typescript": {
53
- "optional": true
54
- }
55
- }
56
- }
1
+ {
2
+ "name": "@ravishranjan/cart",
3
+ "version": "2.1.0",
4
+ "description": "A native cart system for web apps with optional React hooks",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/types.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "build": "tsup src --format esm,cjs --dts",
13
+ "dev": "tsup src --watch",
14
+ "build:cdn": "esbuild src/core/cart.ts --bundle --minify --format=iife --global-name=Cart --outfile=dist/cart.min.js",
15
+ "prepare": "npm run build && npm run build:cdn"
16
+ },
17
+ "keywords": [
18
+ "cart",
19
+ "react",
20
+ "localstorage",
21
+ "frontend",
22
+ "native",
23
+ "cdn"
24
+ ],
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/index.d.ts",
28
+ "import": "./dist/index.mjs",
29
+ "require": "./dist/index.js"
30
+ },
31
+ "./react": {
32
+ "types": "./dist/react/index.d.ts",
33
+ "import": "./dist/react/index.mjs",
34
+ "require": "./dist/react/index.js"
35
+ }
36
+ },
37
+ "unpkg": "dist/cart.min.js",
38
+ "jsdelivr": "dist/cart.min.js",
39
+ "author": "Ravish Ranjan",
40
+ "license": "ISC",
41
+ "devDependencies": {
42
+ "@types/react": "^19.2.2",
43
+ "esbuild": "^0.25.10",
44
+ "tsup": "^8.5.0",
45
+ "typescript": "^5.9.3"
46
+ },
47
+ "peerDependencies": {
48
+ "react": ">=18.0.0",
49
+ "react-dom": "^18.0.0"
50
+ },
51
+ "peerDependenciesMeta": {
52
+ "react": {
53
+ "optional": true
54
+ },
55
+ "react-dom": {
56
+ "optional": true
57
+ },
58
+ "typescript": {
59
+ "optional": true
60
+ }
61
+ }
62
+ }