})
).toBe(JSON.stringify({ n: 1, np: 2 }, null, 2))
})
+
+ test('objects with custom toString', () => {
+ class TestClass {
+ toString() {
+ return 'foo'
+ }
+ }
+ const instance = new TestClass()
+ expect(toDisplayString(instance)).toBe('foo')
+ const obj = { toString: () => 'bar' }
+ expect(toDisplayString(obj)).toBe('bar')
+ })
test('native objects', () => {
const div = document.createElement('div')
- expect(toDisplayString(div)).toBe(`"[object HTMLDivElement]"`)
+ expect(toDisplayString(div)).toBe('[object HTMLDivElement]')
expect(toDisplayString({ div })).toMatchInlineSnapshot(`
"{
\\"div\\": \\"[object HTMLDivElement]\\"
-import { isArray, isMap, isObject, isPlainObject, isSet } from './index'
+import {
+ isArray,
+ isMap,
+ isObject,
+ isPlainObject,
+ isSet,
+ objectToString
+} from './index'
/**
* For converting {{ interpolation }} values to displayed strings.
export const toDisplayString = (val: unknown): string => {
return val == null
? ''
- : isObject(val)
+ : isArray(val) || (isObject(val) && val.toString === objectToString)
? JSON.stringify(val, replacer, 2)
: String(val)
}