@ravishranjan/cart 2.1.0 → 2.1.2

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/cart.min.js CHANGED
@@ -1 +1 @@
1
- "use strict";var Cart=(()=>{var s=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var u=Object.prototype.hasOwnProperty;var l=(i,t)=>{for(var e in t)s(i,e,{get:t[e],enumerable:!0})},y=(i,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of c(t))!u.call(i,a)&&a!==e&&s(i,a,{get:()=>t[a],enumerable:!(r=g(t,a))||r.enumerable});return i};var d=i=>y(s({},"__esModule",{value:!0}),i);var h={};l(h,{Cart:()=>o});function n(i){if(typeof window>"u")throw new Error("No window object found");return i=="localStorage"?window.localStorage:window.sessionStorage}var o=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)}};return d(h);})();
1
+ "use strict";var Cart=(()=>{var o=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var u=Object.prototype.hasOwnProperty;var l=(i,t)=>{for(var e in t)o(i,e,{get:t[e],enumerable:!0})},d=(i,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of c(t))!u.call(i,a)&&a!==e&&o(i,a,{get:()=>t[a],enumerable:!(r=g(t,a))||r.enumerable});return i};var y=i=>d(o({},"__esModule",{value:!0}),i);var h={};l(h,{Cart:()=>s,default:()=>f});function n(i){if(typeof window>"u")throw new Error("No window object found");return i=="localStorage"?window.localStorage:window.sessionStorage}var 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)}},f=s;return y(h);})();
@@ -0,0 +1,15 @@
1
+ import {
2
+ CartContext
3
+ } from "./chunk-RBWMJEAD.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,47 @@
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
+ };
@@ -0,0 +1,55 @@
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
+ };
@@ -22,4 +22,4 @@ declare class Cart {
22
22
  getTotal(category?: string): number;
23
23
  }
24
24
 
25
- export { Cart, type CartItem };
25
+ export { Cart, type CartItem, Cart as default };
@@ -22,4 +22,4 @@ declare class Cart {
22
22
  getTotal(category?: string): number;
23
23
  }
24
24
 
25
- export { Cart, type CartItem };
25
+ export { Cart, type CartItem, Cart as default };
package/dist/core/cart.js CHANGED
@@ -20,7 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/core/cart.ts
21
21
  var cart_exports = {};
22
22
  __export(cart_exports, {
23
- Cart: () => Cart
23
+ Cart: () => Cart,
24
+ default: () => cart_default
24
25
  });
25
26
  module.exports = __toCommonJS(cart_exports);
26
27
 
@@ -75,6 +76,7 @@ var Cart = class {
75
76
  );
76
77
  }
77
78
  };
79
+ var cart_default = Cart;
78
80
  // Annotate the CommonJS export names for ESM import in node:
79
81
  0 && (module.exports = {
80
82
  Cart
@@ -1,7 +1,9 @@
1
1
  import {
2
- Cart
3
- } from "../chunk-TVKKRH3O.mjs";
2
+ Cart,
3
+ cart_default
4
+ } from "../chunk-ZA6BU4XH.mjs";
4
5
  import "../chunk-7RBGBF5M.mjs";
5
6
  export {
6
- Cart
7
+ Cart,
8
+ cart_default as default
7
9
  };
package/dist/index.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- export { Cart, CartItem } from './core/cart.mjs';
1
+ export { default as Cart, CartItem } from './core/cart.mjs';
2
2
  export { getStorage } from './core/storage.mjs';
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { Cart, CartItem } from './core/cart.js';
1
+ export { default as Cart, CartItem } from './core/cart.js';
2
2
  export { getStorage } from './core/storage.js';
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  Cart
3
- } from "./chunk-TVKKRH3O.mjs";
3
+ } from "./chunk-ZA6BU4XH.mjs";
4
4
  import {
5
5
  getStorage
6
6
  } from "./chunk-7RBGBF5M.mjs";
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  CartContext,
3
3
  CartProvider
4
- } from "../chunk-XPRNR6EK.mjs";
5
- import "../chunk-TVKKRH3O.mjs";
4
+ } from "../chunk-RBWMJEAD.mjs";
5
+ import "../chunk-ZA6BU4XH.mjs";
6
6
  import "../chunk-7RBGBF5M.mjs";
7
7
  export {
8
8
  CartContext,
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  useCart
3
- } from "../chunk-SR5T5KWM.mjs";
3
+ } from "../chunk-BCVOXMCT.mjs";
4
4
  import {
5
5
  CartProvider
6
- } from "../chunk-XPRNR6EK.mjs";
7
- import "../chunk-TVKKRH3O.mjs";
6
+ } from "../chunk-RBWMJEAD.mjs";
7
+ import "../chunk-ZA6BU4XH.mjs";
8
8
  import "../chunk-7RBGBF5M.mjs";
9
9
  export {
10
10
  CartProvider,
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  useCart
3
- } from "../chunk-SR5T5KWM.mjs";
4
- import "../chunk-XPRNR6EK.mjs";
5
- import "../chunk-TVKKRH3O.mjs";
3
+ } from "../chunk-BCVOXMCT.mjs";
4
+ import "../chunk-RBWMJEAD.mjs";
5
+ import "../chunk-ZA6BU4XH.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.1.0",
3
+ "version": "2.1.2",
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",
@@ -45,8 +45,8 @@
45
45
  "typescript": "^5.9.3"
46
46
  },
47
47
  "peerDependencies": {
48
- "react": ">=18.0.0",
49
- "react-dom": "^18.0.0"
48
+ "react": ">=18.0.0 <20.0.0",
49
+ "react-dom": ">=18.0.0 <20.0.0"
50
50
  },
51
51
  "peerDependenciesMeta": {
52
52
  "react": {