From a6a3fa9cc0661715d0609cf16ecef6fdf6ab07e5 Mon Sep 17 00:00:00 2001 From: Dan Bornstein Date: Fri, 22 Nov 2024 11:44:22 -0800 Subject: [PATCH] Tweak rendering of object keys. --- src/loggy-intf/private/HumanVisitor.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/loggy-intf/private/HumanVisitor.js b/src/loggy-intf/private/HumanVisitor.js index d2096509..443fc080 100644 --- a/src/loggy-intf/private/HumanVisitor.js +++ b/src/loggy-intf/private/HumanVisitor.js @@ -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, ']:'); } }