-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertor.js
48 lines (39 loc) · 864 Bytes
/
convertor.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
/*
Convertor Kata
https://www.codewars.com/kata/596fd97f65ad2fc072000037/train/javascript
*/
class Converter {
constructor(number, unit) {
this.number = number;
this.unit = unit;
this.units = ['B', 'KB', 'MB', 'GB', 'TB'];
}
get size() {
const formatted_num = Math.floor(this.number * 1000)/1000;
return `${formatted_num} ${this.unit}`;
}
unit() {
return this.unit;
}
toB() {
this.convertor('B')
}
toKB() {
this.convertor('KB')
}
toMB() {
this.convertor('MB')
}
toGB() {
this.convertor('GB')
}
toTB() {
this.convertor('TB')
}
convertor(target){
const current_index = this.units.indexOf(this.unit);
const target_index = this.units.indexOf(target);
this.unit = target;
this.number = this.number * 2 ** - (10*(target_index - current_index));
}
}