Skip to content

Commit

Permalink
fix: ensure complex object keys are quoted
Browse files Browse the repository at this point in the history
  • Loading branch information
pcorpet committed Jun 10, 2021
1 parent 5db74c7 commit b4dea33
Show file tree
Hide file tree
Showing 3 changed files with 18,201 additions and 49 deletions.
14 changes: 12 additions & 2 deletions lib/object.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { inspectProperty, inspectList } from './helpers'

function quoteComplexKey(key) {
if (typeof key !== 'string' || key.match(/^[a-zA-Z_][a-zA-Z_0-9]*$/)) {
return key
}
return JSON.stringify(key)
.replace(/'/g, "\\'")
.replace(/\\"/g, '"')
.replace(/(^"|"$)/g, "'")
}

export default function inspectObject(object, options) {
const properties = Object.getOwnPropertyNames(object)
const symbols = Object.getOwnPropertySymbols ? Object.getOwnPropertySymbols(object) : []
Expand All @@ -8,12 +18,12 @@ export default function inspectObject(object, options) {
}
options.truncate -= 4
const propertyContents = inspectList(
properties.map(key => [key, object[key]]),
properties.map(key => [quoteComplexKey(key), object[key]]),
options,
inspectProperty
)
const symbolContents = inspectList(
symbols.map(key => [key, object[key]]),
symbols.map(key => [quoteComplexKey(key), object[key]]),
options,
inspectProperty
)
Expand Down

0 comments on commit b4dea33

Please sign in to comment.