Skip to content

Commit d5e7a15

Browse files
committed
feat: Utils better log() and padUtcTimestamp
1 parent 68d7db6 commit d5e7a15

1 file changed

Lines changed: 65 additions & 2 deletions

File tree

src/Utils.js

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// src/Utils.js
2+
import { toast } from "react-toastify";
23

34
class Utils {
45
// If undefined defaults to false
@@ -28,6 +29,34 @@ class Utils {
2829
}
2930
}
3031

32+
/**
33+
* Pads a partial UTC timestamp string to the full HH:mm:ss format.
34+
* Treats the input as a literal UTC string with no timezone conversion.
35+
* @param {string} timeString The user-provided time string (e.g., "2025-09-06T" or "2025-09-06T14:30").
36+
* @returns {string} The fully padded string (e.g., "2025-09-06T00:00:00" or "2025-09-06T14:30:00").
37+
*/
38+
export function padUtcTimestamp(timeString) {
39+
if (!timeString) {
40+
return timeString;
41+
}
42+
43+
const [datePart, timePart] = timeString.split("T");
44+
45+
// If there's no 'T' or the date part is missing, it's not a format we can pad.
46+
if (timePart === undefined || !datePart) {
47+
return timeString;
48+
}
49+
50+
const timeSegments = timePart.split(":").filter(Boolean); // filter(Boolean) handles the "T" with nothing after it.
51+
52+
while (timeSegments.length < 3) {
53+
timeSegments.push("00");
54+
}
55+
56+
const paddedTimePart = timeSegments.join(":");
57+
return `${datePart}T${paddedTimePart}`;
58+
}
59+
3160
window.debug = {
3261
enable: () => {
3362
Utils.isDebugEnabled = true;
@@ -47,8 +76,42 @@ window.debug = {
4776

4877
// Export the log function directly
4978
export const log = (...args) => {
50-
if (Utils.isDebugEnabled) {
51-
console.log(...args);
79+
const validToastTypes = ["info", "success", "warn", "error"];
80+
const lastArg = args[args.length - 1];
81+
let toastType = null;
82+
83+
// First, check for an explicit toast type (e.g., 'info', 'warn')
84+
if (typeof lastArg === "string" && validToastTypes.includes(lastArg)) {
85+
toastType = args.pop();
86+
}
87+
88+
// If no explicit type, infer 'error' if an Error object is present
89+
if (!toastType && args.some((arg) => arg instanceof Error)) {
90+
toastType = "error";
91+
}
92+
93+
// Exit early if debug is off AND no toast is requested.
94+
if (!Utils.isDebugEnabled && !toastType) {
95+
return;
96+
}
97+
98+
const consoleMethod =
99+
{
100+
warn: console.warn,
101+
error: console.error,
102+
}[toastType] || console.log;
103+
104+
consoleMethod(...args);
105+
106+
if (toastType) {
107+
const message = args
108+
.map((arg) => {
109+
if (arg instanceof Error) return arg.message;
110+
if (typeof arg === "object" && arg !== null) return JSON.stringify(arg);
111+
return String(arg);
112+
})
113+
.join(" ");
114+
toast[toastType](message);
52115
}
53116
};
54117

0 commit comments

Comments
 (0)