Skip to content

Commit

Permalink
fix: ignore error when key of Map is an object
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 committed Sep 21, 2022
1 parent b56cc5d commit da2053e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
3 changes: 2 additions & 1 deletion examples/basic/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ const loopArray = [
loopArray[1] = loopArray

const longArray = Array.from({ length: 1000 }).map((_, i) => i)
const map = new Map<string, any>()
const map = new Map<any, any>()
map.set('foo', 1)
map.set('goo', 'hello')
map.set({}, 'world')

const set = new Set([1, 2, 3])

Expand Down
4 changes: 3 additions & 1 deletion src/components/DataTypes/Object.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ export const ObjectType: React.FC<DataItemProps<object>> = (props) => {
if (value instanceof Map) {
let _count = 0
for (const item of value) {
const [key, value] = item
// fixme: key might be a object, array, or any value for the `Map<any, any>`
const [k, value] = item
const key = `${k}`
elements.push(
<DataKeyPair key={key} path={[...props.path, key]} value={value}
editable={false}/>
Expand Down
18 changes: 18 additions & 0 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ describe('render <JsonViewer/>', () => {
render(<JsonViewer value={new Set([1, '2', 3.0, Infinity])}/>)
})

it('render Map', () => {
render(
<JsonViewer
value={new Map<string, number>([['foo', 1], ['goo', 2]])}/>
)
render(
<JsonViewer
value={new Map<any, number>([[[], 1], [{}, 2]])}/>
)
})

it('render object', () => {
render(<JsonViewer value={{}}/>)
render(<JsonViewer value={{
Expand All @@ -53,4 +64,11 @@ describe('render <JsonViewer/>', () => {
}
}}/>)
})

it('render function', () => {
render(<JsonViewer value={function aPlusB (a: number, b: number) {
return a + b
}}/>)
render(<JsonViewer value={(a: number, b: number) => a + b}/>)
})
})

0 comments on commit da2053e

Please sign in to comment.