@ravishranjan/cart 2.0.2 → 2.0.3

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.
@@ -0,0 +1,43 @@
1
+ import {
2
+ Cart
3
+ } from "./chunk-YTW5MCYO.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 = ({ 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
+
40
+ export {
41
+ CartContext,
42
+ CartProvider
43
+ };
@@ -0,0 +1,15 @@
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
+ };
@@ -1,2 +1,9 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import * as react from 'react';
1
3
 
2
- export { }
4
+ declare const CartContext: react.Context<any>;
5
+ declare const CartProvider: ({ children }: {
6
+ children: React.ReactNode;
7
+ }) => react_jsx_runtime.JSX.Element;
8
+
9
+ export { CartContext, CartProvider };
@@ -1,2 +1,9 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import * as react from 'react';
1
3
 
2
- export { }
4
+ declare const CartContext: react.Context<any>;
5
+ declare const CartProvider: ({ children }: {
6
+ children: React.ReactNode;
7
+ }) => react_jsx_runtime.JSX.Element;
8
+
9
+ export { CartContext, CartProvider };
@@ -1 +1,118 @@
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/react/CartContext.tsx
21
+ var CartContext_exports = {};
22
+ __export(CartContext_exports, {
23
+ CartContext: () => CartContext,
24
+ CartProvider: () => CartProvider
25
+ });
26
+ module.exports = __toCommonJS(CartContext_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/CartContext.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
+ // Annotate the CommonJS export names for ESM import in node:
115
+ 0 && (module.exports = {
116
+ CartContext,
117
+ CartProvider
118
+ });
@@ -0,0 +1,10 @@
1
+ import {
2
+ CartContext,
3
+ CartProvider
4
+ } from "../chunk-3AB6KTDB.mjs";
5
+ import "../chunk-YTW5MCYO.mjs";
6
+ import "../chunk-7RBGBF5M.mjs";
7
+ export {
8
+ CartContext,
9
+ CartProvider
10
+ };
@@ -1,2 +1 @@
1
- export { CartProvider, useCart } from './useCart.mjs';
2
- import 'react/jsx-runtime';
1
+ export { useCart } from './useCart.mjs';
@@ -1,2 +1 @@
1
- export { CartProvider, useCart } from './useCart.js';
2
- import 'react/jsx-runtime';
1
+ export { useCart } from './useCart.js';
@@ -20,106 +20,25 @@ 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,
24
23
  useCart: () => useCart
25
24
  });
26
25
  module.exports = __toCommonJS(react_exports);
27
26
 
28
27
  // 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
- }
28
+ var import_react2 = require("react");
36
29
 
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
30
+ // src/react/CartContext.tsx
31
+ var import_react = require("react");
84
32
  var import_jsx_runtime = require("react/jsx-runtime");
85
33
  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
- };
34
+
35
+ // src/react/useCart.tsx
116
36
  var useCart = () => {
117
- const ctx = (0, import_react.useContext)(CartContext);
37
+ const ctx = (0, import_react2.useContext)(CartContext);
118
38
  if (!ctx) throw new Error("useCart must be used within CartProvider");
119
39
  return ctx;
120
40
  };
121
41
  // Annotate the CommonJS export names for ESM import in node:
122
42
  0 && (module.exports = {
123
- CartProvider,
124
43
  useCart
125
44
  });
@@ -1,10 +1,9 @@
1
1
  import {
2
- CartProvider,
3
2
  useCart
4
- } from "../chunk-RX26FWTT.mjs";
3
+ } from "../chunk-K4RESSJQ.mjs";
4
+ import "../chunk-3AB6KTDB.mjs";
5
5
  import "../chunk-YTW5MCYO.mjs";
6
6
  import "../chunk-7RBGBF5M.mjs";
7
7
  export {
8
- CartProvider,
9
8
  useCart
10
9
  };
@@ -1,8 +1,3 @@
1
- import * as react_jsx_runtime from 'react/jsx-runtime';
2
-
3
- declare const CartProvider: ({ children }: {
4
- children: React.ReactNode;
5
- }) => react_jsx_runtime.JSX.Element;
6
1
  declare const useCart: () => any;
7
2
 
8
- export { CartProvider, useCart };
3
+ export { useCart };
@@ -1,8 +1,3 @@
1
- import * as react_jsx_runtime from 'react/jsx-runtime';
2
-
3
- declare const CartProvider: ({ children }: {
4
- children: React.ReactNode;
5
- }) => react_jsx_runtime.JSX.Element;
6
1
  declare const useCart: () => any;
7
2
 
8
- export { CartProvider, useCart };
3
+ export { useCart };
@@ -20,104 +20,23 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/react/useCart.tsx
21
21
  var useCart_exports = {};
22
22
  __export(useCart_exports, {
23
- CartProvider: () => CartProvider,
24
23
  useCart: () => useCart
25
24
  });
26
25
  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
- }
26
+ var import_react2 = require("react");
34
27
 
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
28
+ // src/react/CartContext.tsx
29
+ var import_react = require("react");
82
30
  var import_jsx_runtime = require("react/jsx-runtime");
83
31
  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
- };
32
+
33
+ // src/react/useCart.tsx
114
34
  var useCart = () => {
115
- const ctx = (0, import_react.useContext)(CartContext);
35
+ const ctx = (0, import_react2.useContext)(CartContext);
116
36
  if (!ctx) throw new Error("useCart must be used within CartProvider");
117
37
  return ctx;
118
38
  };
119
39
  // Annotate the CommonJS export names for ESM import in node:
120
40
  0 && (module.exports = {
121
- CartProvider,
122
41
  useCart
123
42
  });
@@ -1,10 +1,9 @@
1
1
  import {
2
- CartProvider,
3
2
  useCart
4
- } from "../chunk-RX26FWTT.mjs";
3
+ } from "../chunk-K4RESSJQ.mjs";
4
+ import "../chunk-3AB6KTDB.mjs";
5
5
  import "../chunk-YTW5MCYO.mjs";
6
6
  import "../chunk-7RBGBF5M.mjs";
7
7
  export {
8
- CartProvider,
9
8
  useCart
10
9
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ravishranjan/cart",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
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",