aboutsummaryrefslogtreecommitdiff
path: root/modern/src/common/util/formatter.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/formatter.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/formatter.js')
-rw-r--r--modern/src/common/util/formatter.js81
1 files changed, 81 insertions, 0 deletions
diff --git a/modern/src/common/util/formatter.js b/modern/src/common/util/formatter.js
new file mode 100644
index 00000000..08e29bc4
--- /dev/null
+++ b/modern/src/common/util/formatter.js
@@ -0,0 +1,81 @@
+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'));
+
+export const formatNumber = (value, precision = 1) => Number(value.toFixed(precision));
+
+export const formatPercentage = (value) => `${value}%`;
+
+export const formatDate = (value, format = 'YYYY-MM-DD HH:mm') => moment(value).format(format);
+export const formatTime = (value, format = 'YYYY-MM-DD HH:mm:ss') => moment(value).format(format);
+
+export const formatStatus = (value, t) => t(prefixString('deviceStatus', value));
+export const formatAlarm = (value, t) => t(prefixString('alarm', value));
+
+export const formatCourse = (value) => {
+ const courseValues = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'];
+ return courseValues[Math.floor(value / 45)];
+};
+
+export const formatDistance = (value, unit, t) => `${distanceFromMeters(value, unit).toFixed(2)} ${distanceUnitString(unit, t)}`;
+
+export const formatSpeed = (value, unit, t) => `${speedFromKnots(value, unit).toFixed(2)} ${speedUnitString(unit, t)}`;
+
+export const formatVolume = (value, unit, t) => `${volumeFromLiters(value, unit).toFixed(2)} ${volumeUnitString(unit, t)}`;
+
+export const formatHours = (value) => moment.duration(value).humanize();
+
+export const formatCoordinate = (key, value, unit) => {
+ let hemisphere;
+ let degrees;
+ let minutes;
+ let seconds;
+
+ if (key === 'latitude') {
+ hemisphere = value >= 0 ? 'N' : 'S';
+ } else {
+ hemisphere = value >= 0 ? 'E' : 'W';
+ }
+
+ switch (unit) {
+ case 'ddm':
+ value = Math.abs(value);
+ degrees = Math.floor(value);
+ minutes = (value - degrees) * 60;
+ return `${degrees}° ${minutes.toFixed(6)}' ${hemisphere}`;
+ case 'dms':
+ value = Math.abs(value);
+ degrees = Math.floor(value);
+ minutes = Math.floor((value - degrees) * 60);
+ seconds = Math.round((value - degrees - minutes / 60) * 3600);
+ return `${degrees}° ${minutes}' ${seconds}" ${hemisphere}`;
+ default:
+ return `${value.toFixed(6)}°`;
+ }
+};
+
+export const getStatusColor = (status) => {
+ switch (status) {
+ case 'online':
+ return 'positive';
+ case 'offline':
+ return 'negative';
+ case 'unknown':
+ default:
+ return 'neutral';
+ }
+};
+
+export const getBatteryStatus = (batteryLevel) => {
+ if (batteryLevel >= 70) {
+ return 'positive';
+ }
+ if (batteryLevel > 30) {
+ return 'medium';
+ }
+ return 'negative';
+};