Skip to content

Commit a1244bc

Browse files
committed
stringifyArg
1 parent 76730de commit a1244bc

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

src/addons/consoleCatcher.ts

+18-20
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@ const createConsoleCatcher = (): {
1313
const consoleOutput: ConsoleLogEvent[] = [];
1414
let isInitialized = false;
1515

16+
/**
17+
* Converts any argument to its string representation
18+
*
19+
* @param arg - Console arguments
20+
*/
21+
const stringifyArg = (arg: unknown): string => {
22+
if (typeof arg === 'string') {
23+
return arg;
24+
}
25+
if (typeof arg === 'number' || typeof arg === 'boolean') {
26+
return String(arg);
27+
}
28+
29+
return safeStringify(arg);
30+
};
31+
1632
/**
1733
* Formats console arguments handling %c directives
1834
*
@@ -30,16 +46,7 @@ const createConsoleCatcher = (): {
3046

3147
if (typeof firstArg !== 'string' || !firstArg.includes('%c')) {
3248
return {
33-
message: args.map((arg) => {
34-
if (typeof arg === 'string') {
35-
return arg;
36-
}
37-
if (typeof arg === 'number' || typeof arg === 'boolean') {
38-
return String(arg);
39-
}
40-
41-
return safeStringify(arg);
42-
}).join(''),
49+
message: args.map(stringifyArg).join(' '),
4350
styles: [],
4451
};
4552
}
@@ -63,16 +70,7 @@ const createConsoleCatcher = (): {
6370
// Add remaining arguments that aren't styles
6471
const remainingArgs = args
6572
.slice(styles.length + 1)
66-
.map((arg) => {
67-
if (typeof arg === 'string') {
68-
return arg;
69-
}
70-
if (typeof arg === 'number' || typeof arg === 'boolean') {
71-
return String(arg);
72-
}
73-
74-
return safeStringify(arg);
75-
})
73+
.map(stringifyArg)
7674
.join(' ');
7775

7876
return {

0 commit comments

Comments
 (0)