Skip to content

fix: avoid trailing comma in jsonStringify when replacer skips last property#120

Merged
jdesrosiers merged 1 commit into
hyperjump-io:mainfrom
Suyog241005:fix/json-stringify-trailing-comma
May 26, 2026
Merged

fix: avoid trailing comma in jsonStringify when replacer skips last property#120
jdesrosiers merged 1 commit into
hyperjump-io:mainfrom
Suyog241005:fix/json-stringify-trailing-comma

Conversation

@Suyog241005
Copy link
Copy Markdown
Contributor

Description

This PR fixes a bug in jsonStringify where omitting the last property of an object via a custom replacer function resulted in a trailing comma (producing invalid JSON, e.g. {"aaa":"foo",}).

Instead of using the raw entries index (entries[index + 1]) to append commas (which doesn't account for properties that get filtered out by the replacer), stringifyObject now dynamically prepends commas before writing any subsequent non-undefined property.

Key Changes:

  • lib/common.js: Refactored stringifyObject to dynamically prefix commas and converted the loop to a modern for-of loop to satisfy the project's @typescript-eslint/prefer-for-of linting rule.
  • lib/stringify.spec.ts: Added a dedicated unit test case validating that omitting the last property produces correct, valid JSON without a trailing comma.

Related Issues

Closes #119

@Suyog241005
Copy link
Copy Markdown
Contributor Author

Hi @jdesrosiers,

I've opened this PR with the fix.

It dynamically prefixes commas in stringifyObject to avoid trailing commas when properties are omitted. I also refactored it to a modern for-of loop to comply with the project's linter rules.

Copy link
Copy Markdown
Collaborator

@jdesrosiers jdesrosiers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks.

@jdesrosiers jdesrosiers merged commit 15833d9 into hyperjump-io:main May 26, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: jsonStringify produces trailing comma if replacer omits the last object property

2 participants