forked from ChromeDevTools/chrome-devtools-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleFormat.ts
More file actions
154 lines (131 loc) · 3.67 KB
/
ConsoleFormat.ts
File metadata and controls
154 lines (131 loc) · 3.67 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
/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* This utility formats console messages with template strings, reusing Chrome DevTools
* Based on the Console Standard (https://console.spec.whatwg.org/#formatter).
*/
// Formats a console message text with its arguments, resolving format specifiers.
export function formatConsoleMessage(
text: string,
args: unknown[],
): {formattedText: string; remainingArgs: unknown[]} {
if (!text) {
return {formattedText: text, remainingArgs: args};
}
let result = '';
let argIndex = 0;
// eslint-disable-next-line no-control-regex
const re = /%([%_Oocsdfi])|\x1B\[([\d;]*)m/g;
let lastIndex = 0;
let match: RegExpExecArray | null;
while ((match = re.exec(text)) !== null) {
result += text.substring(lastIndex, match.index);
lastIndex = re.lastIndex;
const specifier = match[1];
if (specifier !== undefined) {
switch (specifier) {
case '%':
// Escaped percent sign
result += '%';
break;
case 's':
// String substitution
if (argIndex < args.length) {
result += formatArg(args[argIndex++], 'string');
} else {
result += match[0]; // Keep the specifier if no arg available
}
break;
case 'c':
// Style substitution
if (argIndex < args.length) {
argIndex++;
} else {
result += match[0];
}
break;
case 'o':
case 'O':
// Object substitution
if (argIndex < args.length) {
result += formatArg(args[argIndex++], 'object');
} else {
result += match[0];
}
break;
case '_':
// Ignore substitution
if (argIndex < args.length) {
argIndex++;
} else {
result += match[0];
}
break;
case 'd':
case 'i':
// Integer substitution
if (argIndex < args.length) {
const value = args[argIndex++];
const numValue =
typeof value === 'number' ? value : Number(value);
result += isNaN(numValue)
? 'NaN'
: Math.floor(numValue).toString();
} else {
result += match[0];
}
break;
case 'f':
// Float substitution
if (argIndex < args.length) {
const value = args[argIndex++];
const numValue =
typeof value === 'number' ? value : Number(value);
result += isNaN(numValue) ? 'NaN' : numValue.toString();
} else {
result += match[0];
}
break;
default:
// Unknown specifier, keep it as is
result += match[0];
break;
}
} else {
// Handle ANSI escape codes - we ignore them in the formatted output
}
}
// Add any remaining text after the last match
result += text.substring(lastIndex);
// Return formatted text and unused arguments
return {
formattedText: result,
remainingArgs: args.slice(argIndex),
};
}
// Formats an argument value for display.
function formatArg(arg: unknown, _hint: 'string' | 'object'): string {
if (arg === null) {
return 'null';
}
if (arg === undefined) {
return 'undefined';
}
if (typeof arg === 'string') {
return arg;
}
if (typeof arg === 'number' || typeof arg === 'boolean') {
return String(arg);
}
if (typeof arg === 'object') {
try {
return JSON.stringify(arg);
} catch {
return String(arg);
}
}
return String(arg);
}