@ravishranjan/cart 2.0.7 → 2.0.9
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/dist/{chunk-RX26FWTT.mjs → chunk-JIOUNG7W.mjs} +5 -10
- package/dist/chunk-JRM56HH5.mjs +15 -0
- package/dist/chunk-TVKKRH3O.mjs +53 -0
- package/dist/core/cart.d.mts +1 -1
- package/dist/core/cart.d.ts +1 -1
- package/dist/core/cart.js +4 -4
- package/dist/core/cart.mjs +1 -1
- package/dist/index.js +4 -4
- package/dist/index.mjs +1 -1
- package/dist/react/CartContext.js +4 -4
- package/dist/react/CartContext.mjs +2 -2
- package/dist/react/index.js +4 -4
- package/dist/react/index.mjs +3 -3
- package/dist/react/useCart.mjs +3 -3
- package/package.json +4 -4
- package/dist/chunk-K4RESSJQ.mjs +0 -15
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Cart
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-TVKKRH3O.mjs";
|
|
4
4
|
|
|
5
|
-
// src/react/
|
|
6
|
-
import { createContext,
|
|
5
|
+
// src/react/CartContext.tsx
|
|
6
|
+
import { createContext, useEffect, useState } from "react";
|
|
7
7
|
import { jsx } from "react/jsx-runtime";
|
|
8
8
|
var CartContext = createContext(null);
|
|
9
9
|
var CartProvider = ({ children }) => {
|
|
@@ -36,13 +36,8 @@ var CartProvider = ({ children }) => {
|
|
|
36
36
|
}, []);
|
|
37
37
|
return /* @__PURE__ */ jsx(CartContext.Provider, { value: api, children });
|
|
38
38
|
};
|
|
39
|
-
var useCart = () => {
|
|
40
|
-
const ctx = useContext(CartContext);
|
|
41
|
-
if (!ctx) throw new Error("useCart must be used within CartProvider");
|
|
42
|
-
return ctx;
|
|
43
|
-
};
|
|
44
39
|
|
|
45
40
|
export {
|
|
46
|
-
|
|
47
|
-
|
|
41
|
+
CartContext,
|
|
42
|
+
CartProvider
|
|
48
43
|
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CartContext
|
|
3
|
+
} from "./chunk-JIOUNG7W.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
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
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/core/cart.d.mts
CHANGED
package/dist/core/cart.d.ts
CHANGED
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(
|
|
49
|
+
addItem(newItem) {
|
|
50
50
|
const cart = this.getCart();
|
|
51
|
-
const exists = cart.find((
|
|
51
|
+
const exists = cart.find((item) => item.id === newItem.id);
|
|
52
52
|
if (exists)
|
|
53
|
-
exists.quantity = (exists.quantity || 1) + (
|
|
54
|
-
else cart.push({ ...
|
|
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) {
|
package/dist/core/cart.mjs
CHANGED
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(
|
|
50
|
+
addItem(newItem) {
|
|
51
51
|
const cart = this.getCart();
|
|
52
|
-
const exists = cart.find((
|
|
52
|
+
const exists = cart.find((item) => item.id === newItem.id);
|
|
53
53
|
if (exists)
|
|
54
|
-
exists.quantity = (exists.quantity || 1) + (
|
|
55
|
-
else cart.push({ ...
|
|
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
|
@@ -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(
|
|
51
|
+
addItem(newItem) {
|
|
52
52
|
const cart = this.getCart();
|
|
53
|
-
const exists = cart.find((
|
|
53
|
+
const exists = cart.find((item) => item.id === newItem.id);
|
|
54
54
|
if (exists)
|
|
55
|
-
exists.quantity = (exists.quantity || 1) + (
|
|
56
|
-
else cart.push({ ...
|
|
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) {
|
package/dist/react/index.js
CHANGED
|
@@ -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(
|
|
56
|
+
addItem(newItem) {
|
|
57
57
|
const cart = this.getCart();
|
|
58
|
-
const exists = cart.find((
|
|
58
|
+
const exists = cart.find((item) => item.id === newItem.id);
|
|
59
59
|
if (exists)
|
|
60
|
-
exists.quantity = (exists.quantity || 1) + (
|
|
61
|
-
else cart.push({ ...
|
|
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) {
|
package/dist/react/index.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
useCart
|
|
3
|
-
} from "../chunk-
|
|
3
|
+
} from "../chunk-JRM56HH5.mjs";
|
|
4
4
|
import {
|
|
5
5
|
CartProvider
|
|
6
|
-
} from "../chunk-
|
|
7
|
-
import "../chunk-
|
|
6
|
+
} from "../chunk-JIOUNG7W.mjs";
|
|
7
|
+
import "../chunk-TVKKRH3O.mjs";
|
|
8
8
|
import "../chunk-7RBGBF5M.mjs";
|
|
9
9
|
export {
|
|
10
10
|
CartProvider,
|
package/dist/react/useCart.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
useCart
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import "../chunk-
|
|
5
|
-
import "../chunk-
|
|
3
|
+
} from "../chunk-JRM56HH5.mjs";
|
|
4
|
+
import "../chunk-JIOUNG7W.mjs";
|
|
5
|
+
import "../chunk-TVKKRH3O.mjs";
|
|
6
6
|
import "../chunk-7RBGBF5M.mjs";
|
|
7
7
|
export {
|
|
8
8
|
useCart
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ravishranjan/cart",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.9",
|
|
4
4
|
"description": "A native cart system for web apps with optional React hooks",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -35,12 +35,12 @@
|
|
|
35
35
|
"license": "ISC",
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@types/react": "^19.2.2",
|
|
38
|
-
"tsup": "^8.5.0"
|
|
38
|
+
"tsup": "^8.5.0",
|
|
39
|
+
"typescript": "^5.9.3"
|
|
39
40
|
},
|
|
40
41
|
"peerDependencies": {
|
|
41
42
|
"react": "^18.0.0 || ^19.0.0",
|
|
42
|
-
"react-dom": "^18.0.0 || ^19.0.0"
|
|
43
|
-
"typescript": "^5.9.3"
|
|
43
|
+
"react-dom": "^18.0.0 || ^19.0.0"
|
|
44
44
|
},
|
|
45
45
|
"peerDependenciesMeta": {
|
|
46
46
|
"react": {
|
package/dist/chunk-K4RESSJQ.mjs
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CartContext
|
|
3
|
-
} from "./chunk-3AB6KTDB.mjs";
|
|
4
|
-
|
|
5
|
-
// src/react/useCart.tsx
|
|
6
|
-
import { useContext } from "react";
|
|
7
|
-
var useCart = () => {
|
|
8
|
-
const ctx = useContext(CartContext);
|
|
9
|
-
if (!ctx) throw new Error("useCart must be used within CartProvider");
|
|
10
|
-
return ctx;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export {
|
|
14
|
-
useCart
|
|
15
|
-
};
|