Skip to content

Commit b3cd34f

Browse files
committed
add ie11 compat
code by samboylett
1 parent 5f2c229 commit b3cd34f

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,15 @@ export default function devalue(value: any, level = defaultLogLevel) {
141141
}
142142
return stringify(json);
143143
}
144-
const obj = `{${Object.keys(thing).map(key => `${safeKey(key)}:${stringify(thing[key])}`).join(',')}}`;
145-
const proto = Object.getPrototypeOf(thing);
146-
if (proto === null) {
147-
return Object.keys(thing).length > 0
148-
? `Object.assign(Object.create(null), ${obj})`
149-
: `Object.create(null)`;
144+
if (Object.getPrototypeOf(thing) === null) {
145+
if (Object.keys(thing).length === 0) {
146+
return 'Object.create(null)';
147+
}
148+
149+
return `Object.create(null,{${Object.keys(thing).map(key => `${safeKey(key)}:{writable:true,value:${stringify(thing[key])}}`).join(',')}})`;
150150
}
151151

152-
return obj;
152+
return `{${Object.keys(thing).map(key => `${safeKey(key)}:${stringify(thing[key])}`).join(',')}}`;
153153
}
154154
}
155155

test/test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ describe('devalue', () => {
124124
describe('misc', () => {
125125
test('Object without prototype', Object.create(null), 'Object.create(null)');
126126

127+
test('Object without prototype with values', Object.assign(Object.create(null), { foo: 1 }), 'Object.create(null,{foo:{writable:true,value:1}})');
128+
127129
// let arr = [];
128130
// arr.x = 42;
129131
// test('Array with named properties', arr, `TODO`);

0 commit comments

Comments
 (0)