Skip to content

Commit

Permalink
Tweak rendering of object keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
danfuzz committed Nov 22, 2024
1 parent f69039a commit a6a3fa9
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/loggy-intf/private/HumanVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,24 @@ export class HumanVisitor extends BaseValueVisitor {
* @returns {TypeText} The rendered form.
*/
#renderKey(key) {
if ((typeof key === 'string') && /^[$_a-zA-Z][$_a-zA-Z0-9]*$/.test(key)) {
// It doesn't have to be quoted.
return `${key}:`;
if (typeof key === 'string') {
if (/^([$_a-zA-Z][$_a-zA-Z0-9]*|[1-9][0-9]{0,15}|0)$/.test(key)) {
// It doesn't have to be quoted.
return `${key}:`;
} else if (/^-[1-9][0-9]{0,15}$/.test(key)) {
// It can be treated like a negative numeric literal.
return `[${key}]:`;
} else {
// It needs to be quoted.
return `${util.inspect(key)}:`;
}
} else {
return new ComboText(this._impl_visitString(key), ':');
// A non-string, perhaps a symbol, however as of this writing this case
// isn't used.
return new ComboText(
'[', ComboText.NO_BREAK,
this._prot_visitSync(key),
ComboText.NO_BREAK, ']:');
}
}

Expand Down

0 comments on commit a6a3fa9

Please sign in to comment.