fix: keep an empty-string key on unflatten instead of renaming it to 0 - #193
Open
spokodev wants to merge 1 commit into
Open
fix: keep an empty-string key on unflatten instead of renaming it to 0#193spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
`getkey` coerces a key to a number when it is numeric, guarding only
against NaN. But `Number('')` is `0` (not NaN), so a top-level empty-string
key was turned into the index 0, breaking the documented round-trip:
unflatten(flatten({ '': 1, x: 2 })) // { '0': 1, x: 2 }, expected { '': 1, x: 2 }
unflatten(flatten({ a: { '': 1 } })) // { a: [1] }
flatten preserves `''` losslessly, so only unflatten corrupted it. Return
the empty string as-is before the numeric coercion. Added a round-trip test.
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.
getkeycoerces a key to a number when it looks numeric, guarding only againstNaN. ButNumber("")is0(notNaN), so a top-level empty-string key is turned into the index0, which breaks the documentedunflatten(flatten(obj))round-trip:flattenpreserves""losslessly (the key has no delimiter), so onlyunflattencorrupts it. The fix returns the empty string as-is before the numeric coercion:Numeric-index keys are unaffected (verified
unflatten({"0":"a","1":"b"})is unchanged). Full suite passes (51 + the new round-trip test);standardlint clean.