aboutsummaryrefslogtreecommitdiff
path: root/modern/src/common/util/converter.js
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-05-08 11:48:09 -0700
committerAnton Tananaev <anton@traccar.org>2022-05-08 11:48:09 -0700
commit2352071211b61c10fa5bf5736baaff7809d18bf0 (patch)
tree743e4adc1cc35fb3585b912daaa8719ae5757f60 /modern/src/common/util/converter.js
parent044733ff543156d76437daae8edb66850d785ac9 (diff)
downloadtrackermap-web-2352071211b61c10fa5bf5736baaff7809d18bf0.tar.gz
trackermap-web-2352071211b61c10fa5bf5736baaff7809d18bf0.tar.bz2
trackermap-web-2352071211b61c10fa5bf5736baaff7809d18bf0.zip
Organize common code
Diffstat (limited to 'modern/src/common/util/converter.js')
-rw-r--r--modern/src/common/util/converter.js83
1 files changed, 83 insertions, 0 deletions
diff --git a/modern/src/common/util/converter.js b/modern/src/common/util/converter.js
new file mode 100644
index 00000000..61e2dfe6
--- /dev/null
+++ b/modern/src/common/util/converter.js
@@ -0,0 +1,83 @@
+const speedConverter = (unit) => {
+ switch (unit) {
+ case 'kmh':
+ return 1.852;
+ case 'mph':
+ return 1.15078;
+ case 'kn':
+ default:
+ return 1;
+ }
+};
+
+export const speedUnitString = (unit, t) => {
+ switch (unit) {
+ case 'kmh':
+ return t('sharedKmh');
+ case 'mph':
+ return t('sharedMph');
+ case 'kn':
+ default:
+ return t('sharedKn');
+ }
+};
+
+export const speedFromKnots = (value, unit) => value * speedConverter(unit);
+
+export const speedToKnots = (value, unit) => value / speedConverter(unit);
+
+const distanceConverter = (unit) => {
+ switch (unit) {
+ case 'mi':
+ return 0.000621371;
+ case 'nmi':
+ return 0.000539957;
+ case 'km':
+ default:
+ return 0.001;
+ }
+};
+
+export const distanceUnitString = (unit, t) => {
+ switch (unit) {
+ case 'mi':
+ return t('sharedMi');
+ case 'nmi':
+ return t('sharedNmi');
+ case 'km':
+ default:
+ return t('sharedKm');
+ }
+};
+
+export const distanceFromMeters = (value, unit) => value * distanceConverter(unit);
+
+export const distanceToMeters = (value, unit) => value / distanceConverter(unit);
+
+const volumeConverter = (unit) => {
+ switch (unit) {
+ case 'impGal':
+ return 4.546;
+ case 'usGal':
+ return 3.785;
+ case 'ltr':
+ default:
+ return 1;
+ }
+};
+
+export const volumeUnitString = (value, unit, t) => {
+ switch (unit) {
+ case 'impGal':
+ return t('sharedGallonAbbreviation');
+ case 'usGal':
+ return t('sharedGallonAbbreviation');
+ case 'ltr':
+ default:
+ return t('sharedLiterAbbreviation');
+ }
+};
+
+export const volumeFromLiters = (value, unit) => value / volumeConverter(unit);
+
+export const volumeToLiters = (value, unit) => value * volumeConverter(unit);