aboutsummaryrefslogtreecommitdiff
path: root/modern/src/common/converter.js
blob: 26e2a82b5b5c9458d15479507d66f297182cc6a3 (plain)
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
const speedConverter = (value, unit, back = false) => {
  let factor;
  switch (unit) {
    case 'kmh':
      factor = 1.852;
      break;
    case 'mph':
      factor = 1.15078;
      break;
    case 'kn':
    default:
      factor = 1;
  }
  return back ? value / factor: value * factor;
};

const distanceConverter = (value, unit, back = false) => {
  let factor;
  switch (unit) {
    case 'mi':
      factor = 0.000621371;
      break;
    case 'nmi':
      factor = 0.000539957;
      break;
    case 'km':
    default:
      factor = 0.001;
  }
  return back ? value / factor : value * factor;  
}

export const speedFromKnots = (value, unit) => {
  return speedConverter(value, unit);
}

export const speedToKnots = (value, unit) => {
  return speedConverter(value, unit, true);
}

export const distanceFromMeters = (value, unit) => {
  return distanceConverter(value, unit);
}

export const distanceToMeters = (value, unit) => {
  return distanceConverter(value, unit, true);
}