@ravishranjan/cart 2.1.4 → 2.1.6
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 +203 -199
- package/dist/cart.min.js +1 -1
- package/dist/{chunk-BCVOXMCT.mjs → chunk-2EP5A44J.mjs} +1 -1
- package/dist/chunk-IDMIQXPW.mjs +24 -0
- package/dist/{chunk-XPRNR6EK.mjs → chunk-NQCCPA7E.mjs} +4 -2
- package/dist/chunk-R4MVVWRS.mjs +64 -0
- package/dist/core/cart.global.js +1 -25
- package/dist/core/cart.global.mjs +13 -7
- package/dist/core/cart.mjs +5 -3
- package/dist/core/storage.mjs +4 -2
- package/dist/index.mjs +10 -4
- package/dist/react/CartContext.mjs +3 -3
- package/dist/react/index.mjs +4 -4
- package/dist/react/useCart.mjs +4 -4
- package/package.json +62 -62
- package/dist/chunk-7RBGBF5M.mjs +0 -9
- package/dist/chunk-RBWMJEAD.mjs +0 -47
- package/dist/chunk-SR5T5KWM.mjs +0 -15
- package/dist/chunk-TVKKRH3O.mjs +0 -53
- package/dist/chunk-ZA6BU4XH.mjs +0 -55
package/README.md
CHANGED
|
@@ -1,199 +1,203 @@
|
|
|
1
|
-
# @ravishranjan/cart
|
|
2
|
-
|
|
3
|
-
A **lightweight, native frontend cart system** with optional **React hooks**.
|
|
4
|
-
|
|
5
|
-
-
|
|
6
|
-
-
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
|
|
10
|
-
---
|
|
11
|
-
|
|
12
|
-
##
|
|
13
|
-
|
|
14
|
-
### Option 1 — NPM
|
|
15
|
-
|
|
16
|
-
```bash
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
|
84
|
-
|
|
85
|
-
| `
|
|
86
|
-
| `
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
<
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
1
|
+
# @ravishranjan/cart
|
|
2
|
+
|
|
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.
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
### Option 1 — NPM
|
|
15
|
+
|
|
16
|
+
```bash
|
|
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
|
+
---
|
|
29
|
+
|
|
30
|
+
## Cart item type
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
interface CartItem {
|
|
34
|
+
id: string | number;
|
|
35
|
+
name: string;
|
|
36
|
+
price?: number;
|
|
37
|
+
quantity?: number;
|
|
38
|
+
category?: string;
|
|
39
|
+
[key: string]: any;
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## Usage Examples
|
|
46
|
+
|
|
47
|
+
### **Basic JavaScript (no framework)**
|
|
48
|
+
|
|
49
|
+
```html
|
|
50
|
+
<script src="https://cdn.jsdelivr.net/npm/@ravishranjan/cart/dist/cart.min.js"></script>
|
|
51
|
+
<script>
|
|
52
|
+
const cart = new Cart({ storage: "localStorage" });
|
|
53
|
+
|
|
54
|
+
cart.addItem({ id: 1, name: "Apple", price: 30, quantity: 2 });
|
|
55
|
+
cart.addItem({ id: 2, name: "Orange", price: 25 });
|
|
56
|
+
|
|
57
|
+
cart.removeItem(2);
|
|
58
|
+
|
|
59
|
+
console.log("Total price:", cart.getTotal());
|
|
60
|
+
console.log(cart.getItems());
|
|
61
|
+
|
|
62
|
+
cart.clear();
|
|
63
|
+
</script>
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
### **Node or Framework Projects (NPM install)**
|
|
69
|
+
|
|
70
|
+
```js
|
|
71
|
+
import Cart from "@ravishranjan/cart";
|
|
72
|
+
|
|
73
|
+
const cart = new Cart({ storage: "localStorage" });
|
|
74
|
+
|
|
75
|
+
cart.addItem({ id: 101, name: "Book", price: 199 });
|
|
76
|
+
cart.addItem({ id: 102, name: "Pen", price: 49, quantity: 2 });
|
|
77
|
+
|
|
78
|
+
console.log(cart.getItems());
|
|
79
|
+
console.log("Total Price:", cart.getTotal());
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
**Available methods:**
|
|
83
|
+
| Method | Description | Example |
|
|
84
|
+
|---------|--------------|----------|
|
|
85
|
+
| `addItem(item)` | Adds an item object | `cart.addItem({ id: 1, name: 'Apple', quantity: 2 })` |
|
|
86
|
+
| `removeItem(id)` | Removes an item by id | `cart.removeItem(1)` |
|
|
87
|
+
| `getItems()` | Returns all items | `cart.getItems()` |
|
|
88
|
+
| `getItems(category)` | Returns items by given catefory | `cart.getItems("<category>")` |
|
|
89
|
+
| `getTotal()` | Sum of all item prices × qty | `cart.getTotal()` |
|
|
90
|
+
| `clear()` | Clears the entire cart | `cart.clear()` |
|
|
91
|
+
|
|
92
|
+
---
|
|
93
|
+
|
|
94
|
+
### **React Usage (with hooks + context)**
|
|
95
|
+
|
|
96
|
+
#### Setup the Provider
|
|
97
|
+
|
|
98
|
+
```tsx
|
|
99
|
+
import React from "react";
|
|
100
|
+
import { CartProvider } from "@ravishranjan/cart/react";
|
|
101
|
+
|
|
102
|
+
import App from "./App";
|
|
103
|
+
|
|
104
|
+
export default function Root() {
|
|
105
|
+
return (
|
|
106
|
+
<CartProvider storage="localStorage">
|
|
107
|
+
<App />
|
|
108
|
+
</CartProvider>
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
#### Use the Hook
|
|
114
|
+
|
|
115
|
+
```tsx
|
|
116
|
+
import { useCart } from "@ravishranjan/cart/react";
|
|
117
|
+
|
|
118
|
+
export default function Shop() {
|
|
119
|
+
const { items, addItem, removeItem, getTotal, clear } = useCart();
|
|
120
|
+
|
|
121
|
+
return (
|
|
122
|
+
<div>
|
|
123
|
+
<button
|
|
124
|
+
onClick={() => addItem({ id: 1, name: "Apple", price: 30 })}
|
|
125
|
+
>
|
|
126
|
+
Add Apple
|
|
127
|
+
</button>
|
|
128
|
+
<button onClick={() => clear()}>Clear Cart</button>
|
|
129
|
+
|
|
130
|
+
<ul>
|
|
131
|
+
{items.map((item) => (
|
|
132
|
+
<li key={item.id}>
|
|
133
|
+
{item.name} - ₹{item.price}
|
|
134
|
+
<button onClick={() => removeItem(item.id)}>X</button>
|
|
135
|
+
</li>
|
|
136
|
+
))}
|
|
137
|
+
</ul>
|
|
138
|
+
|
|
139
|
+
<h3>Total: ₹{getTotal()}</h3>
|
|
140
|
+
</div>
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
147
|
+
## Storage Options
|
|
148
|
+
|
|
149
|
+
| Option | Description |
|
|
150
|
+
| ------------------ | -------------------------------------------------------- |
|
|
151
|
+
| `"localStorage"` | Persists cart across browser sessions (via localStorage) |
|
|
152
|
+
| `"sessionStorage"` | Clears cart when the tab is closed (via sessionStorage) |
|
|
153
|
+
|
|
154
|
+
Usage:
|
|
155
|
+
|
|
156
|
+
```js
|
|
157
|
+
const cart = new Cart({ storage: "sessionStorage" });
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
or in React:
|
|
161
|
+
|
|
162
|
+
```tsx
|
|
163
|
+
<CartProvider storage="sessionStorage">
|
|
164
|
+
<App />
|
|
165
|
+
</CartProvider>
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
---
|
|
169
|
+
|
|
170
|
+
## CDN Quick Demo
|
|
171
|
+
|
|
172
|
+
```html
|
|
173
|
+
<!DOCTYPE html>
|
|
174
|
+
<html>
|
|
175
|
+
<head>
|
|
176
|
+
<title>Cart Demo</title>
|
|
177
|
+
</head>
|
|
178
|
+
<body>
|
|
179
|
+
<script src="https://cdn.jsdelivr.net/npm/@ravishranjan/cart/dist/cart.min.js"></script>
|
|
180
|
+
<script>
|
|
181
|
+
const cart = new Cart();
|
|
182
|
+
cart.addItem({ id: 1, name: "Coffee", price: 120 });
|
|
183
|
+
console.log(cart.getTotal()); // 120
|
|
184
|
+
</script>
|
|
185
|
+
</body>
|
|
186
|
+
</html>
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
## API Summary
|
|
192
|
+
|
|
193
|
+
| API | Type | Description |
|
|
194
|
+
| -------------- | --------------- | ------------------------------- |
|
|
195
|
+
| `Cart` | Class | Core cart logic |
|
|
196
|
+
| `CartProvider` | React Component | Context provider for cart state |
|
|
197
|
+
| `useCart()` | React Hook | Access cart in React components |
|
|
198
|
+
|
|
199
|
+
---
|
|
200
|
+
|
|
201
|
+
## License
|
|
202
|
+
|
|
203
|
+
ISC © [Ravish Ranjan](https://npmjs.com/~ravishranjan)
|
package/dist/cart.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var Cart=(()=>{var
|
|
1
|
+
"use strict";var Cart=(()=>{var o=(i,t)=>()=>(i&&(t=i(i=0)),t);var y=(i,t)=>()=>(t||i((t={exports:{}}).exports,t),t.exports);function n(i){if(typeof window>"u")throw new Error("No window object found");return i=="localStorage"?window.localStorage:window.sessionStorage}var g=o(()=>{"use strict"});var s,c,u=o(()=>{"use strict";g();s=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)}},c=s});var d=y((v,l)=>{u();l.exports=c});return d();})();
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
2
|
+
var __esm = (fn, res) => function __init() {
|
|
3
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
4
|
+
};
|
|
5
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
6
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
// src/core/storage.ts
|
|
10
|
+
function getStorage(type) {
|
|
11
|
+
if (typeof window == "undefined") throw new Error("No window object found");
|
|
12
|
+
return type == "localStorage" ? window.localStorage : window.sessionStorage;
|
|
13
|
+
}
|
|
14
|
+
var init_storage = __esm({
|
|
15
|
+
"src/core/storage.ts"() {
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
export {
|
|
20
|
+
__esm,
|
|
21
|
+
__commonJS,
|
|
22
|
+
getStorage,
|
|
23
|
+
init_storage
|
|
24
|
+
};
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
|
-
Cart
|
|
3
|
-
|
|
2
|
+
Cart,
|
|
3
|
+
init_cart
|
|
4
|
+
} from "./chunk-R4MVVWRS.mjs";
|
|
4
5
|
|
|
5
6
|
// src/react/CartContext.tsx
|
|
7
|
+
init_cart();
|
|
6
8
|
import { createContext, useEffect, useState } from "react";
|
|
7
9
|
import { jsx } from "react/jsx-runtime";
|
|
8
10
|
var CartContext = createContext(null);
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__esm,
|
|
3
|
+
getStorage,
|
|
4
|
+
init_storage
|
|
5
|
+
} from "./chunk-IDMIQXPW.mjs";
|
|
6
|
+
|
|
7
|
+
// src/core/cart.ts
|
|
8
|
+
var Cart, cart_default;
|
|
9
|
+
var init_cart = __esm({
|
|
10
|
+
"src/core/cart.ts"() {
|
|
11
|
+
init_storage();
|
|
12
|
+
Cart = class {
|
|
13
|
+
constructor({
|
|
14
|
+
storage = "localStorage",
|
|
15
|
+
key = "cart"
|
|
16
|
+
} = {}) {
|
|
17
|
+
this.storage = getStorage(storage);
|
|
18
|
+
this.key = key;
|
|
19
|
+
}
|
|
20
|
+
getCart() {
|
|
21
|
+
const data = this.storage.getItem(this.key);
|
|
22
|
+
return data ? JSON.parse(data) : [];
|
|
23
|
+
}
|
|
24
|
+
save(cart) {
|
|
25
|
+
this.storage.setItem(this.key, JSON.stringify(cart));
|
|
26
|
+
}
|
|
27
|
+
addItem(newItem) {
|
|
28
|
+
const cart = this.getCart();
|
|
29
|
+
const exists = cart.find((item) => item.id === newItem.id);
|
|
30
|
+
if (exists)
|
|
31
|
+
exists.quantity = (exists.quantity || 1) + (newItem.quantity || 1);
|
|
32
|
+
else cart.push({ ...newItem, quantity: newItem.quantity || 1 });
|
|
33
|
+
this.save(cart);
|
|
34
|
+
}
|
|
35
|
+
removeItem(id) {
|
|
36
|
+
this.save(this.getCart().filter((item) => item.id !== id));
|
|
37
|
+
}
|
|
38
|
+
clear() {
|
|
39
|
+
this.save([]);
|
|
40
|
+
}
|
|
41
|
+
getItems(category) {
|
|
42
|
+
const items = this.getCart();
|
|
43
|
+
return category ? items.filter((item) => item.category === category) : items;
|
|
44
|
+
}
|
|
45
|
+
getTotal(category) {
|
|
46
|
+
let items = this.getCart();
|
|
47
|
+
if (category) {
|
|
48
|
+
items = items.filter((item) => item.category === category);
|
|
49
|
+
}
|
|
50
|
+
return items.reduce(
|
|
51
|
+
(sum, item) => sum + (item.price || 0) * (item.quantity || 1),
|
|
52
|
+
0
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
cart_default = Cart;
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
export {
|
|
61
|
+
Cart,
|
|
62
|
+
cart_default,
|
|
63
|
+
init_cart
|
|
64
|
+
};
|
package/dist/core/cart.global.js
CHANGED
|
@@ -1,28 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/core/cart.global.ts
|
|
21
|
-
var cart_global_exports = {};
|
|
22
|
-
__export(cart_global_exports, {
|
|
23
|
-
default: () => cart_global_default
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(cart_global_exports);
|
|
26
2
|
|
|
27
3
|
// src/core/storage.ts
|
|
28
4
|
function getStorage(type) {
|
|
@@ -78,4 +54,4 @@ var Cart = class {
|
|
|
78
54
|
var cart_default = Cart;
|
|
79
55
|
|
|
80
56
|
// src/core/cart.global.ts
|
|
81
|
-
|
|
57
|
+
module.exports = cart_default;
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import {
|
|
2
|
-
cart_default
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
cart_default,
|
|
3
|
+
init_cart
|
|
4
|
+
} from "../chunk-R4MVVWRS.mjs";
|
|
5
|
+
import {
|
|
6
|
+
__commonJS
|
|
7
|
+
} from "../chunk-IDMIQXPW.mjs";
|
|
5
8
|
|
|
6
9
|
// src/core/cart.global.ts
|
|
7
|
-
var
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
var require_cart_global = __commonJS({
|
|
11
|
+
"src/core/cart.global.ts"(exports, module) {
|
|
12
|
+
init_cart();
|
|
13
|
+
module.exports = cart_default;
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
export default require_cart_global();
|
package/dist/core/cart.mjs
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Cart,
|
|
3
|
-
cart_default
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
cart_default,
|
|
4
|
+
init_cart
|
|
5
|
+
} from "../chunk-R4MVVWRS.mjs";
|
|
6
|
+
import "../chunk-IDMIQXPW.mjs";
|
|
7
|
+
init_cart();
|
|
6
8
|
export {
|
|
7
9
|
Cart,
|
|
8
10
|
cart_default as default
|
package/dist/core/storage.mjs
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import {
|
|
2
|
-
Cart
|
|
3
|
-
|
|
2
|
+
Cart,
|
|
3
|
+
init_cart
|
|
4
|
+
} from "./chunk-R4MVVWRS.mjs";
|
|
4
5
|
import {
|
|
5
|
-
getStorage
|
|
6
|
-
|
|
6
|
+
getStorage,
|
|
7
|
+
init_storage
|
|
8
|
+
} from "./chunk-IDMIQXPW.mjs";
|
|
9
|
+
|
|
10
|
+
// src/index.ts
|
|
11
|
+
init_cart();
|
|
12
|
+
init_storage();
|
|
7
13
|
export {
|
|
8
14
|
Cart,
|
|
9
15
|
getStorage
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
CartContext,
|
|
3
3
|
CartProvider
|
|
4
|
-
} from "../chunk-
|
|
5
|
-
import "../chunk-
|
|
6
|
-
import "../chunk-
|
|
4
|
+
} from "../chunk-NQCCPA7E.mjs";
|
|
5
|
+
import "../chunk-R4MVVWRS.mjs";
|
|
6
|
+
import "../chunk-IDMIQXPW.mjs";
|
|
7
7
|
export {
|
|
8
8
|
CartContext,
|
|
9
9
|
CartProvider
|
package/dist/react/index.mjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
2
|
useCart
|
|
3
|
-
} from "../chunk-
|
|
3
|
+
} from "../chunk-2EP5A44J.mjs";
|
|
4
4
|
import {
|
|
5
5
|
CartProvider
|
|
6
|
-
} from "../chunk-
|
|
7
|
-
import "../chunk-
|
|
8
|
-
import "../chunk-
|
|
6
|
+
} from "../chunk-NQCCPA7E.mjs";
|
|
7
|
+
import "../chunk-R4MVVWRS.mjs";
|
|
8
|
+
import "../chunk-IDMIQXPW.mjs";
|
|
9
9
|
export {
|
|
10
10
|
CartProvider,
|
|
11
11
|
useCart
|
package/dist/react/useCart.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
useCart
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import "../chunk-
|
|
5
|
-
import "../chunk-
|
|
6
|
-
import "../chunk-
|
|
3
|
+
} from "../chunk-2EP5A44J.mjs";
|
|
4
|
+
import "../chunk-NQCCPA7E.mjs";
|
|
5
|
+
import "../chunk-R4MVVWRS.mjs";
|
|
6
|
+
import "../chunk-IDMIQXPW.mjs";
|
|
7
7
|
export {
|
|
8
8
|
useCart
|
|
9
9
|
};
|
package/package.json
CHANGED
|
@@ -1,62 +1,62 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@ravishranjan/cart",
|
|
3
|
-
"version": "2.1.
|
|
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.global.ts --bundle --minify --format=iife --global-name=Cart --platform=browser --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 <20.0.0",
|
|
49
|
-
"react-dom": ">=18.0.0 <20.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
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@ravishranjan/cart",
|
|
3
|
+
"version": "2.1.6",
|
|
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.global.ts --bundle --minify --format=iife --global-name=Cart --platform=browser --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 <20.0.0",
|
|
49
|
+
"react-dom": ">=18.0.0 <20.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
|
+
}
|
package/dist/chunk-7RBGBF5M.mjs
DELETED
package/dist/chunk-RBWMJEAD.mjs
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Cart
|
|
3
|
-
} from "./chunk-ZA6BU4XH.mjs";
|
|
4
|
-
|
|
5
|
-
// src/react/CartContext.tsx
|
|
6
|
-
import { createContext, useEffect, useState } from "react";
|
|
7
|
-
import { jsx } from "react/jsx-runtime";
|
|
8
|
-
var CartContext = createContext(null);
|
|
9
|
-
var CartProvider = ({
|
|
10
|
-
children,
|
|
11
|
-
storage = "localStorage",
|
|
12
|
-
key = "cart"
|
|
13
|
-
}) => {
|
|
14
|
-
const cartInstance = new Cart({ key, storage });
|
|
15
|
-
const [items, setItems] = useState(cartInstance.getItems());
|
|
16
|
-
const sync = () => setItems(cartInstance.getItems());
|
|
17
|
-
const api = {
|
|
18
|
-
cart: items,
|
|
19
|
-
addItem: (item) => {
|
|
20
|
-
cartInstance.addItem(item);
|
|
21
|
-
sync();
|
|
22
|
-
},
|
|
23
|
-
removeItem: (id) => {
|
|
24
|
-
cartInstance.removeItem(id);
|
|
25
|
-
sync();
|
|
26
|
-
},
|
|
27
|
-
getItems: (category) => {
|
|
28
|
-
cartInstance.getItems(category);
|
|
29
|
-
},
|
|
30
|
-
clear: () => {
|
|
31
|
-
cartInstance.clear();
|
|
32
|
-
sync();
|
|
33
|
-
},
|
|
34
|
-
total: cartInstance.getTotal()
|
|
35
|
-
};
|
|
36
|
-
useEffect(() => {
|
|
37
|
-
const handleSync = () => sync();
|
|
38
|
-
window.addEventListener("storage", handleSync);
|
|
39
|
-
return () => window.removeEventListener("storage", handleSync);
|
|
40
|
-
}, []);
|
|
41
|
-
return /* @__PURE__ */ jsx(CartContext.Provider, { value: api, children });
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
export {
|
|
45
|
-
CartContext,
|
|
46
|
-
CartProvider
|
|
47
|
-
};
|
package/dist/chunk-SR5T5KWM.mjs
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CartContext
|
|
3
|
-
} from "./chunk-XPRNR6EK.mjs";
|
|
4
|
-
|
|
5
|
-
// src/react/useCart.tsx
|
|
6
|
-
import { useContext } from "react";
|
|
7
|
-
function useCart() {
|
|
8
|
-
const context = useContext(CartContext);
|
|
9
|
-
if (!context) throw new Error("useCart must be used within CartProvider");
|
|
10
|
-
return context;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export {
|
|
14
|
-
useCart
|
|
15
|
-
};
|
package/dist/chunk-TVKKRH3O.mjs
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
getStorage
|
|
3
|
-
} from "./chunk-7RBGBF5M.mjs";
|
|
4
|
-
|
|
5
|
-
// src/core/cart.ts
|
|
6
|
-
var Cart = class {
|
|
7
|
-
constructor({
|
|
8
|
-
storage = "localStorage",
|
|
9
|
-
key = "cart"
|
|
10
|
-
} = {}) {
|
|
11
|
-
this.storage = getStorage(storage);
|
|
12
|
-
this.key = key;
|
|
13
|
-
}
|
|
14
|
-
getCart() {
|
|
15
|
-
const data = this.storage.getItem(this.key);
|
|
16
|
-
return data ? JSON.parse(data) : [];
|
|
17
|
-
}
|
|
18
|
-
save(cart) {
|
|
19
|
-
this.storage.setItem(this.key, JSON.stringify(cart));
|
|
20
|
-
}
|
|
21
|
-
addItem(newItem) {
|
|
22
|
-
const cart = this.getCart();
|
|
23
|
-
const exists = cart.find((item) => item.id === newItem.id);
|
|
24
|
-
if (exists)
|
|
25
|
-
exists.quantity = (exists.quantity || 1) + (newItem.quantity || 1);
|
|
26
|
-
else cart.push({ ...newItem, quantity: newItem.quantity || 1 });
|
|
27
|
-
this.save(cart);
|
|
28
|
-
}
|
|
29
|
-
removeItem(id) {
|
|
30
|
-
this.save(this.getCart().filter((item) => item.id !== id));
|
|
31
|
-
}
|
|
32
|
-
clear() {
|
|
33
|
-
this.save([]);
|
|
34
|
-
}
|
|
35
|
-
getItems(category) {
|
|
36
|
-
const items = this.getCart();
|
|
37
|
-
return category ? items.filter((item) => item.category === category) : items;
|
|
38
|
-
}
|
|
39
|
-
getTotal(category) {
|
|
40
|
-
let items = this.getCart();
|
|
41
|
-
if (category) {
|
|
42
|
-
items = items.filter((item) => item.category === category);
|
|
43
|
-
}
|
|
44
|
-
return items.reduce(
|
|
45
|
-
(sum, item) => sum + (item.price || 0) * (item.quantity || 1),
|
|
46
|
-
0
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export {
|
|
52
|
-
Cart
|
|
53
|
-
};
|
package/dist/chunk-ZA6BU4XH.mjs
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
getStorage
|
|
3
|
-
} from "./chunk-7RBGBF5M.mjs";
|
|
4
|
-
|
|
5
|
-
// src/core/cart.ts
|
|
6
|
-
var Cart = class {
|
|
7
|
-
constructor({
|
|
8
|
-
storage = "localStorage",
|
|
9
|
-
key = "cart"
|
|
10
|
-
} = {}) {
|
|
11
|
-
this.storage = getStorage(storage);
|
|
12
|
-
this.key = key;
|
|
13
|
-
}
|
|
14
|
-
getCart() {
|
|
15
|
-
const data = this.storage.getItem(this.key);
|
|
16
|
-
return data ? JSON.parse(data) : [];
|
|
17
|
-
}
|
|
18
|
-
save(cart) {
|
|
19
|
-
this.storage.setItem(this.key, JSON.stringify(cart));
|
|
20
|
-
}
|
|
21
|
-
addItem(newItem) {
|
|
22
|
-
const cart = this.getCart();
|
|
23
|
-
const exists = cart.find((item) => item.id === newItem.id);
|
|
24
|
-
if (exists)
|
|
25
|
-
exists.quantity = (exists.quantity || 1) + (newItem.quantity || 1);
|
|
26
|
-
else cart.push({ ...newItem, quantity: newItem.quantity || 1 });
|
|
27
|
-
this.save(cart);
|
|
28
|
-
}
|
|
29
|
-
removeItem(id) {
|
|
30
|
-
this.save(this.getCart().filter((item) => item.id !== id));
|
|
31
|
-
}
|
|
32
|
-
clear() {
|
|
33
|
-
this.save([]);
|
|
34
|
-
}
|
|
35
|
-
getItems(category) {
|
|
36
|
-
const items = this.getCart();
|
|
37
|
-
return category ? items.filter((item) => item.category === category) : items;
|
|
38
|
-
}
|
|
39
|
-
getTotal(category) {
|
|
40
|
-
let items = this.getCart();
|
|
41
|
-
if (category) {
|
|
42
|
-
items = items.filter((item) => item.category === category);
|
|
43
|
-
}
|
|
44
|
-
return items.reduce(
|
|
45
|
-
(sum, item) => sum + (item.price || 0) * (item.quantity || 1),
|
|
46
|
-
0
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
var cart_default = Cart;
|
|
51
|
-
|
|
52
|
-
export {
|
|
53
|
-
Cart,
|
|
54
|
-
cart_default
|
|
55
|
-
};
|