From b985e4f00ddd263b77b559fe1ec1de5fcee1d436 Mon Sep 17 00:00:00 2001 From: Pascal Corpet Date: Fri, 25 Jun 2021 09:49:44 +0200 Subject: [PATCH] feat: handle objects with an anonymous prototype (#49) --- index.js | 11 ++++++++--- test/objects.js | 10 ++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index f6946da..16a3469 100644 --- a/index.js +++ b/index.js @@ -151,9 +151,14 @@ export function inspect(value, options) { return inspectHTMLElement(value, options) } - // If it is a class, inspect it like an object but add the constructor name - if ('constructor' in value && value.constructor !== Object) { - return inspectClass(value, options) + if ('constructor' in value) { + // If it is a class, inspect it like an object but add the constructor name + if (value.constructor !== Object) { + return inspectClass(value, options) + } + + // If it is an object with an anonymous prototype, display it as an object. + return inspectObject(value, options) } // We have run out of options! Just stringify the value diff --git a/test/objects.js b/test/objects.js index 23beb2b..deda334 100644 --- a/test/objects.js +++ b/test/objects.js @@ -35,6 +35,16 @@ for (const [suite, inspect] of Object.entries({ }) } + it('returns `{}` for empty objects with an anonoymous prototype', () => { + expect(inspect(Object.create({ a: 1 }))).to.equal('{}') + }) + + it("shows objects' own properties for objects with an anonoymous prototype", () => { + const obj = Object.create({ a: 1 }) + obj.b = 2 + expect(inspect(obj)).to.equal('{ b: 2 }') + }) + describe('truncate', () => { it('returns the full representation when truncate is over string length', () => { expect(inspect({ a: 1, b: 2, c: 3 }, { truncate: 20 })).to.equal('{ a: 1, b: 2, c: 3 }')