-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit-converter.module.js
More file actions
54 lines (53 loc) · 1.29 KB
/
Copy pathunit-converter.module.js
File metadata and controls
54 lines (53 loc) · 1.29 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
class Converter {
calculate(value) {
return parseInt(value) * this.ratio
}
map(array) {
return array.map((item) => {
if (Array.isArray(item)) {
return this.map(item);
}
let result = this.calculate(item);
if (typeof item == "string") return result.toString();
return result;
});
}
convert(...num) {
if (!num.length) return;
let result = this.map(num);
if (result.length == 1) return result[0];
return result;
}
}
class InchInCm extends Converter {
constructor() {
super();
this.ratio = 2.54;
}
}
class FInC extends Converter {
constructor() {
super();
this.ratio = 5/9;
this.substract = 32;
}
calculate(value) {
return (parseInt(value) - this.substract) * this.ratio;
}
}
class RadInGrad extends Converter {
constructor() {
super();
this.ratio = 180 / Math.PI;
}
}
export function createConverter(obj) {
let {from, to} = obj;
if (from === "cm" && to === "mm") {
return new InchInCm(obj);
}
if (from === "F" && to === "C") {
return new FInC(obj);
}
return new RadInGrad(obj);
}