@ravishranjan/cart 2.0.4 → 2.0.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.
@@ -1 +1,4 @@
1
1
  export { useCart } from './useCart.mjs';
2
+ export { CartProvider } from './CartContext.mjs';
3
+ import 'react/jsx-runtime';
4
+ import 'react';
@@ -1 +1,4 @@
1
1
  export { useCart } from './useCart.js';
2
+ export { CartProvider } from './CartContext.js';
3
+ import 'react/jsx-runtime';
4
+ import 'react';
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/react/index.ts
21
21
  var react_exports = {};
22
22
  __export(react_exports, {
23
+ CartProvider: () => CartProvider,
23
24
  useCart: () => useCart
24
25
  });
25
26
  module.exports = __toCommonJS(react_exports);
@@ -29,8 +30,92 @@ var import_react2 = require("react");
29
30
 
30
31
  // src/react/CartContext.tsx
31
32
  var import_react = require("react");
33
+
34
+ // src/core/storage.ts
35
+ function getStorage(type) {
36
+ if (typeof window == "undefined") throw new Error("No window object found");
37
+ return type == "localStorage" ? window.localStorage : window.sessionStorage;
38
+ }
39
+
40
+ // src/core/cart.ts
41
+ var Cart = class {
42
+ constructor({
43
+ storage = "localStorage",
44
+ key = "cart"
45
+ } = {}) {
46
+ this.storage = getStorage(storage);
47
+ this.key = key;
48
+ }
49
+ getCart() {
50
+ const data = this.storage.getItem(this.key);
51
+ return data ? JSON.parse(data) : [];
52
+ }
53
+ save(cart) {
54
+ this.storage.setItem(this.key, JSON.stringify(cart));
55
+ }
56
+ addItem(item) {
57
+ const cart = this.getCart();
58
+ const exists = cart.find((item2) => item2.id === item2.id);
59
+ if (exists)
60
+ exists.quantity = (exists.quantity || 1) + (item.quantity || 1);
61
+ else cart.push({ ...item, quantity: item.quantity || 1 });
62
+ this.save(cart);
63
+ }
64
+ removeItem(id) {
65
+ this.save(this.getCart().filter((item) => item.id !== id));
66
+ }
67
+ clear() {
68
+ this.save([]);
69
+ }
70
+ getItems(category) {
71
+ const items = this.getCart();
72
+ return category ? items.filter((item) => item.category === category) : items;
73
+ }
74
+ getTotal(category) {
75
+ let items = this.getCart();
76
+ if (category) {
77
+ items = items.filter((item) => item.category === category);
78
+ }
79
+ return items.reduce(
80
+ (sum, item) => sum + (item.price || 0) * (item.quantity || 1),
81
+ 0
82
+ );
83
+ }
84
+ };
85
+
86
+ // src/react/CartContext.tsx
32
87
  var import_jsx_runtime = require("react/jsx-runtime");
33
88
  var CartContext = (0, import_react.createContext)(null);
89
+ var CartProvider = ({ children }) => {
90
+ const cartInstance = new Cart();
91
+ const [items, setItems] = (0, import_react.useState)(cartInstance.getItems());
92
+ const sync = () => setItems(cartInstance.getItems());
93
+ const api = {
94
+ cart: items,
95
+ addItem: (item) => {
96
+ cartInstance.addItem(item);
97
+ sync();
98
+ },
99
+ removeItem: (id) => {
100
+ cartInstance.removeItem(id);
101
+ sync();
102
+ },
103
+ getItems: (category) => {
104
+ cartInstance.getItems(category);
105
+ },
106
+ clear: () => {
107
+ cartInstance.clear();
108
+ sync();
109
+ },
110
+ total: cartInstance.getTotal()
111
+ };
112
+ (0, import_react.useEffect)(() => {
113
+ const handleSync = () => sync();
114
+ window.addEventListener("storage", handleSync);
115
+ return () => window.removeEventListener("storage", handleSync);
116
+ }, []);
117
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CartContext.Provider, { value: api, children });
118
+ };
34
119
 
35
120
  // src/react/useCart.tsx
36
121
  var useCart = () => {
@@ -40,5 +125,6 @@ var useCart = () => {
40
125
  };
41
126
  // Annotate the CommonJS export names for ESM import in node:
42
127
  0 && (module.exports = {
128
+ CartProvider,
43
129
  useCart
44
130
  });
@@ -1,9 +1,12 @@
1
1
  import {
2
2
  useCart
3
3
  } from "../chunk-K4RESSJQ.mjs";
4
- import "../chunk-3AB6KTDB.mjs";
4
+ import {
5
+ CartProvider
6
+ } from "../chunk-3AB6KTDB.mjs";
5
7
  import "../chunk-YTW5MCYO.mjs";
6
8
  import "../chunk-7RBGBF5M.mjs";
7
9
  export {
10
+ CartProvider,
8
11
  useCart
9
12
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ravishranjan/cart",
3
- "version": "2.0.4",
3
+ "version": "2.0.6",
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",
@@ -21,10 +21,12 @@
21
21
  ],
22
22
  "exports": {
23
23
  ".": {
24
+ "types": "./dist/index.d.ts",
24
25
  "import": "./dist/index.mjs",
25
26
  "require": "./dist/index.js"
26
27
  },
27
28
  "./react": {
29
+ "types": "./dist/react/index.d.ts",
28
30
  "import": "./dist/react/index.mjs",
29
31
  "require": "./dist/react/index.js"
30
32
  }