@ravishranjan/cart 2.0.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/dist/chunk-7RBGBF5M.mjs +9 -0
- package/dist/chunk-RX26FWTT.mjs +48 -0
- package/dist/chunk-YTW5MCYO.mjs +53 -0
- package/dist/core/cart.d.mts +25 -0
- package/dist/core/cart.d.ts +25 -0
- package/dist/core/cart.js +81 -0
- package/dist/core/cart.mjs +7 -0
- package/dist/core/storage.d.mts +3 -0
- package/dist/core/storage.d.ts +3 -0
- package/dist/core/storage.js +33 -0
- package/dist/core/storage.mjs +6 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +83 -0
- package/dist/index.mjs +10 -0
- package/dist/react/CartContext.d.mts +2 -0
- package/dist/react/CartContext.d.ts +2 -0
- package/dist/react/CartContext.js +1 -0
- package/dist/react/CartContext.mjs +0 -0
- package/dist/react/index.d.mts +2 -0
- package/dist/react/index.d.ts +2 -0
- package/dist/react/index.js +125 -0
- package/dist/react/index.mjs +10 -0
- package/dist/react/useCart.d.mts +8 -0
- package/dist/react/useCart.d.ts +8 -0
- package/dist/react/useCart.js +123 -0
- package/dist/react/useCart.mjs +10 -0
- package/package.json +54 -0
@@ -0,0 +1,48 @@
|
|
1
|
+
import {
|
2
|
+
Cart
|
3
|
+
} from "./chunk-YTW5MCYO.mjs";
|
4
|
+
|
5
|
+
// src/react/useCart.tsx
|
6
|
+
import { createContext, useContext, useEffect, useState } from "react";
|
7
|
+
import { jsx } from "react/jsx-runtime";
|
8
|
+
var CartContext = createContext(null);
|
9
|
+
var CartProvider = ({ children }) => {
|
10
|
+
const cartInstance = new Cart();
|
11
|
+
const [items, setItems] = useState(cartInstance.getItems());
|
12
|
+
const sync = () => setItems(cartInstance.getItems());
|
13
|
+
const api = {
|
14
|
+
cart: items,
|
15
|
+
addItem: (item) => {
|
16
|
+
cartInstance.addItem(item);
|
17
|
+
sync();
|
18
|
+
},
|
19
|
+
removeItem: (id) => {
|
20
|
+
cartInstance.removeItem(id);
|
21
|
+
sync();
|
22
|
+
},
|
23
|
+
getItems: (category) => {
|
24
|
+
cartInstance.getItems(category);
|
25
|
+
},
|
26
|
+
clear: () => {
|
27
|
+
cartInstance.clear();
|
28
|
+
sync();
|
29
|
+
},
|
30
|
+
total: cartInstance.getTotal()
|
31
|
+
};
|
32
|
+
useEffect(() => {
|
33
|
+
const handleSync = () => sync();
|
34
|
+
window.addEventListener("storage", handleSync);
|
35
|
+
return () => window.removeEventListener("storage", handleSync);
|
36
|
+
}, []);
|
37
|
+
return /* @__PURE__ */ jsx(CartContext.Provider, { value: api, children });
|
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
|
+
|
45
|
+
export {
|
46
|
+
CartProvider,
|
47
|
+
useCart
|
48
|
+
};
|
@@ -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(item) {
|
22
|
+
const cart = this.getCart();
|
23
|
+
const exists = cart.find((item2) => item2.id === item2.id);
|
24
|
+
if (exists)
|
25
|
+
exists.quantity = (exists.quantity || 1) + (item.quantity || 1);
|
26
|
+
else cart.push({ ...item, quantity: item.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
|
+
};
|
@@ -0,0 +1,25 @@
|
|
1
|
+
interface CartItem {
|
2
|
+
id: string | number;
|
3
|
+
name: string;
|
4
|
+
price?: number;
|
5
|
+
quantity?: number;
|
6
|
+
category?: string;
|
7
|
+
[key: string]: any;
|
8
|
+
}
|
9
|
+
declare class Cart {
|
10
|
+
private storage;
|
11
|
+
private key;
|
12
|
+
constructor({ storage, key, }?: {
|
13
|
+
storage?: "localStorage" | "sessionStorage";
|
14
|
+
key?: string;
|
15
|
+
});
|
16
|
+
private getCart;
|
17
|
+
private save;
|
18
|
+
addItem(item: CartItem): void;
|
19
|
+
removeItem(id: string | number): void;
|
20
|
+
clear(): void;
|
21
|
+
getItems(category?: string): CartItem[];
|
22
|
+
getTotal(category?: string): number;
|
23
|
+
}
|
24
|
+
|
25
|
+
export { Cart, type CartItem };
|
@@ -0,0 +1,25 @@
|
|
1
|
+
interface CartItem {
|
2
|
+
id: string | number;
|
3
|
+
name: string;
|
4
|
+
price?: number;
|
5
|
+
quantity?: number;
|
6
|
+
category?: string;
|
7
|
+
[key: string]: any;
|
8
|
+
}
|
9
|
+
declare class Cart {
|
10
|
+
private storage;
|
11
|
+
private key;
|
12
|
+
constructor({ storage, key, }?: {
|
13
|
+
storage?: "localStorage" | "sessionStorage";
|
14
|
+
key?: string;
|
15
|
+
});
|
16
|
+
private getCart;
|
17
|
+
private save;
|
18
|
+
addItem(item: CartItem): void;
|
19
|
+
removeItem(id: string | number): void;
|
20
|
+
clear(): void;
|
21
|
+
getItems(category?: string): CartItem[];
|
22
|
+
getTotal(category?: string): number;
|
23
|
+
}
|
24
|
+
|
25
|
+
export { Cart, type CartItem };
|
@@ -0,0 +1,81 @@
|
|
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.ts
|
21
|
+
var cart_exports = {};
|
22
|
+
__export(cart_exports, {
|
23
|
+
Cart: () => Cart
|
24
|
+
});
|
25
|
+
module.exports = __toCommonJS(cart_exports);
|
26
|
+
|
27
|
+
// src/core/storage.ts
|
28
|
+
function getStorage(type) {
|
29
|
+
if (typeof window == "undefined") throw new Error("No window object found");
|
30
|
+
return type == "localStorage" ? window.localStorage : window.sessionStorage;
|
31
|
+
}
|
32
|
+
|
33
|
+
// src/core/cart.ts
|
34
|
+
var Cart = class {
|
35
|
+
constructor({
|
36
|
+
storage = "localStorage",
|
37
|
+
key = "cart"
|
38
|
+
} = {}) {
|
39
|
+
this.storage = getStorage(storage);
|
40
|
+
this.key = key;
|
41
|
+
}
|
42
|
+
getCart() {
|
43
|
+
const data = this.storage.getItem(this.key);
|
44
|
+
return data ? JSON.parse(data) : [];
|
45
|
+
}
|
46
|
+
save(cart) {
|
47
|
+
this.storage.setItem(this.key, JSON.stringify(cart));
|
48
|
+
}
|
49
|
+
addItem(item) {
|
50
|
+
const cart = this.getCart();
|
51
|
+
const exists = cart.find((item2) => item2.id === item2.id);
|
52
|
+
if (exists)
|
53
|
+
exists.quantity = (exists.quantity || 1) + (item.quantity || 1);
|
54
|
+
else cart.push({ ...item, quantity: item.quantity || 1 });
|
55
|
+
this.save(cart);
|
56
|
+
}
|
57
|
+
removeItem(id) {
|
58
|
+
this.save(this.getCart().filter((item) => item.id !== id));
|
59
|
+
}
|
60
|
+
clear() {
|
61
|
+
this.save([]);
|
62
|
+
}
|
63
|
+
getItems(category) {
|
64
|
+
const items = this.getCart();
|
65
|
+
return category ? items.filter((item) => item.category === category) : items;
|
66
|
+
}
|
67
|
+
getTotal(category) {
|
68
|
+
let items = this.getCart();
|
69
|
+
if (category) {
|
70
|
+
items = items.filter((item) => item.category === category);
|
71
|
+
}
|
72
|
+
return items.reduce(
|
73
|
+
(sum, item) => sum + (item.price || 0) * (item.quantity || 1),
|
74
|
+
0
|
75
|
+
);
|
76
|
+
}
|
77
|
+
};
|
78
|
+
// Annotate the CommonJS export names for ESM import in node:
|
79
|
+
0 && (module.exports = {
|
80
|
+
Cart
|
81
|
+
});
|
@@ -0,0 +1,33 @@
|
|
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/storage.ts
|
21
|
+
var storage_exports = {};
|
22
|
+
__export(storage_exports, {
|
23
|
+
getStorage: () => getStorage
|
24
|
+
});
|
25
|
+
module.exports = __toCommonJS(storage_exports);
|
26
|
+
function getStorage(type) {
|
27
|
+
if (typeof window == "undefined") throw new Error("No window object found");
|
28
|
+
return type == "localStorage" ? window.localStorage : window.sessionStorage;
|
29
|
+
}
|
30
|
+
// Annotate the CommonJS export names for ESM import in node:
|
31
|
+
0 && (module.exports = {
|
32
|
+
getStorage
|
33
|
+
});
|
package/dist/index.d.mts
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
@@ -0,0 +1,83 @@
|
|
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/index.ts
|
21
|
+
var index_exports = {};
|
22
|
+
__export(index_exports, {
|
23
|
+
Cart: () => Cart,
|
24
|
+
getStorage: () => getStorage
|
25
|
+
});
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
27
|
+
|
28
|
+
// src/core/storage.ts
|
29
|
+
function getStorage(type) {
|
30
|
+
if (typeof window == "undefined") throw new Error("No window object found");
|
31
|
+
return type == "localStorage" ? window.localStorage : window.sessionStorage;
|
32
|
+
}
|
33
|
+
|
34
|
+
// src/core/cart.ts
|
35
|
+
var Cart = class {
|
36
|
+
constructor({
|
37
|
+
storage = "localStorage",
|
38
|
+
key = "cart"
|
39
|
+
} = {}) {
|
40
|
+
this.storage = getStorage(storage);
|
41
|
+
this.key = key;
|
42
|
+
}
|
43
|
+
getCart() {
|
44
|
+
const data = this.storage.getItem(this.key);
|
45
|
+
return data ? JSON.parse(data) : [];
|
46
|
+
}
|
47
|
+
save(cart) {
|
48
|
+
this.storage.setItem(this.key, JSON.stringify(cart));
|
49
|
+
}
|
50
|
+
addItem(item) {
|
51
|
+
const cart = this.getCart();
|
52
|
+
const exists = cart.find((item2) => item2.id === item2.id);
|
53
|
+
if (exists)
|
54
|
+
exists.quantity = (exists.quantity || 1) + (item.quantity || 1);
|
55
|
+
else cart.push({ ...item, quantity: item.quantity || 1 });
|
56
|
+
this.save(cart);
|
57
|
+
}
|
58
|
+
removeItem(id) {
|
59
|
+
this.save(this.getCart().filter((item) => item.id !== id));
|
60
|
+
}
|
61
|
+
clear() {
|
62
|
+
this.save([]);
|
63
|
+
}
|
64
|
+
getItems(category) {
|
65
|
+
const items = this.getCart();
|
66
|
+
return category ? items.filter((item) => item.category === category) : items;
|
67
|
+
}
|
68
|
+
getTotal(category) {
|
69
|
+
let items = this.getCart();
|
70
|
+
if (category) {
|
71
|
+
items = items.filter((item) => item.category === category);
|
72
|
+
}
|
73
|
+
return items.reduce(
|
74
|
+
(sum, item) => sum + (item.price || 0) * (item.quantity || 1),
|
75
|
+
0
|
76
|
+
);
|
77
|
+
}
|
78
|
+
};
|
79
|
+
// Annotate the CommonJS export names for ESM import in node:
|
80
|
+
0 && (module.exports = {
|
81
|
+
Cart,
|
82
|
+
getStorage
|
83
|
+
});
|
package/dist/index.mjs
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
"use strict";
|
File without changes
|
@@ -0,0 +1,125 @@
|
|
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/react/index.ts
|
21
|
+
var react_exports = {};
|
22
|
+
__export(react_exports, {
|
23
|
+
CartProvider: () => CartProvider,
|
24
|
+
useCart: () => useCart
|
25
|
+
});
|
26
|
+
module.exports = __toCommonJS(react_exports);
|
27
|
+
|
28
|
+
// src/react/useCart.tsx
|
29
|
+
var import_react = require("react");
|
30
|
+
|
31
|
+
// src/core/storage.ts
|
32
|
+
function getStorage(type) {
|
33
|
+
if (typeof window == "undefined") throw new Error("No window object found");
|
34
|
+
return type == "localStorage" ? window.localStorage : window.sessionStorage;
|
35
|
+
}
|
36
|
+
|
37
|
+
// src/core/cart.ts
|
38
|
+
var Cart = class {
|
39
|
+
constructor({
|
40
|
+
storage = "localStorage",
|
41
|
+
key = "cart"
|
42
|
+
} = {}) {
|
43
|
+
this.storage = getStorage(storage);
|
44
|
+
this.key = key;
|
45
|
+
}
|
46
|
+
getCart() {
|
47
|
+
const data = this.storage.getItem(this.key);
|
48
|
+
return data ? JSON.parse(data) : [];
|
49
|
+
}
|
50
|
+
save(cart) {
|
51
|
+
this.storage.setItem(this.key, JSON.stringify(cart));
|
52
|
+
}
|
53
|
+
addItem(item) {
|
54
|
+
const cart = this.getCart();
|
55
|
+
const exists = cart.find((item2) => item2.id === item2.id);
|
56
|
+
if (exists)
|
57
|
+
exists.quantity = (exists.quantity || 1) + (item.quantity || 1);
|
58
|
+
else cart.push({ ...item, quantity: item.quantity || 1 });
|
59
|
+
this.save(cart);
|
60
|
+
}
|
61
|
+
removeItem(id) {
|
62
|
+
this.save(this.getCart().filter((item) => item.id !== id));
|
63
|
+
}
|
64
|
+
clear() {
|
65
|
+
this.save([]);
|
66
|
+
}
|
67
|
+
getItems(category) {
|
68
|
+
const items = this.getCart();
|
69
|
+
return category ? items.filter((item) => item.category === category) : items;
|
70
|
+
}
|
71
|
+
getTotal(category) {
|
72
|
+
let items = this.getCart();
|
73
|
+
if (category) {
|
74
|
+
items = items.filter((item) => item.category === category);
|
75
|
+
}
|
76
|
+
return items.reduce(
|
77
|
+
(sum, item) => sum + (item.price || 0) * (item.quantity || 1),
|
78
|
+
0
|
79
|
+
);
|
80
|
+
}
|
81
|
+
};
|
82
|
+
|
83
|
+
// src/react/useCart.tsx
|
84
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
85
|
+
var CartContext = (0, import_react.createContext)(null);
|
86
|
+
var CartProvider = ({ children }) => {
|
87
|
+
const cartInstance = new Cart();
|
88
|
+
const [items, setItems] = (0, import_react.useState)(cartInstance.getItems());
|
89
|
+
const sync = () => setItems(cartInstance.getItems());
|
90
|
+
const api = {
|
91
|
+
cart: items,
|
92
|
+
addItem: (item) => {
|
93
|
+
cartInstance.addItem(item);
|
94
|
+
sync();
|
95
|
+
},
|
96
|
+
removeItem: (id) => {
|
97
|
+
cartInstance.removeItem(id);
|
98
|
+
sync();
|
99
|
+
},
|
100
|
+
getItems: (category) => {
|
101
|
+
cartInstance.getItems(category);
|
102
|
+
},
|
103
|
+
clear: () => {
|
104
|
+
cartInstance.clear();
|
105
|
+
sync();
|
106
|
+
},
|
107
|
+
total: cartInstance.getTotal()
|
108
|
+
};
|
109
|
+
(0, import_react.useEffect)(() => {
|
110
|
+
const handleSync = () => sync();
|
111
|
+
window.addEventListener("storage", handleSync);
|
112
|
+
return () => window.removeEventListener("storage", handleSync);
|
113
|
+
}, []);
|
114
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CartContext.Provider, { value: api, children });
|
115
|
+
};
|
116
|
+
var useCart = () => {
|
117
|
+
const ctx = (0, import_react.useContext)(CartContext);
|
118
|
+
if (!ctx) throw new Error("useCart must be used within CartProvider");
|
119
|
+
return ctx;
|
120
|
+
};
|
121
|
+
// Annotate the CommonJS export names for ESM import in node:
|
122
|
+
0 && (module.exports = {
|
123
|
+
CartProvider,
|
124
|
+
useCart
|
125
|
+
});
|
@@ -0,0 +1,123 @@
|
|
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/react/useCart.tsx
|
21
|
+
var useCart_exports = {};
|
22
|
+
__export(useCart_exports, {
|
23
|
+
CartProvider: () => CartProvider,
|
24
|
+
useCart: () => useCart
|
25
|
+
});
|
26
|
+
module.exports = __toCommonJS(useCart_exports);
|
27
|
+
var import_react = require("react");
|
28
|
+
|
29
|
+
// src/core/storage.ts
|
30
|
+
function getStorage(type) {
|
31
|
+
if (typeof window == "undefined") throw new Error("No window object found");
|
32
|
+
return type == "localStorage" ? window.localStorage : window.sessionStorage;
|
33
|
+
}
|
34
|
+
|
35
|
+
// src/core/cart.ts
|
36
|
+
var Cart = class {
|
37
|
+
constructor({
|
38
|
+
storage = "localStorage",
|
39
|
+
key = "cart"
|
40
|
+
} = {}) {
|
41
|
+
this.storage = getStorage(storage);
|
42
|
+
this.key = key;
|
43
|
+
}
|
44
|
+
getCart() {
|
45
|
+
const data = this.storage.getItem(this.key);
|
46
|
+
return data ? JSON.parse(data) : [];
|
47
|
+
}
|
48
|
+
save(cart) {
|
49
|
+
this.storage.setItem(this.key, JSON.stringify(cart));
|
50
|
+
}
|
51
|
+
addItem(item) {
|
52
|
+
const cart = this.getCart();
|
53
|
+
const exists = cart.find((item2) => item2.id === item2.id);
|
54
|
+
if (exists)
|
55
|
+
exists.quantity = (exists.quantity || 1) + (item.quantity || 1);
|
56
|
+
else cart.push({ ...item, quantity: item.quantity || 1 });
|
57
|
+
this.save(cart);
|
58
|
+
}
|
59
|
+
removeItem(id) {
|
60
|
+
this.save(this.getCart().filter((item) => item.id !== id));
|
61
|
+
}
|
62
|
+
clear() {
|
63
|
+
this.save([]);
|
64
|
+
}
|
65
|
+
getItems(category) {
|
66
|
+
const items = this.getCart();
|
67
|
+
return category ? items.filter((item) => item.category === category) : items;
|
68
|
+
}
|
69
|
+
getTotal(category) {
|
70
|
+
let items = this.getCart();
|
71
|
+
if (category) {
|
72
|
+
items = items.filter((item) => item.category === category);
|
73
|
+
}
|
74
|
+
return items.reduce(
|
75
|
+
(sum, item) => sum + (item.price || 0) * (item.quantity || 1),
|
76
|
+
0
|
77
|
+
);
|
78
|
+
}
|
79
|
+
};
|
80
|
+
|
81
|
+
// src/react/useCart.tsx
|
82
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
83
|
+
var CartContext = (0, import_react.createContext)(null);
|
84
|
+
var CartProvider = ({ children }) => {
|
85
|
+
const cartInstance = new Cart();
|
86
|
+
const [items, setItems] = (0, import_react.useState)(cartInstance.getItems());
|
87
|
+
const sync = () => setItems(cartInstance.getItems());
|
88
|
+
const api = {
|
89
|
+
cart: items,
|
90
|
+
addItem: (item) => {
|
91
|
+
cartInstance.addItem(item);
|
92
|
+
sync();
|
93
|
+
},
|
94
|
+
removeItem: (id) => {
|
95
|
+
cartInstance.removeItem(id);
|
96
|
+
sync();
|
97
|
+
},
|
98
|
+
getItems: (category) => {
|
99
|
+
cartInstance.getItems(category);
|
100
|
+
},
|
101
|
+
clear: () => {
|
102
|
+
cartInstance.clear();
|
103
|
+
sync();
|
104
|
+
},
|
105
|
+
total: cartInstance.getTotal()
|
106
|
+
};
|
107
|
+
(0, import_react.useEffect)(() => {
|
108
|
+
const handleSync = () => sync();
|
109
|
+
window.addEventListener("storage", handleSync);
|
110
|
+
return () => window.removeEventListener("storage", handleSync);
|
111
|
+
}, []);
|
112
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CartContext.Provider, { value: api, children });
|
113
|
+
};
|
114
|
+
var useCart = () => {
|
115
|
+
const ctx = (0, import_react.useContext)(CartContext);
|
116
|
+
if (!ctx) throw new Error("useCart must be used within CartProvider");
|
117
|
+
return ctx;
|
118
|
+
};
|
119
|
+
// Annotate the CommonJS export names for ESM import in node:
|
120
|
+
0 && (module.exports = {
|
121
|
+
CartProvider,
|
122
|
+
useCart
|
123
|
+
});
|
package/package.json
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
{
|
2
|
+
"name": "@ravishranjan/cart",
|
3
|
+
"version": "2.0.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
|
+
},
|
15
|
+
"keywords": [
|
16
|
+
"cart",
|
17
|
+
"react",
|
18
|
+
"localstorage",
|
19
|
+
"frontend",
|
20
|
+
"native"
|
21
|
+
],
|
22
|
+
"exports": {
|
23
|
+
".": {
|
24
|
+
"import": "./dist/index.mjs",
|
25
|
+
"require": "./dist/index.js"
|
26
|
+
},
|
27
|
+
"./react": {
|
28
|
+
"import": "./dist/react/index.mjs",
|
29
|
+
"require": "./dist/react/index.js"
|
30
|
+
}
|
31
|
+
},
|
32
|
+
"author": "Ravish Ranjan",
|
33
|
+
"license": "ISC",
|
34
|
+
"devDependencies": {
|
35
|
+
"@types/react": "^19.2.2",
|
36
|
+
"tsup": "^8.5.0"
|
37
|
+
},
|
38
|
+
"peerDependencies": {
|
39
|
+
"react": "^18.0.0 || ^19.0.0",
|
40
|
+
"react-dom": "^18.0.0 || ^19.0.0",
|
41
|
+
"typescript": "^5.9.3"
|
42
|
+
},
|
43
|
+
"peerDependenciesMeta": {
|
44
|
+
"react": {
|
45
|
+
"optional": true
|
46
|
+
},
|
47
|
+
"react-dom": {
|
48
|
+
"optional": true
|
49
|
+
},
|
50
|
+
"typescript": {
|
51
|
+
"optional": true
|
52
|
+
}
|
53
|
+
}
|
54
|
+
}
|