Skip to content

Commit 10eec71

Browse files
committed
Custom JSON.stringify in Demo app to handle non-JSON types
1 parent f528a38 commit 10eec71

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

demo/src/helpers.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,20 @@ import { JsonData } from '@json-edit-react'
33
export const truncate = (string: string, length = 200) =>
44
string.length < length ? string : `${string.slice(0, length - 2).trim()}...`
55

6-
export const getLineHeight = (data: JsonData) => JSON.stringify(data, null, 2).split('\n').length
6+
export const getLineHeight = (data: JsonData) => jsonStringify(data).split('\n').length
7+
8+
// Special JSON.stringify with custom replacer functions for special types
9+
const jsonStringify = (data: JsonData) =>
10+
JSON.stringify(
11+
data,
12+
(_, value) => {
13+
if (typeof value === 'bigint') {
14+
return value.toString()
15+
}
16+
if (typeof value === 'symbol') {
17+
return value.toString()
18+
}
19+
return value
20+
},
21+
2
22+
)

0 commit comments

Comments
 (0)