Skip to content

Commit

Permalink
fix: do not quote string results of custom inspect functions (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
pcorpet authored Jun 24, 2021
1 parent 5db74c7 commit 5e76830
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ export function inspect(value, options) {
// If `options.customInspect` is set to true then try to use the custom inspector
if (customInspect && value) {
const output = inspectCustom(value, options, type)
if (output) return inspect(output, options)
if (output) {
if (typeof output === 'string') return output
return inspect(output, options)
}
}

const proto = value ? Object.getPrototypeOf(value) : false
Expand Down
13 changes: 10 additions & 3 deletions test/acceptance.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ describe('objects', () => {

it('uses custom inspect function if `customInspect` is turned on', () => {
const obj = {
inspect: () => 1,
inspect: () => 'abc',
}
expect(inspect(obj, { customInspect: true })).to.equal('abc')
})

it('inspects custom inspect function result', () => {
const obj = {
inspect: () => ['foobarbazbing'],
}
expect(inspect(obj, { customInspect: true })).to.equal('1')
expect(inspect(obj, { customInspect: true, truncate: 15 })).to.equal("[ 'foobarba…' ]")
})

it('uses a custom deeply nested inspect function if `customInspect` is turned on', () => {
Expand All @@ -39,7 +46,7 @@ describe('objects', () => {
inspect: (depth, options) => options.stylize('Object content', 'string'),
},
}
expect(inspect(obj, { customInspect: true })).to.equal("{ sub: 'Object content' }")
expect(inspect(obj, { customInspect: true })).to.equal('{ sub: Object content }')
})

it('inspect with custom object-returning inspect', () => {
Expand Down

0 comments on commit 5e76830

Please sign in to comment.