diff --git a/src/index.ts b/src/index.ts index 8f2c2c2..059fb54 100644 --- a/src/index.ts +++ b/src/index.ts @@ -141,15 +141,15 @@ export default function devalue(value: any, level = defaultLogLevel) { } return stringify(json); } - const obj = `{${Object.keys(thing).map(key => `${safeKey(key)}:${stringify(thing[key])}`).join(',')}}`; - const proto = Object.getPrototypeOf(thing); - if (proto === null) { - return Object.keys(thing).length > 0 - ? `Object.assign(Object.create(null), ${obj})` - : `Object.create(null)`; + if (Object.getPrototypeOf(thing) === null) { + if (Object.keys(thing).length === 0) { + return 'Object.create(null)'; + } + + return `Object.create(null,{${Object.keys(thing).map(key => `${safeKey(key)}:{writable:true,value:${stringify(thing[key])}}`).join(',')}})`; } - return obj; + return `{${Object.keys(thing).map(key => `${safeKey(key)}:${stringify(thing[key])}`).join(',')}}`; } } diff --git a/test/test.ts b/test/test.ts index e6652e3..8ee3711 100644 --- a/test/test.ts +++ b/test/test.ts @@ -124,6 +124,8 @@ describe('devalue', () => { describe('misc', () => { test('Object without prototype', Object.create(null), 'Object.create(null)'); + test('Object without prototype with values', Object.assign(Object.create(null), { foo: 1 }), 'Object.create(null,{foo:{writable:true,value:1}})'); + // let arr = []; // arr.x = 42; // test('Array with named properties', arr, `TODO`);