@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.
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  Cart
3
- } from "./chunk-YTW5MCYO.mjs";
3
+ } from "./chunk-TVKKRH3O.mjs";
4
4
 
5
- // src/react/useCart.tsx
6
- import { createContext, useContext, useEffect, useState } from "react";
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
- CartProvider,
47
- useCart
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
+ };
@@ -15,7 +15,7 @@ declare class Cart {
15
15
  });
16
16
  private getCart;
17
17
  private save;
18
- addItem(item: CartItem): void;
18
+ addItem(newItem: CartItem): void;
19
19
  removeItem(id: string | number): void;
20
20
  clear(): void;
21
21
  getItems(category?: string): CartItem[];
@@ -15,7 +15,7 @@ declare class Cart {
15
15
  });
16
16
  private getCart;
17
17
  private save;
18
- addItem(item: CartItem): void;
18
+ addItem(newItem: CartItem): void;
19
19
  removeItem(id: string | number): void;
20
20
  clear(): void;
21
21
  getItems(category?: string): CartItem[];
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(item) {
49
+ addItem(newItem) {
50
50
  const cart = this.getCart();
51
- const exists = cart.find((item2) => item2.id === item2.id);
51
+ const exists = cart.find((item) => item.id === newItem.id);
52
52
  if (exists)
53
- exists.quantity = (exists.quantity || 1) + (item.quantity || 1);
54
- else cart.push({ ...item, quantity: item.quantity || 1 });
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) {
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  Cart
3
- } from "../chunk-YTW5MCYO.mjs";
3
+ } from "../chunk-TVKKRH3O.mjs";
4
4
  import "../chunk-7RBGBF5M.mjs";
5
5
  export {
6
6
  Cart
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(item) {
50
+ addItem(newItem) {
51
51
  const cart = this.getCart();
52
- const exists = cart.find((item2) => item2.id === item2.id);
52
+ const exists = cart.find((item) => item.id === newItem.id);
53
53
  if (exists)
54
- exists.quantity = (exists.quantity || 1) + (item.quantity || 1);
55
- else cart.push({ ...item, quantity: item.quantity || 1 });
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
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  Cart
3
- } from "./chunk-YTW5MCYO.mjs";
3
+ } from "./chunk-TVKKRH3O.mjs";
4
4
  import {
5
5
  getStorage
6
6
  } from "./chunk-7RBGBF5M.mjs";
@@ -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(item) {
51
+ addItem(newItem) {
52
52
  const cart = this.getCart();
53
- const exists = cart.find((item2) => item2.id === item2.id);
53
+ const exists = cart.find((item) => item.id === newItem.id);
54
54
  if (exists)
55
- exists.quantity = (exists.quantity || 1) + (item.quantity || 1);
56
- else cart.push({ ...item, quantity: item.quantity || 1 });
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) {
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  CartContext,
3
3
  CartProvider
4
- } from "../chunk-3AB6KTDB.mjs";
5
- import "../chunk-YTW5MCYO.mjs";
4
+ } from "../chunk-JIOUNG7W.mjs";
5
+ import "../chunk-TVKKRH3O.mjs";
6
6
  import "../chunk-7RBGBF5M.mjs";
7
7
  export {
8
8
  CartContext,
@@ -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(item) {
56
+ addItem(newItem) {
57
57
  const cart = this.getCart();
58
- const exists = cart.find((item2) => item2.id === item2.id);
58
+ const exists = cart.find((item) => item.id === newItem.id);
59
59
  if (exists)
60
- exists.quantity = (exists.quantity || 1) + (item.quantity || 1);
61
- else cart.push({ ...item, quantity: item.quantity || 1 });
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) {
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  useCart
3
- } from "../chunk-K3ENNZKF.mjs";
3
+ } from "../chunk-JRM56HH5.mjs";
4
4
  import {
5
5
  CartProvider
6
- } from "../chunk-3AB6KTDB.mjs";
7
- import "../chunk-YTW5MCYO.mjs";
6
+ } from "../chunk-JIOUNG7W.mjs";
7
+ import "../chunk-TVKKRH3O.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-K3ENNZKF.mjs";
4
- import "../chunk-3AB6KTDB.mjs";
5
- import "../chunk-YTW5MCYO.mjs";
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.7",
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": {
@@ -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
- };