storybookjs / telejson

🛰 JSON parse & stringify with support for cyclic objects, functions, dates, regex, infinity, undefined, null, NaN, Classes, Instances
MIT License
169 stars 29 forks source link

[Bug] Fails if object has key of `''` #93

Open tmeasday opened 1 year ago

tmeasday commented 1 year ago

Describe the bug

These lines: https://github.com/storybookjs/telejson/blob/6d2e7a4b7704c927a7dc7b5cb1735cc252922302/src/index.ts#L149-L150 assume objects cannot have keys ''. As we saw in https://github.com/storybookjs/storybook/pull/20755, that isn't a good assumption.

Steps to reproduce the behavior

## in node REPL

> const b = {}
undefined

> b[''] = { b: b}
<ref *1> { b: { '': [Circular *1] } }

> require('telejson').stringify(b)
Uncaught TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Object'
    |     <anonymous> -> object with constructor 'Object'
    --- property 'b' closes the circle
    at JSON.stringify (<anonymous>)
    at Object.stringify (/Users/tom/GitHub/chromaui/chromatic/node_modules/telejson/dist/cjs/index.js:467:15)

Expected behavior

It should stringify correctly.