-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathhandleInputs.js
140 lines (125 loc) · 4.55 KB
/
handleInputs.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//takes in a request and outputs the updated instance
//todo: move property system into plugins.
function getHandlers(handlerArray){
if(!handlerArray){
console.error("Handlers not provided..");
}
return handlerArray.reduce((acc, handler) => {
acc[handler.name] = handler;
return acc;
}, {});
};
function toCents (amount) {
if (typeof amount !== 'string' && typeof amount !== 'number') {
throw new Error('Amount passed must be of type String or Number.')
}
return Math.round(100 * parseFloat(typeof amount === 'string' ? amount.replace(/[$,]/g, '') : amount))
}
function getPriceAdjustments(properties, handlers) {
if(properties) {
return properties.reduce((acc, prop) => {
if (
handlers[prop.type] &&
handlers[prop.type].priceHandler &&
prop.config &&
prop.config.pricing &&
prop.config.pricing.operation
) {
const adjuster = handlers[prop.type].priceHandler;
let valToPUsh = {
name: prop.name,
type: prop.type,
operation: prop.config.pricing.operation,
value: adjuster(prop.data, prop.config) || 0
}
acc.push(valToPUsh);
}
return acc;
}, [])
}else{
return [];
}
};
module.exports = {
getBasePrice(properties, handlers, currentPrice, cents=false){
let adjustments = [];
try {
adjustments = getPriceAdjustments(properties, handlers);
}catch(e){
console.error("price error", e);
}
let additions = 0;
let multiplication = 1;
for(let adjustment of adjustments) {
let operation = adjustment.operation;
switch (operation) {
case "add" :
additions += cents ? toCents(adjustment.value) : adjustment.value;
break;
case "subtract" :
additions -= cents ? toCents(adjustment.value) : adjustment.value;
break;
case "multiply" :
multiplication += (adjustment.value / 100)
break;
case "divide" :
multiplication -= (adjustment.value / 100)
break;
default :
throw "Bad operation : " + operation
}
}
return (currentPrice - additions)/multiplication;
},
getPrice : function(properties, handlers, basePrice, cents=false){
let adjustments = [];
try {
adjustments = getPriceAdjustments(properties, handlers);
}catch(e){
console.error("price error", e);
}
return (basePrice + adjustments.reduce((acc, adjustment) => {
let operation = adjustment.operation;
if(adjustment.value === null || adjustment.value === undefined){
return acc;
}
switch (operation) {
case "add" :
acc += cents ? toCents(adjustment.value) : adjustment.value;
break;
case "subtract" :
acc -= cents ? toCents(adjustment.value) : adjustment.value;
break;
case "multiply" :
acc += (basePrice * (adjustment.value / 100));
break;
case "divide" :
acc -= (basePrice * (adjustment.value / 100));
break;
default :
throw "Bad operation : " + operation
}
return acc;
}, 0));
},
validateProperties: function (properties, handlers) {
// let handlers = getHandlers(handlerArray);
return properties.reduce((acc, prop) => {
//todo: reduce duplicate code that exists here and in webpack code.
if (!handlers[prop.type]) {
return acc;
}
const validator = handlers[prop.type].validator;
if (validator) {
const validationResult = validator(prop.data, prop.config)
if (validationResult != true) {
prop.error = validationResult;
acc.push(prop);
}
}
return acc;
}, [])
},
getPriceAdjustments,
toCents
};