Skip to content

Commit

Permalink
Human logging of object keys (#440)
Browse files Browse the repository at this point in the history
This PR tweaks the human-form rendering of object keys, to expand the
use of un-quoted forms when possible. It also drops styling
(colorization) of quoted strings, which I think made them harder to read
especially in contexts where the values are also literal strings.
  • Loading branch information
danfuzz authored Nov 22, 2024
2 parents f69039a + 7b30c38 commit 45ecb40
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ Other notable changes:
* general:
* Allow node version 23.
* `loggy-intf` / `loggy`:
* Made several improvements to "human" (non-JSON) log rendering, including
fixing it to be able to log values with reference cycles.
* Improved the data model used to encode logged items, including:
* Representing functions and classes as structured objects instead of just
strings.
* Making it possible to encode values with reference cycles.
* Improved "human" (non-JSON) log rendering, including:
* Correctly rendering shared references.
* Tweaking the styling for readability.
* `structy`:
* Started allowing any object (plain or not) to be used as the argument to the
`BaseStruct` constructor.
Expand Down
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 45ecb40

Please sign in to comment.