forked from SkiFilmReviews/snow-forecast-sfr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit-util.js
136 lines (125 loc) · 2.98 KB
/
unit-util.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
/**
* Utility module that helps us convert from/to metric/imperial
*/
UnitUtil = {
//Setup conversion variables as constants
TO_MPH: 1.609344,
TO_KPH: 0.62137,
TO_CM: 0.39370,
TO_IN: 2.54,
TO_FT: 3.2808399,
TO_M: 0.3048,
/**
* PUBLIC
* Converts mph to kph
* speed
* in mph
*
* returns: speed in kph, rounded up.
*/
speedToMetric: function(speed){
var kph = speed / this.TO_KPH;
return Math.round(kph);
},
/**
* PUBLIC
* Converts kph to mph
* speed
* in kph
*
* returns: speed in mph, rounded up.
*/
speedToImperial: function(speed){
var mph = speed / this.TO_MPH;
return this.roundTo(5, Math.round(mph));
},
/**
* PUBLIC
* Converts feet to metres
* distance
* in feet
*
* returns: metres, rounded up to nearest 50m.
*/
distanceToMetric: function(distance){
return this.roundTo(50, Math.round(distance*this.TO_M));
},
/**
* PUBLIC
* Converts metres to feet
* distance
* in metres
*
* returns: feet, rounded up to nearest 100ft.
*/
distanceToImperial: function(distance){
return this.roundTo(100,Math.round(distance*this.TO_FT));
},
/**
* PUBLIC
* Converts inches to cm
* volume
* in inches
*
* returns: cm, rounded up.
*/
volumeToMetric: function(volume){
return Math.round( (volume/this.TO_CM)*10 )/10;
},
/**
* PUBLIC
* Converts cm to inches
* volume
* in cm
*
* returns: inches, rounded up.
*/
volumeToImperial: function(volume){
return Math.round( (volume/this.TO_IN)*10 )/10;
},
/**
* PUBLIC
* Converts degrees fahrenheit to celcius
* temp
* temperature in degrees fahrenheit
*
* returns: temperature in degrees celcius rounded up.
*/
temperatureToMetric: function(temp){
var temperature = (temp-32)*5/9;
return this.roundTo(1, Math.round(temperature));
},
/**
* PUBLIC
* Converts degrees celcius to fahrenheit
* temp
* temperature in degrees celcius
*
* returns: temperature in fahrenheit rounded up.
*/
temperatureToImperial: function(temp){
var temperature = (temp*9)/5 +32;
return this.roundTo(1, Math.round(temperature));
},
/**
* Public (shouldn't be...)
* Copied code from stack overflow to round numbers to nearest multiple.
* Credit to Makrem Saleh from:
* https://stackoverflow.com/questions/3254047/round-number-up-to-the-nearest-multiple-of-3
*
* multiple
* Nearest number to round to
* value
*
* returns: number that has been rounded up/down to specified multiple.
*/
roundTo: function(multiple, value){
var rem = Math.round(value%multiple);
if (rem <= (multiple/2)) {
return Math.round(value-rem);
} else {
return Math.round(value+multiple-rem);
}
}
};
module.exports = UnitUtil;