-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct.js
executable file
·120 lines (102 loc) · 3.9 KB
/
product.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Product Class
* Author: Karim Saad
* Month/Year: 08/2019
*/
const PDefaults = {
name: null,
baseprice: null,
currency: null,
weight: " ",
unitweight: null
}
const replaceString = require('replace-string');
const hashcode = require("./hashcode.js");
/*
Class of a Product Object
Parses the data from a html file & gives the basic structure for a product e.g. baseprice etc.
*/
class Product {
constructor(name, weight, baseprice, currency, unitweight){
this.name = name || PDefaults.name ;
this.baseprice = baseprice || PDefaults.baseprice ;
this.currency = currency || PDefaults.currency;
this.weight = weight || " 500g ";
this.unitweight = unitweight || PDefaults.unitweight;
this.namehash = hashcode(name);
this.unitweight = this.detectUnitWeight(unitweight);
this.weight = this.detectWeight(weight);
this.currency = this.detectCurrency(currency);
if(this.baseprice === null){
this.baseprice = Number.parseFloat(currency.split("EUR")[0].replace(",", "."));
}
}
toString(){
return this.name;
}
calcRealPrice(){
if(this.weight == null)return this.baseprice;
return (this.weight*this.baseprice);
}
detectWeight(str){
if(str === undefined)return;
var sta=null;
var strs=str.split(" ");
var spli = 0
var thus = this;
strs.forEach(function (element) {
if(element != undefined) {
spli+=1
if(thus.howOftenInString(element, 'ST') == 1 && thus.isLastCharAndBeforeNumber(element, 'ST')) {
sta = Number(element.split("ST")[0]);
}
if(thus.howOftenInString(element, 'l') == 1 && thus.isLastCharAndBeforeNumber(element, 'l')) {
sta = Number(element.split("l")[0]);
}
if(thus.howOftenInString(element,'g') == 1 && thus.isLastCharAndBeforeNumber(element, 'g')) {
// Mostly 250g, 500g etc.
sta = Number(element.split("g")[0]);
}
if(element == "g") {
// Mostly 250g, 500g etc.
console.log(strs[spli-2])
sta = Number(strs[spli-2]);
}
}
});
return sta;
}
detectUnitWeight(str){
var unitweight = "";
unitweight = str.split("/");
if(unitweight !== undefined && unitweight !== null){
unitweight = unitweight[1]
if(unitweight === undefined || unitweight === null) return null;
if(unitweight.includes("kg"))return "kg";
if(unitweight.includes('Liter'))return 'Liter';
if(unitweight.includes("g"))return "kg"; // Later around calculated
}
return unitweight;
}
detectCurrency(str){
if (str.includes('EUR'))return "EUR";
return null;
}
isString(str){
return (typeof myVar === str || str instanceof String);
}
isLastCharAndBeforeNumber = function (str, k){
str=replaceString(str, ' ', '');
return (str[str.length-1] == k && Number(str[str.length-2]) >= 0);
}
howOftenInString = function (str, k){
if(str === null || k === null)return 0;
var i=0;
str = str.split("");
str.forEach(function (el) {
if(el == k)i++;
});
return i;
}
}
module.exports = Product