fix: avoid trailing comma in jsonStringify when replacer skips last property#120
Open
Suyog241005 wants to merge 1 commit into
Open
fix: avoid trailing comma in jsonStringify when replacer skips last property#120Suyog241005 wants to merge 1 commit into
Suyog241005 wants to merge 1 commit into
Conversation
Author
|
Hi @jdesrosiers, I've opened this PR with the fix. It dynamically prefixes commas in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes a bug in
jsonStringifywhere 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),stringifyObjectnow dynamically prepends commas before writing any subsequent non-undefined property.Key Changes:
lib/common.js: RefactoredstringifyObjectto dynamically prefix commas and converted the loop to a modernfor-ofloop to satisfy the project's@typescript-eslint/prefer-for-oflinting 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