forked from googlemaps/fleet-debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.js
More file actions
162 lines (138 loc) · 4.52 KB
/
Utils.js
File metadata and controls
162 lines (138 loc) · 4.52 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// src/Utils.js
import { toast } from "react-toastify";
class Utils {
// If undefined defaults to false
static isDebugEnabled = localStorage.getItem("debug") === "true";
/*
* Formats a duration into something friendly
* for human consumption.
*/
static formatDuration(duration) {
let sec_num = duration / 1000;
let hours = Math.floor(sec_num / 3600);
let minutes = Math.floor((sec_num - hours * 3600) / 60);
let seconds = Math.floor(sec_num - hours * 3600 - minutes * 60);
let timeStr = "";
if (hours > 0) {
timeStr += hours + " hours ";
}
if (minutes > 0) {
timeStr += minutes + " minutes ";
}
if (seconds > 0) {
timeStr += seconds + " seconds";
}
return timeStr;
}
/**
* Formats a duration for TTL warnings.
* > 1 day: "X days"
* 1h - 24h: "X hours"
* 1m - 60m: "X minutes"
* < 1m: "X seconds"
*/
static formatTTLRemaining(ms) {
const days = ms / (1000 * 60 * 60 * 24);
const hours = ms / (1000 * 60 * 60);
const minutes = ms / (1000 * 60);
const seconds = ms / 1000;
if (days >= 1) {
const d = Math.ceil(days);
return `${d} day${d > 1 ? "s" : ""}`;
} else if (hours >= 1) {
const h = Math.ceil(hours);
return `${h} hour${h > 1 ? "s" : ""}`;
} else if (minutes >= 1) {
const m = Math.ceil(minutes);
return `${m} minute${m > 1 ? "s" : ""}`;
} else {
const s = Math.ceil(seconds);
return `${s} second${s !== 1 ? "s" : ""}`;
}
}
}
/**
* Pads a partial UTC timestamp string to the full HH:mm:ss format.
* Treats the input as a literal UTC string with no timezone conversion.
* @param {string} timeString The user-provided time string (e.g., "2025-09-06T" or "2025-09-06T14:30").
* @returns {string} The fully padded string (e.g., "2025-09-06T00:00:00" or "2025-09-06T14:30:00").
*/
export function padUtcTimestamp(timeString) {
if (!timeString) {
return timeString;
}
const [datePart, timePart] = timeString.split("T");
// If there's no 'T' or the date part is missing, it's not a format we can pad.
if (timePart === undefined || !datePart) {
return timeString;
}
const timeSegments = timePart.split(":").filter(Boolean); // filter(Boolean) handles the "T" with nothing after it.
while (timeSegments.length < 3) {
timeSegments.push("00");
}
const paddedTimePart = timeSegments.join(":");
return `${datePart}T${paddedTimePart}`;
}
window.debug = {
enable: () => {
Utils.isDebugEnabled = true;
localStorage.setItem("debug", "true");
console.log("Debug enabled");
},
disable: () => {
Utils.isDebugEnabled = false;
localStorage.setItem("debug", "false");
console.log("Debug disabled");
},
status: () => {
console.log("Debug is", Utils.isDebugEnabled ? "enabled" : "disabled");
return Utils.isDebugEnabled;
},
};
// Export the log function directly
export const log = (...args) => {
const validToastTypes = ["info", "success", "warn", "error"];
const lastArg = args[args.length - 1];
let toastType = null;
// First, check for an explicit toast type (e.g., 'info', 'warn')
if (typeof lastArg === "string" && validToastTypes.includes(lastArg)) {
toastType = args.pop();
}
// If no explicit type, infer 'error' if an Error object is present
if (!toastType && args.some((arg) => arg instanceof Error)) {
toastType = "error";
}
// Exit early if debug is off AND no toast is requested.
if (!Utils.isDebugEnabled && !toastType) {
return;
}
const consoleMethod =
{
warn: console.warn,
error: console.error,
}[toastType] || console.log;
consoleMethod(...args);
if (toastType) {
const message = args
.map((arg) => {
if (arg instanceof Error) return arg.message;
if (typeof arg === "object" && arg !== null) return JSON.stringify(arg);
return String(arg);
})
.join(" ");
toast[toastType](message);
}
};
/**
* Formats a distance in meters into a human-readable string (meters/km and feet/miles).
* @param {number} distanceMeters
* @returns {{ metric: string, imperial: string }}
*/
export function formatDistance(distanceMeters) {
const distanceFeet = distanceMeters * 3.28084;
const distanceMiles = distanceMeters * 0.000621371;
const metric = distanceMeters < 1000 ? distanceMeters.toFixed(0) + " m" : (distanceMeters / 1000).toFixed(2) + " km";
const imperial = distanceFeet < 5280 ? distanceFeet.toFixed(0) + " ft" : distanceMiles.toFixed(2) + " mi";
return { metric, imperial };
}
export { Utils as default };