Skip to content

Commit 46d93a2

Browse files
authored
fix: support custom toString method in %s format (#7637)
1 parent 3fb3fbf commit 46d93a2

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

packages/utils/src/display.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ export function format(...args: unknown[]): string {
110110
return '-0'
111111
}
112112
if (typeof value === 'object' && value !== null) {
113+
if (typeof value.toString === 'function' && value.toString !== Object.prototype.toString) {
114+
return value.toString()
115+
}
113116
return inspect(value, { depth: 0, colors: false })
114117
}
115118
return String(value)

test/core/test/utils-display.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ describe('format', () => {
1818
['%s', -0],
1919
['%s', null],
2020
['%s', null, 'next'],
21+
[
22+
'%s',
23+
new (class {
24+
constructor(public value: string) {}
25+
toString() {
26+
return this.value
27+
}
28+
})('string value'),
29+
],
2130
['%d', 100],
2231
['%d', 100n],
2332
['%d', null],

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy