@ravishranjan/cart 2.1.2 → 2.1.4
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
|
|
1
|
+
"use strict";var Cart=(()=>{var s=Object.defineProperty;var u=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var l=Object.prototype.hasOwnProperty;var d=(i,t)=>{for(var e in t)s(i,e,{get:t[e],enumerable:!0})},f=(i,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of c(t))!l.call(i,a)&&a!==e&&s(i,a,{get:()=>t[a],enumerable:!(r=u(t,a))||r.enumerable});return i};var m=i=>f(s({},"__esModule",{value:!0}),i);var p={};d(p,{default:()=>y});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)}},g=o;var y=g;return m(p);})();
|
|
@@ -0,0 +1,81 @@
|
|
|
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/core/cart.global.ts
|
|
21
|
+
var cart_global_exports = {};
|
|
22
|
+
__export(cart_global_exports, {
|
|
23
|
+
default: () => cart_global_default
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(cart_global_exports);
|
|
26
|
+
|
|
27
|
+
// src/core/storage.ts
|
|
28
|
+
function getStorage(type) {
|
|
29
|
+
if (typeof window == "undefined") throw new Error("No window object found");
|
|
30
|
+
return type == "localStorage" ? window.localStorage : window.sessionStorage;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// src/core/cart.ts
|
|
34
|
+
var Cart = class {
|
|
35
|
+
constructor({
|
|
36
|
+
storage = "localStorage",
|
|
37
|
+
key = "cart"
|
|
38
|
+
} = {}) {
|
|
39
|
+
this.storage = getStorage(storage);
|
|
40
|
+
this.key = key;
|
|
41
|
+
}
|
|
42
|
+
getCart() {
|
|
43
|
+
const data = this.storage.getItem(this.key);
|
|
44
|
+
return data ? JSON.parse(data) : [];
|
|
45
|
+
}
|
|
46
|
+
save(cart) {
|
|
47
|
+
this.storage.setItem(this.key, JSON.stringify(cart));
|
|
48
|
+
}
|
|
49
|
+
addItem(newItem) {
|
|
50
|
+
const cart = this.getCart();
|
|
51
|
+
const exists = cart.find((item) => item.id === newItem.id);
|
|
52
|
+
if (exists)
|
|
53
|
+
exists.quantity = (exists.quantity || 1) + (newItem.quantity || 1);
|
|
54
|
+
else cart.push({ ...newItem, quantity: newItem.quantity || 1 });
|
|
55
|
+
this.save(cart);
|
|
56
|
+
}
|
|
57
|
+
removeItem(id) {
|
|
58
|
+
this.save(this.getCart().filter((item) => item.id !== id));
|
|
59
|
+
}
|
|
60
|
+
clear() {
|
|
61
|
+
this.save([]);
|
|
62
|
+
}
|
|
63
|
+
getItems(category) {
|
|
64
|
+
const items = this.getCart();
|
|
65
|
+
return category ? items.filter((item) => item.category === category) : items;
|
|
66
|
+
}
|
|
67
|
+
getTotal(category) {
|
|
68
|
+
let items = this.getCart();
|
|
69
|
+
if (category) {
|
|
70
|
+
items = items.filter((item) => item.category === category);
|
|
71
|
+
}
|
|
72
|
+
return items.reduce(
|
|
73
|
+
(sum, item) => sum + (item.price || 0) * (item.quantity || 1),
|
|
74
|
+
0
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
var cart_default = Cart;
|
|
79
|
+
|
|
80
|
+
// src/core/cart.global.ts
|
|
81
|
+
var cart_global_default = cart_default;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ravishranjan/cart",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.4",
|
|
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 --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": [
|