@mks2508/better-logger 0.14.0-alpha.2 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunks/{Logger-Crw1woqR.js → Logger-CTAaDeZi.js} +49 -16
- package/dist/chunks/{Logger-Crw1woqR.js.map → Logger-CTAaDeZi.js.map} +1 -1
- package/dist/chunks/{Logger-D_p3xnbq.js → Logger-D8pK7XXt.js} +2 -2
- package/dist/chunks/{Logger-D_p3xnbq.js.map → Logger-D8pK7XXt.js.map} +1 -1
- package/dist/chunks/ScopedLogger-D-7q8uVP.js +2 -0
- package/dist/chunks/ScopedLogger-D-7q8uVP.js.map +1 -0
- package/dist/chunks/ScopedLogger-hS0qSkZD.js +361 -0
- package/dist/chunks/ScopedLogger-hS0qSkZD.js.map +1 -0
- package/dist/core.js +280 -307
- package/dist/exports.cjs +1 -1
- package/dist/exports.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +226 -159
- package/dist/index.js.map +1 -1
- package/dist/styling.cjs +1 -1
- package/dist/styling.js +2 -2
- package/dist/types/Logger.d.ts +11 -7
- package/dist/types/Logger.d.ts.map +1 -1
- package/dist/types/core.d.ts +122 -216
- package/dist/types/core.d.ts.map +1 -1
- package/dist/types/index.d.ts +119 -4
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +2 -1
- package/src/Logger.ts +99 -58
- package/src/index.ts +32 -25
- package/dist/.tsbuildinfo +0 -1
- package/dist/Logger.d.ts +0 -708
- package/dist/Logger.d.ts.map +0 -1
- package/dist/Logger.js +0 -1124
- package/dist/ScopedLogger.d.ts +0 -318
- package/dist/ScopedLogger.d.ts.map +0 -1
- package/dist/ScopedLogger.js +0 -426
- package/dist/cli/CommandProcessor.d.ts +0 -103
- package/dist/cli/CommandProcessor.d.ts.map +0 -1
- package/dist/cli/CommandProcessor.js +0 -184
- package/dist/cli/commands/ConfigCommand.d.ts +0 -17
- package/dist/cli/commands/ConfigCommand.d.ts.map +0 -1
- package/dist/cli/commands/ConfigCommand.js +0 -88
- package/dist/cli/commands/ExportCommand.d.ts +0 -51
- package/dist/cli/commands/ExportCommand.d.ts.map +0 -1
- package/dist/cli/commands/ExportCommand.js +0 -243
- package/dist/cli/commands/HistoryCommand.d.ts +0 -50
- package/dist/cli/commands/HistoryCommand.d.ts.map +0 -1
- package/dist/cli/commands/HistoryCommand.js +0 -96
- package/dist/cli/commands/StatusCommand.d.ts +0 -33
- package/dist/cli/commands/StatusCommand.d.ts.map +0 -1
- package/dist/cli/commands/StatusCommand.js +0 -93
- package/dist/cli/commands/ThemeCommand.d.ts +0 -33
- package/dist/cli/commands/ThemeCommand.d.ts.map +0 -1
- package/dist/cli/commands/ThemeCommand.js +0 -79
- package/dist/cli/help.d.ts +0 -15
- package/dist/cli/help.d.ts.map +0 -1
- package/dist/cli/help.js +0 -118
- package/dist/cli/index.d.ts +0 -15
- package/dist/cli/index.d.ts.map +0 -1
- package/dist/cli/index.js +0 -46
- package/dist/constants.d.ts +0 -122
- package/dist/constants.d.ts.map +0 -1
- package/dist/constants.js +0 -159
- package/dist/core.d.ts +0 -134
- package/dist/core.d.ts.map +0 -1
- package/dist/example.d.ts +0 -18
- package/dist/example.d.ts.map +0 -1
- package/dist/example.js +0 -154
- package/dist/exports-module.d.ts +0 -203
- package/dist/exports-module.d.ts.map +0 -1
- package/dist/exports-module.js +0 -260
- package/dist/handlers/AnalyticsLogHandler.d.ts +0 -11
- package/dist/handlers/AnalyticsLogHandler.d.ts.map +0 -1
- package/dist/handlers/AnalyticsLogHandler.js +0 -19
- package/dist/handlers/ExportLogHandler.d.ts +0 -103
- package/dist/handlers/ExportLogHandler.d.ts.map +0 -1
- package/dist/handlers/ExportLogHandler.js +0 -516
- package/dist/handlers/FileLogHandler.d.ts +0 -36
- package/dist/handlers/FileLogHandler.d.ts.map +0 -1
- package/dist/handlers/FileLogHandler.js +0 -156
- package/dist/handlers/RemoteLogHandler.d.ts +0 -14
- package/dist/handlers/RemoteLogHandler.d.ts.map +0 -1
- package/dist/handlers/RemoteLogHandler.js +0 -37
- package/dist/handlers/index.d.ts +0 -8
- package/dist/handlers/index.d.ts.map +0 -1
- package/dist/handlers/index.js +0 -7
- package/dist/index.d.ts +0 -157
- package/dist/index.d.ts.map +0 -1
- package/dist/main.d.ts +0 -2
- package/dist/main.d.ts.map +0 -1
- package/dist/main.js +0 -139
- package/dist/styling/LogStyleBuilder.d.ts +0 -149
- package/dist/styling/LogStyleBuilder.d.ts.map +0 -1
- package/dist/styling/LogStyleBuilder.js +0 -289
- package/dist/styling/SemanticStyles.d.ts +0 -181
- package/dist/styling/SemanticStyles.d.ts.map +0 -1
- package/dist/styling/SemanticStyles.js +0 -339
- package/dist/styling/SmartPresets.d.ts +0 -25
- package/dist/styling/SmartPresets.d.ts.map +0 -1
- package/dist/styling/SmartPresets.js +0 -276
- package/dist/styling/StyleBuilder.d.ts +0 -140
- package/dist/styling/StyleBuilder.d.ts.map +0 -1
- package/dist/styling/StyleBuilder.js +0 -280
- package/dist/styling/banners.d.ts +0 -45
- package/dist/styling/banners.d.ts.map +0 -1
- package/dist/styling/banners.js +0 -155
- package/dist/styling/index.d.ts +0 -10
- package/dist/styling/index.d.ts.map +0 -1
- package/dist/styling/index.js +0 -9
- package/dist/styling/themes.d.ts +0 -10
- package/dist/styling/themes.d.ts.map +0 -1
- package/dist/styling/themes.js +0 -231
- package/dist/styling-module.d.ts +0 -185
- package/dist/styling-module.d.ts.map +0 -1
- package/dist/styling-module.js +0 -199
- package/dist/types/core.js +0 -28
- package/dist/types/handlers.d.ts +0 -88
- package/dist/types/handlers.d.ts.map +0 -1
- package/dist/types/handlers.js +0 -4
- package/dist/types/index.js +0 -4
- package/dist/utils/environment.d.ts +0 -47
- package/dist/utils/environment.d.ts.map +0 -1
- package/dist/utils/environment.js +0 -85
- package/dist/utils/formatting.d.ts +0 -40
- package/dist/utils/formatting.d.ts.map +0 -1
- package/dist/utils/formatting.js +0 -117
- package/dist/utils/index.d.ts +0 -7
- package/dist/utils/index.d.ts.map +0 -1
- package/dist/utils/index.js +0 -6
- package/dist/utils/opentui-detection.d.ts +0 -34
- package/dist/utils/opentui-detection.d.ts.map +0 -1
- package/dist/utils/opentui-detection.js +0 -116
- package/dist/utils/output.d.ts +0 -44
- package/dist/utils/output.d.ts.map +0 -1
- package/dist/utils/output.js +0 -150
- package/dist/utils/stackTrace.d.ts +0 -9
- package/dist/utils/stackTrace.d.ts.map +0 -1
- package/dist/utils/stackTrace.js +0 -81
- package/dist/utils/timestamps.d.ts +0 -20
- package/dist/utils/timestamps.d.ts.map +0 -1
- package/dist/utils/timestamps.js +0 -71
package/dist/utils/output.js
DELETED
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Styled output creation utilities for Advanced Logger
|
|
3
|
-
*/
|
|
4
|
-
import { formatTimestamp } from './timestamps.js';
|
|
5
|
-
import { StyleBuilder } from '../styling/index.js';
|
|
6
|
-
import { ADAPTIVE_COLORS } from '../constants.js';
|
|
7
|
-
/**
|
|
8
|
-
* Detects the current DevTools theme preference
|
|
9
|
-
*/
|
|
10
|
-
export function detectDevToolsTheme() {
|
|
11
|
-
try {
|
|
12
|
-
if (typeof window === 'undefined' || !window.matchMedia) {
|
|
13
|
-
return 'light'; // Safe fallback for SSR or older browsers
|
|
14
|
-
}
|
|
15
|
-
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
|
|
16
|
-
return mediaQuery.matches ? 'dark' : 'light';
|
|
17
|
-
}
|
|
18
|
-
catch (error) {
|
|
19
|
-
console.warn('Failed to detect DevTools theme:', error);
|
|
20
|
-
return 'light'; // Fallback to light theme
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Gets adaptive color based on current DevTools theme
|
|
25
|
-
*/
|
|
26
|
-
export function getAdaptiveColor(colors, theme) {
|
|
27
|
-
const currentTheme = theme ?? detectDevToolsTheme();
|
|
28
|
-
return colors[currentTheme];
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Sets up theme change listener for dynamic updates
|
|
32
|
-
*/
|
|
33
|
-
export function setupThemeChangeListener(callback) {
|
|
34
|
-
try {
|
|
35
|
-
if (typeof window === 'undefined' || !window.matchMedia) {
|
|
36
|
-
return null;
|
|
37
|
-
}
|
|
38
|
-
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
|
|
39
|
-
const handler = (e) => {
|
|
40
|
-
callback(e.matches ? 'dark' : 'light');
|
|
41
|
-
};
|
|
42
|
-
// Use newer addEventListener if available, fallback to addListener
|
|
43
|
-
if (mediaQuery.addEventListener) {
|
|
44
|
-
mediaQuery.addEventListener('change', handler);
|
|
45
|
-
return () => mediaQuery.removeEventListener('change', handler);
|
|
46
|
-
}
|
|
47
|
-
else if (mediaQuery.addListener) {
|
|
48
|
-
mediaQuery.addListener(handler);
|
|
49
|
-
return () => mediaQuery.removeListener?.(handler);
|
|
50
|
-
}
|
|
51
|
-
return null;
|
|
52
|
-
}
|
|
53
|
-
catch (error) {
|
|
54
|
-
console.warn('Failed to set up theme change listener:', error);
|
|
55
|
-
return null;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Creates styled console output with multiple %c formatters
|
|
60
|
-
*/
|
|
61
|
-
export function createStyledOutput(level, levelStyles, prefix, message, stackInfo, autoDetectTheme = true) {
|
|
62
|
-
const levelConfig = levelStyles[level];
|
|
63
|
-
const timestamp = formatTimestamp();
|
|
64
|
-
const currentTheme = autoDetectTheme ? detectDevToolsTheme() : 'light';
|
|
65
|
-
// Base styles with adaptive colors
|
|
66
|
-
const timestampStyle = new StyleBuilder()
|
|
67
|
-
.color(getAdaptiveColor(ADAPTIVE_COLORS.timestamp, currentTheme))
|
|
68
|
-
.size('11px')
|
|
69
|
-
.font('Monaco, Consolas, monospace')
|
|
70
|
-
.build();
|
|
71
|
-
const levelStyle = new StyleBuilder()
|
|
72
|
-
.bg(levelConfig.background)
|
|
73
|
-
.color(levelConfig.color)
|
|
74
|
-
.border(levelConfig.border)
|
|
75
|
-
.shadow(levelConfig.shadow)
|
|
76
|
-
.padding('2px 8px')
|
|
77
|
-
.rounded('4px')
|
|
78
|
-
.bold()
|
|
79
|
-
.font('Monaco, Consolas, monospace')
|
|
80
|
-
.size('12px')
|
|
81
|
-
.build();
|
|
82
|
-
const prefixStyle = new StyleBuilder()
|
|
83
|
-
.bg(getAdaptiveColor(ADAPTIVE_COLORS.prefixBackground, currentTheme))
|
|
84
|
-
.color(getAdaptiveColor(ADAPTIVE_COLORS.prefix, currentTheme))
|
|
85
|
-
.padding('2px 6px')
|
|
86
|
-
.rounded('3px')
|
|
87
|
-
.bold()
|
|
88
|
-
.font('Monaco, Consolas, monospace')
|
|
89
|
-
.size('11px')
|
|
90
|
-
.build();
|
|
91
|
-
const messageStyle = new StyleBuilder()
|
|
92
|
-
.color(getAdaptiveColor(ADAPTIVE_COLORS.messageText, currentTheme))
|
|
93
|
-
.font('system-ui, -apple-system, sans-serif')
|
|
94
|
-
.size('14px')
|
|
95
|
-
.build();
|
|
96
|
-
const locationStyle = new StyleBuilder()
|
|
97
|
-
.color(getAdaptiveColor(ADAPTIVE_COLORS.location, currentTheme))
|
|
98
|
-
.size('11px')
|
|
99
|
-
.font('Monaco, Consolas, monospace')
|
|
100
|
-
.build();
|
|
101
|
-
// Build format string and styles
|
|
102
|
-
let format = `%c${timestamp.slice(11, 23)} %c${levelConfig.emoji} ${levelConfig.label}`;
|
|
103
|
-
const styles = [timestampStyle, levelStyle];
|
|
104
|
-
if (prefix) {
|
|
105
|
-
format += ` %c${prefix}`;
|
|
106
|
-
styles.push(prefixStyle);
|
|
107
|
-
}
|
|
108
|
-
format += ` %c${message}`;
|
|
109
|
-
styles.push(messageStyle);
|
|
110
|
-
if (stackInfo) {
|
|
111
|
-
format += ` %c(${stackInfo.file}:${stackInfo.line}:${stackInfo.column})`;
|
|
112
|
-
styles.push(locationStyle);
|
|
113
|
-
}
|
|
114
|
-
return [format, ...styles];
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Create a unique ID for log entries
|
|
118
|
-
*/
|
|
119
|
-
export function generateLogId() {
|
|
120
|
-
return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Escape HTML entities for safe HTML output
|
|
124
|
-
*/
|
|
125
|
-
export function escapeHtml(text) {
|
|
126
|
-
const div = document.createElement('div');
|
|
127
|
-
div.textContent = text;
|
|
128
|
-
return div.innerHTML;
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* Convert objects to safe string representation for logging
|
|
132
|
-
*/
|
|
133
|
-
export function safeStringify(obj, _maxDepth = 3) {
|
|
134
|
-
try {
|
|
135
|
-
return JSON.stringify(obj, (_key, value) => {
|
|
136
|
-
if (typeof value === 'function')
|
|
137
|
-
return '[Function]';
|
|
138
|
-
if (value instanceof Error)
|
|
139
|
-
return `[Error: ${value.message}]`;
|
|
140
|
-
if (value instanceof Date)
|
|
141
|
-
return value.toISOString();
|
|
142
|
-
if (typeof value === 'undefined')
|
|
143
|
-
return '[undefined]';
|
|
144
|
-
return value;
|
|
145
|
-
}, 2);
|
|
146
|
-
}
|
|
147
|
-
catch (error) {
|
|
148
|
-
return String(obj);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Stack trace parsing utilities for Advanced Logger
|
|
3
|
-
*/
|
|
4
|
-
import type { StackInfo } from '../types/index.js';
|
|
5
|
-
/**
|
|
6
|
-
* Parses the current stack trace to extract caller information
|
|
7
|
-
*/
|
|
8
|
-
export declare function parseStackTrace(): StackInfo | null;
|
|
9
|
-
//# sourceMappingURL=stackTrace.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"stackTrace.d.ts","sourceRoot":"","sources":["../../src/utils/stackTrace.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAEnD;;GAEG;AACH,wBAAgB,eAAe,IAAI,SAAS,GAAG,IAAI,CAqFlD"}
|
package/dist/utils/stackTrace.js
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Stack trace parsing utilities for Advanced Logger
|
|
3
|
-
*/
|
|
4
|
-
/**
|
|
5
|
-
* Parses the current stack trace to extract caller information
|
|
6
|
-
*/
|
|
7
|
-
export function parseStackTrace() {
|
|
8
|
-
try {
|
|
9
|
-
const stack = new Error().stack;
|
|
10
|
-
if (!stack) {
|
|
11
|
-
return null;
|
|
12
|
-
}
|
|
13
|
-
const lines = stack.split('\n').filter(line => line.trim());
|
|
14
|
-
// Find the first caller that's not from Logger methods
|
|
15
|
-
for (let i = 1; i < lines.length; i++) {
|
|
16
|
-
const line = lines[i];
|
|
17
|
-
if (!line) {
|
|
18
|
-
continue;
|
|
19
|
-
}
|
|
20
|
-
// Skip if line contains Logger methods or parseStackTrace
|
|
21
|
-
if (line.includes('parseStackTrace') ||
|
|
22
|
-
line.includes('Logger.') ||
|
|
23
|
-
line.includes('.log(') ||
|
|
24
|
-
line.includes('createStyledOutput')) {
|
|
25
|
-
continue;
|
|
26
|
-
}
|
|
27
|
-
// Parse different stack trace formats
|
|
28
|
-
let match;
|
|
29
|
-
// Chrome format: "at functionName (file:line:column)" or "at file:line:column"
|
|
30
|
-
const chromeMatch = line.match(/at\s+(?:(.+?)\s+\()?(.+?):(\d+):(\d+)\)?$/);
|
|
31
|
-
if (chromeMatch) {
|
|
32
|
-
match = chromeMatch;
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
// Firefox format: "functionName@file:line:column"
|
|
36
|
-
const firefoxMatch = line.match(/(.+?)@(.+?):(\d+):(\d+)$/);
|
|
37
|
-
if (firefoxMatch) {
|
|
38
|
-
match = firefoxMatch;
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
// Safari/other formats
|
|
42
|
-
const safariMatch = line.match(/(\S+)?@(.+?):(\d+):(\d+)$/);
|
|
43
|
-
if (safariMatch) {
|
|
44
|
-
match = safariMatch;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
if (!match) {
|
|
49
|
-
continue;
|
|
50
|
-
}
|
|
51
|
-
const [, functionName, file, lineStr, columnStr] = match;
|
|
52
|
-
if (!file) {
|
|
53
|
-
continue;
|
|
54
|
-
}
|
|
55
|
-
// Process file path - remove query params and get filename
|
|
56
|
-
const fileParts = file.split('/');
|
|
57
|
-
const fileName = fileParts[fileParts.length - 1];
|
|
58
|
-
if (!fileName) {
|
|
59
|
-
continue;
|
|
60
|
-
}
|
|
61
|
-
const cleanFileName = fileName.split('?')[0];
|
|
62
|
-
// Parse line and column numbers
|
|
63
|
-
const lineNum = lineStr ? parseInt(lineStr, 10) : 0;
|
|
64
|
-
const columnNum = columnStr ? parseInt(columnStr, 10) : 0;
|
|
65
|
-
// Clean function name
|
|
66
|
-
const cleanFunction = functionName && functionName.trim()
|
|
67
|
-
? functionName.trim()
|
|
68
|
-
: undefined;
|
|
69
|
-
return {
|
|
70
|
-
file: cleanFileName ? cleanFileName : 'unknown',
|
|
71
|
-
line: lineNum,
|
|
72
|
-
column: columnNum,
|
|
73
|
-
function: cleanFunction
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
return null;
|
|
77
|
-
}
|
|
78
|
-
catch {
|
|
79
|
-
return null;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Timestamp utilities for Advanced Logger
|
|
3
|
-
*/
|
|
4
|
-
/**
|
|
5
|
-
* Formats the timestamp using modern Date API
|
|
6
|
-
*/
|
|
7
|
-
export declare function formatTimestamp(): string;
|
|
8
|
-
/**
|
|
9
|
-
* Parse relative time strings like "2h", "30m", "1d" into milliseconds
|
|
10
|
-
*/
|
|
11
|
-
export declare function parseRelativeTime(timeStr: string): number;
|
|
12
|
-
/**
|
|
13
|
-
* Parse time input which can be Date, ISO string, or relative time
|
|
14
|
-
*/
|
|
15
|
-
export declare function parseTimeInput(input: Date | string | number): Date;
|
|
16
|
-
/**
|
|
17
|
-
* Format timestamp for display in different contexts
|
|
18
|
-
*/
|
|
19
|
-
export declare function formatDisplayTime(date: Date, format?: 'short' | 'full' | 'time-only'): string;
|
|
20
|
-
//# sourceMappingURL=timestamps.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"timestamps.d.ts","sourceRoot":"","sources":["../../src/utils/timestamps.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH;;GAEG;AACH,wBAAgB,eAAe,IAAI,MAAM,CAQxC;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAUzD;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,CA2BlE;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,OAAO,GAAG,MAAM,GAAG,WAAqB,GAAG,MAAM,CAUtG"}
|
package/dist/utils/timestamps.js
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Timestamp utilities for Advanced Logger
|
|
3
|
-
*/
|
|
4
|
-
import { TIME_UNITS } from '../constants.js';
|
|
5
|
-
/**
|
|
6
|
-
* Formats the timestamp using modern Date API
|
|
7
|
-
*/
|
|
8
|
-
export function formatTimestamp() {
|
|
9
|
-
try {
|
|
10
|
-
// Use modern Temporal API if available, fallback to Date
|
|
11
|
-
const now = new Date();
|
|
12
|
-
return now.toISOString();
|
|
13
|
-
}
|
|
14
|
-
catch {
|
|
15
|
-
return new Date().toISOString();
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Parse relative time strings like "2h", "30m", "1d" into milliseconds
|
|
20
|
-
*/
|
|
21
|
-
export function parseRelativeTime(timeStr) {
|
|
22
|
-
const match = timeStr.match(/^(\d+)(ms|s|m|h|d)$/);
|
|
23
|
-
if (!match) {
|
|
24
|
-
throw new Error(`Invalid time format: ${timeStr}. Use format like "2h", "30m", "1d"`);
|
|
25
|
-
}
|
|
26
|
-
const [, amount, unit] = match;
|
|
27
|
-
const multiplier = TIME_UNITS[unit];
|
|
28
|
-
return parseInt(amount || '0', 10) * multiplier;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Parse time input which can be Date, ISO string, or relative time
|
|
32
|
-
*/
|
|
33
|
-
export function parseTimeInput(input) {
|
|
34
|
-
if (input instanceof Date) {
|
|
35
|
-
return input;
|
|
36
|
-
}
|
|
37
|
-
if (typeof input === 'number') {
|
|
38
|
-
// Assume hours ago
|
|
39
|
-
return new Date(Date.now() - input * TIME_UNITS.h);
|
|
40
|
-
}
|
|
41
|
-
if (typeof input === 'string') {
|
|
42
|
-
// Try parsing as ISO date first
|
|
43
|
-
const isoDate = new Date(input);
|
|
44
|
-
if (!isNaN(isoDate.getTime())) {
|
|
45
|
-
return isoDate;
|
|
46
|
-
}
|
|
47
|
-
// Try parsing as relative time
|
|
48
|
-
try {
|
|
49
|
-
const ms = parseRelativeTime(input);
|
|
50
|
-
return new Date(Date.now() - ms);
|
|
51
|
-
}
|
|
52
|
-
catch {
|
|
53
|
-
throw new Error(`Invalid time format: ${input}`);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
throw new Error(`Unsupported time input type: ${typeof input}`);
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Format timestamp for display in different contexts
|
|
60
|
-
*/
|
|
61
|
-
export function formatDisplayTime(date, format = 'short') {
|
|
62
|
-
switch (format) {
|
|
63
|
-
case 'time-only':
|
|
64
|
-
return date.toTimeString().slice(0, 8); // HH:MM:SS
|
|
65
|
-
case 'full':
|
|
66
|
-
return date.toISOString();
|
|
67
|
-
case 'short':
|
|
68
|
-
default:
|
|
69
|
-
return date.toISOString().slice(11, 23); // HH:MM:SS.mmm
|
|
70
|
-
}
|
|
71
|
-
}
|