@ravishranjan/cart 2.1.3 → 2.1.5

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 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);})();
1
+ "use strict";var Cart=(()=>{var o=(i,t)=>()=>(i&&(t=i(i=0)),t);var y=(i,t)=>()=>(t||i((t={exports:{}}).exports,t),t.exports);function n(i){if(typeof window>"u")throw new Error("No window object found");return i=="localStorage"?window.localStorage:window.sessionStorage}var g=o(()=>{"use strict"});var s,c,u=o(()=>{"use strict";g();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)}},c=s});var d=y((v,l)=>{u();l.exports=c});return d();})();
@@ -0,0 +1,15 @@
1
+ import {
2
+ CartContext
3
+ } from "./chunk-NQCCPA7E.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,24 @@
1
+ var __getOwnPropNames = Object.getOwnPropertyNames;
2
+ var __esm = (fn, res) => function __init() {
3
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
4
+ };
5
+ var __commonJS = (cb, mod) => function __require() {
6
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
7
+ };
8
+
9
+ // src/core/storage.ts
10
+ function getStorage(type) {
11
+ if (typeof window == "undefined") throw new Error("No window object found");
12
+ return type == "localStorage" ? window.localStorage : window.sessionStorage;
13
+ }
14
+ var init_storage = __esm({
15
+ "src/core/storage.ts"() {
16
+ }
17
+ });
18
+
19
+ export {
20
+ __esm,
21
+ __commonJS,
22
+ getStorage,
23
+ init_storage
24
+ };
@@ -0,0 +1,49 @@
1
+ import {
2
+ Cart,
3
+ init_cart
4
+ } from "./chunk-R4MVVWRS.mjs";
5
+
6
+ // src/react/CartContext.tsx
7
+ init_cart();
8
+ import { createContext, useEffect, useState } from "react";
9
+ import { jsx } from "react/jsx-runtime";
10
+ var CartContext = createContext(null);
11
+ var CartProvider = ({
12
+ children,
13
+ storage = "localStorage",
14
+ key = "cart"
15
+ }) => {
16
+ const cartInstance = new Cart({ key, storage });
17
+ const [items, setItems] = useState(cartInstance.getItems());
18
+ const sync = () => setItems(cartInstance.getItems());
19
+ const api = {
20
+ cart: items,
21
+ addItem: (item) => {
22
+ cartInstance.addItem(item);
23
+ sync();
24
+ },
25
+ removeItem: (id) => {
26
+ cartInstance.removeItem(id);
27
+ sync();
28
+ },
29
+ getItems: (category) => {
30
+ cartInstance.getItems(category);
31
+ },
32
+ clear: () => {
33
+ cartInstance.clear();
34
+ sync();
35
+ },
36
+ total: cartInstance.getTotal()
37
+ };
38
+ useEffect(() => {
39
+ const handleSync = () => sync();
40
+ window.addEventListener("storage", handleSync);
41
+ return () => window.removeEventListener("storage", handleSync);
42
+ }, []);
43
+ return /* @__PURE__ */ jsx(CartContext.Provider, { value: api, children });
44
+ };
45
+
46
+ export {
47
+ CartContext,
48
+ CartProvider
49
+ };
@@ -0,0 +1,64 @@
1
+ import {
2
+ __esm,
3
+ getStorage,
4
+ init_storage
5
+ } from "./chunk-IDMIQXPW.mjs";
6
+
7
+ // src/core/cart.ts
8
+ var Cart, cart_default;
9
+ var init_cart = __esm({
10
+ "src/core/cart.ts"() {
11
+ init_storage();
12
+ Cart = class {
13
+ constructor({
14
+ storage = "localStorage",
15
+ key = "cart"
16
+ } = {}) {
17
+ this.storage = getStorage(storage);
18
+ this.key = key;
19
+ }
20
+ getCart() {
21
+ const data = this.storage.getItem(this.key);
22
+ return data ? JSON.parse(data) : [];
23
+ }
24
+ save(cart) {
25
+ this.storage.setItem(this.key, JSON.stringify(cart));
26
+ }
27
+ addItem(newItem) {
28
+ const cart = this.getCart();
29
+ const exists = cart.find((item) => item.id === newItem.id);
30
+ if (exists)
31
+ exists.quantity = (exists.quantity || 1) + (newItem.quantity || 1);
32
+ else cart.push({ ...newItem, quantity: newItem.quantity || 1 });
33
+ this.save(cart);
34
+ }
35
+ removeItem(id) {
36
+ this.save(this.getCart().filter((item) => item.id !== id));
37
+ }
38
+ clear() {
39
+ this.save([]);
40
+ }
41
+ getItems(category) {
42
+ const items = this.getCart();
43
+ return category ? items.filter((item) => item.category === category) : items;
44
+ }
45
+ getTotal(category) {
46
+ let items = this.getCart();
47
+ if (category) {
48
+ items = items.filter((item) => item.category === category);
49
+ }
50
+ return items.reduce(
51
+ (sum, item) => sum + (item.price || 0) * (item.quantity || 1),
52
+ 0
53
+ );
54
+ }
55
+ };
56
+ cart_default = Cart;
57
+ }
58
+ });
59
+
60
+ export {
61
+ Cart,
62
+ cart_default,
63
+ init_cart
64
+ };
@@ -0,0 +1,5 @@
1
+ import Cart from './cart.mjs';
2
+
3
+
4
+
5
+ export { Cart as default };
@@ -0,0 +1,5 @@
1
+ import Cart from './cart.js';
2
+
3
+
4
+
5
+ export { Cart as default };
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+
3
+ // src/core/storage.ts
4
+ function getStorage(type) {
5
+ if (typeof window == "undefined") throw new Error("No window object found");
6
+ return type == "localStorage" ? window.localStorage : window.sessionStorage;
7
+ }
8
+
9
+ // src/core/cart.ts
10
+ var Cart = class {
11
+ constructor({
12
+ storage = "localStorage",
13
+ key = "cart"
14
+ } = {}) {
15
+ this.storage = getStorage(storage);
16
+ this.key = key;
17
+ }
18
+ getCart() {
19
+ const data = this.storage.getItem(this.key);
20
+ return data ? JSON.parse(data) : [];
21
+ }
22
+ save(cart) {
23
+ this.storage.setItem(this.key, JSON.stringify(cart));
24
+ }
25
+ addItem(newItem) {
26
+ const cart = this.getCart();
27
+ const exists = cart.find((item) => item.id === newItem.id);
28
+ if (exists)
29
+ exists.quantity = (exists.quantity || 1) + (newItem.quantity || 1);
30
+ else cart.push({ ...newItem, quantity: newItem.quantity || 1 });
31
+ this.save(cart);
32
+ }
33
+ removeItem(id) {
34
+ this.save(this.getCart().filter((item) => item.id !== id));
35
+ }
36
+ clear() {
37
+ this.save([]);
38
+ }
39
+ getItems(category) {
40
+ const items = this.getCart();
41
+ return category ? items.filter((item) => item.category === category) : items;
42
+ }
43
+ getTotal(category) {
44
+ let items = this.getCart();
45
+ if (category) {
46
+ items = items.filter((item) => item.category === category);
47
+ }
48
+ return items.reduce(
49
+ (sum, item) => sum + (item.price || 0) * (item.quantity || 1),
50
+ 0
51
+ );
52
+ }
53
+ };
54
+ var cart_default = Cart;
55
+
56
+ // src/core/cart.global.ts
57
+ module.exports = cart_default;
@@ -0,0 +1,16 @@
1
+ import {
2
+ cart_default,
3
+ init_cart
4
+ } from "../chunk-R4MVVWRS.mjs";
5
+ import {
6
+ __commonJS
7
+ } from "../chunk-IDMIQXPW.mjs";
8
+
9
+ // src/core/cart.global.ts
10
+ var require_cart_global = __commonJS({
11
+ "src/core/cart.global.ts"(exports, module) {
12
+ init_cart();
13
+ module.exports = cart_default;
14
+ }
15
+ });
16
+ export default require_cart_global();
@@ -1,8 +1,10 @@
1
1
  import {
2
2
  Cart,
3
- cart_default
4
- } from "../chunk-ZA6BU4XH.mjs";
5
- import "../chunk-7RBGBF5M.mjs";
3
+ cart_default,
4
+ init_cart
5
+ } from "../chunk-R4MVVWRS.mjs";
6
+ import "../chunk-IDMIQXPW.mjs";
7
+ init_cart();
6
8
  export {
7
9
  Cart,
8
10
  cart_default as default
@@ -1,6 +1,8 @@
1
1
  import {
2
- getStorage
3
- } from "../chunk-7RBGBF5M.mjs";
2
+ getStorage,
3
+ init_storage
4
+ } from "../chunk-IDMIQXPW.mjs";
5
+ init_storage();
4
6
  export {
5
7
  getStorage
6
8
  };
package/dist/index.mjs CHANGED
@@ -1,9 +1,15 @@
1
1
  import {
2
- Cart
3
- } from "./chunk-ZA6BU4XH.mjs";
2
+ Cart,
3
+ init_cart
4
+ } from "./chunk-R4MVVWRS.mjs";
4
5
  import {
5
- getStorage
6
- } from "./chunk-7RBGBF5M.mjs";
6
+ getStorage,
7
+ init_storage
8
+ } from "./chunk-IDMIQXPW.mjs";
9
+
10
+ // src/index.ts
11
+ init_cart();
12
+ init_storage();
7
13
  export {
8
14
  Cart,
9
15
  getStorage
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  CartContext,
3
3
  CartProvider
4
- } from "../chunk-RBWMJEAD.mjs";
5
- import "../chunk-ZA6BU4XH.mjs";
6
- import "../chunk-7RBGBF5M.mjs";
4
+ } from "../chunk-NQCCPA7E.mjs";
5
+ import "../chunk-R4MVVWRS.mjs";
6
+ import "../chunk-IDMIQXPW.mjs";
7
7
  export {
8
8
  CartContext,
9
9
  CartProvider
@@ -1,11 +1,11 @@
1
1
  import {
2
2
  useCart
3
- } from "../chunk-BCVOXMCT.mjs";
3
+ } from "../chunk-2EP5A44J.mjs";
4
4
  import {
5
5
  CartProvider
6
- } from "../chunk-RBWMJEAD.mjs";
7
- import "../chunk-ZA6BU4XH.mjs";
8
- import "../chunk-7RBGBF5M.mjs";
6
+ } from "../chunk-NQCCPA7E.mjs";
7
+ import "../chunk-R4MVVWRS.mjs";
8
+ import "../chunk-IDMIQXPW.mjs";
9
9
  export {
10
10
  CartProvider,
11
11
  useCart
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  useCart
3
- } from "../chunk-BCVOXMCT.mjs";
4
- import "../chunk-RBWMJEAD.mjs";
5
- import "../chunk-ZA6BU4XH.mjs";
6
- import "../chunk-7RBGBF5M.mjs";
3
+ } from "../chunk-2EP5A44J.mjs";
4
+ import "../chunk-NQCCPA7E.mjs";
5
+ import "../chunk-R4MVVWRS.mjs";
6
+ import "../chunk-IDMIQXPW.mjs";
7
7
  export {
8
8
  useCart
9
9
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ravishranjan/cart",
3
- "version": "2.1.3",
3
+ "version": "2.1.5",
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",
@@ -11,7 +11,7 @@
11
11
  "scripts": {
12
12
  "build": "tsup src --format esm,cjs --dts",
13
13
  "dev": "tsup src --watch",
14
- "build:cdn": "esbuild src/core/cart.ts --bundle --minify --format=iife --global-name=Cart --platform=browser --outfile=dist/cart.min.js",
14
+ "build:cdn": "esbuild src/core/cart.global.ts --bundle --minify --format=iife --global-name=Cart --platform=browser --outfile=dist/cart.min.js",
15
15
  "prepare": "npm run build && npm run build:cdn"
16
16
  },
17
17
  "keywords": [