Skip to content

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

Open
Suyog241005 wants to merge 1 commit into
hyperjump-io:mainfrom
Suyog241005:fix/json-stringify-trailing-comma
Open

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

Conversation

@Suyog241005
Copy link
Copy Markdown

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
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.

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

1 participant