aboutsummaryrefslogtreecommitdiff
path: root/modern/src/common
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-05-06 14:15:23 -0700
committerAnton Tananaev <anton@traccar.org>2022-05-06 14:15:23 -0700
commit3812292cc4413efff1358eb34cc5bccab9d1cbae (patch)
tree8aa26184ec58a7f231869d427a28f8e999fa2093 /modern/src/common
parent1b513d2cbf4dde335f9a2822beaaeeb171816c8d (diff)
downloadtrackermap-web-3812292cc4413efff1358eb34cc5bccab9d1cbae.tar.gz
trackermap-web-3812292cc4413efff1358eb34cc5bccab9d1cbae.tar.bz2
trackermap-web-3812292cc4413efff1358eb34cc5bccab9d1cbae.zip
Handle unit attributes
Diffstat (limited to 'modern/src/common')
-rw-r--r--modern/src/common/converter.js58
-rw-r--r--modern/src/common/formatter.js37
2 files changed, 59 insertions, 36 deletions
diff --git a/modern/src/common/converter.js b/modern/src/common/converter.js
index d45db5d6..e70473ca 100644
--- a/modern/src/common/converter.js
+++ b/modern/src/common/converter.js
@@ -10,6 +10,22 @@ const speedConverter = (unit) => {
}
};
+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':
@@ -22,10 +38,46 @@ const distanceConverter = (unit) => {
}
};
-export const speedFromKnots = (value, unit) => value * speedConverter(unit);
-
-export const speedToKnots = (value, unit) => value / speedConverter(unit);
+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);
diff --git a/modern/src/common/formatter.js b/modern/src/common/formatter.js
index bc3af6fb..06e3b12f 100644
--- a/modern/src/common/formatter.js
+++ b/modern/src/common/formatter.js
@@ -1,4 +1,5 @@
import moment from 'moment';
+import { distanceFromMeters, distanceUnitString, speedFromKnots, speedUnitString, volumeFromLiters, volumeUnitString } from './converter';
import { prefixString } from './stringUtils';
export const formatBoolean = (value, t) => (value ? t('sharedYes') : t('sharedNo'));
@@ -18,41 +19,11 @@ export const formatCourse = (value) => {
return courseValues[Math.floor(value / 45)];
};
-export const formatDistance = (value, unit, t) => {
- switch (unit) {
- case 'mi':
- return `${(value * 0.000621371).toFixed(2)} ${t('sharedMi')}`;
- case 'nmi':
- return `${(value * 0.000539957).toFixed(2)} ${t('sharedNmi')}`;
- case 'km':
- default:
- return `${(value * 0.001).toFixed(2)} ${t('sharedKm')}`;
- }
-};
+export const formatDistance = (value, unit, t) => `${distanceFromMeters(value, unit).toFixed(2)} ${distanceUnitString(unit, t)}`;
-export const formatSpeed = (value, unit, t) => {
- switch (unit) {
- case 'kmh':
- return `${(value * 1.852).toFixed(2)} ${t('sharedKmh')}`;
- case 'mph':
- return `${(value * 1.15078).toFixed(2)} ${t('sharedMph')}`;
- case 'kn':
- default:
- return `${(value * 1).toFixed(2)} ${t('sharedKn')}`;
- }
-};
+export const formatSpeed = (value, unit, t) => `${speedFromKnots(value, unit).toFixed(2)} ${speedUnitString(unit, t)}`;
-export const formatVolume = (value, unit, t) => {
- switch (unit) {
- case 'impGal':
- return `${(value / 4.546).toFixed(2)} ${t('sharedGallonAbbreviation')}`;
- case 'usGal':
- return `${(value / 3.785).toFixed(2)} ${t('sharedGallonAbbreviation')}`;
- case 'ltr':
- default:
- return `${(value / 1).toFixed(2)} ${t('sharedLiterAbbreviation')}`;
- }
-};
+export const formatVolume = (value, unit, t) => `${volumeFromLiters(value, unit).toFixed(2)} ${volumeUnitString(unit, t)}`;
export const formatHours = (value) => moment.duration(value).humanize();