-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshop-helpers.js
More file actions
78 lines (60 loc) · 1.39 KB
/
shop-helpers.js
File metadata and controls
78 lines (60 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class ShopHelpers {
constructor() {
this.storage = window.sessionStorage;
}
_emptyCart() {
this.storage.clear();
}
_formatNumber(num, places) {
return num.toFixed(places);
}
_extractPrice(element) {
const text = element.text();
const price = text.replace( this.currencyString, "").replace(" ", "");
return price;
}
_convertString(numStr) {
let num;
if( /^[-+]?[0-9]+\.[0-9]+$/.test( numStr ) ) {
num = parseFloat( numStr );
} else if( /^\d+$/.test( numStr ) ) {
num = parseInt( numStr, 10 );
} else {
num = Number( numStr );
}
return !isNaN(num) ? num : false;
}
_convertNumber(n) {
return n.toString();
}
_toJSONObject(str) {
return JSON.parse(str);
}
_toJSONString(obj) {
return JSON.stringify(obj);
}
_addToCart(values) {
const cart = this.storage.getItem(this.cartName);
const cartObject = this._toJSONObject(cart);
const cartCopy = cartObject;
const items = cartCopy.items;
items.push(values);
this.storage.setItem(this.cartName, this._toJSONString(cartCopy));
}
_calculateShipping(qty) {
let shipping = 0;
if( qty >= 6 ) {
shipping = 10;
}
if( qty >= 12 && qty <= 30 ) {
shipping = 20;
}
if( qty >= 30 && qty <= 60 ) {
shipping = 30;
}
if( qty > 60 ) {
shipping = 0;
}
return shipping;
}
}