Skip to content

Commit

Permalink
feat: handle objects with an anonymous prototype (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
pcorpet authored Jun 25, 2021
1 parent 917757d commit b985e4f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
11 changes: 8 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions test/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }')
Expand Down

0 comments on commit b985e4f

Please sign in to comment.