-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertHandler.js
162 lines (121 loc) · 3.39 KB
/
convertHandler.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
*
*
* Complete the handler logic below
*
*
*/
function ConvertHandler() {
this.getNum = function(input) {
// const numberRegex = /([0-9\/\.]*)(lbs?|kgs?|gals?|Ls?|mis?|kms?)*$/
// const numberRegex = /([0-9\/\.]*)(.*)$/
// const numberRegex = /([0-9\/\.]*)([A-Za-z]*)$/
const numberRegex = /^([0-9\.]+\/?[0-9\.]+|[0-9\.]*)([A-Za-z]*)$/
const numberMatch = input.match(numberRegex)
// console.log('this is numberMatch: ', numberMatch)
if (input.match(/^(lbs?|kgs?|gals?|Ls?|mis?|kms?)*$/)) {
return 1
}
if (!numberMatch) {
return undefined
}
return eval(numberMatch[1]);
};
this.getUnit = function(input) {
const unitRegex = /(lbs?|kgs?|gals?|Ls?|mis?|kms?)*$/i
const unitMatch = input.match(unitRegex)
// console.log('this is unitMatch: ', unitMatch)
if (!unitMatch || !unitMatch[0]) {
return undefined
}
return unitMatch[0];
};
this.stripPlural = function(initUnit) {
if (initUnit.substr(initUnit.length - 1, 1) === 's') {
return initUnit.substring(0, initUnit.length - 1)
}
return initUnit
}
this.getReturnUnit = function(initUnit) {
initUnit = this.stripPlural(initUnit).toLowerCase()
switch(initUnit) {
case "lb":
return "kg";
break;
case "kg":
return "lbs";
break;
case "gal":
return "l";
break;
case "l":
return "gal";
break;
case "mi":
return "km";
break;
case "km":
return "mi";
break;
default:
return undefined
}
};
this.spellOutUnit = function(unit) {
unit = this.stripPlural(unit).toLowerCase()
switch(unit) {
case "lb":
return "pound(s)";
break;
case "kg":
return "kilogram(s)";
break;
case "gal":
return "gallon(s)";
break;
case "l":
return "liter(s)";
break;
case "mi":
return "mile(s)";
break;
case "km":
return "kilometer(s)";
break;
default:
return undefined
}
};
this.convert = function(initNum, initUnit) {
const galToL = 3.78541; // 1 gal is 3.78 L
const lbsToKg = 0.453592; // 1 lb is .45 kilos
const miToKm = 1.60934; // 1 mi is 1.6 km
initUnit = this.stripPlural(initUnit)
switch (initUnit) {
case "lb":
return parseFloat((initNum * lbsToKg).toFixed(5));
break;
case "kg":
return parseFloat((initNum / lbsToKg).toFixed(5));
break;
case "gal":
return parseFloat((initNum * galToL).toFixed(5));
break;
case "L":
return parseFloat((initNum / galToL).toFixed(5));
break;
case "mi":
return parseFloat((initNum * miToKm).toFixed(5));
break;
case "km":
return parseFloat((initNum / miToKm).toFixed(5));
break;
default:
return undefined
}
};
this.getString = function(initNum, initUnit, returnNum, returnUnit) {
return initNum + ' ' + this.spellOutUnit(initUnit) + ' converts to ' + returnNum + ' ' + this.spellOutUnit(returnUnit)
};
}
module.exports = ConvertHandler;