fix: sort ALL_CAPS keys before TitleCase and lowercase in destructure - #338
Open
0x5t4l1n wants to merge 1 commit into
Open
fix: sort ALL_CAPS keys before TitleCase and lowercase in destructure#3380x5t4l1n wants to merge 1 commit into
0x5t4l1n wants to merge 1 commit into
Conversation
Resolves mthadley#300. naturalCompare alone sorts by ASCII value, placing capitalised keys (B=66) before all-caps ones that start lower (F=70). Add a capGroup() function inside createSorter() that buckets names into ALL_CAPS (0) → TitleCase (1) → lowercase (2) before falling through to the existing naturalCompare, ensuring e.g. { FOO, Bar, baz } is the canonical order regardless of caseSensitive setting.
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.
Summary
Fixes #300 — ALL_CAPS keys (e.g.
FOO,MAX_VALUE) now sort before TitleCase keys (e.g.Bar), which sort before lowercase keys (e.g.baz).Root cause:
createSorter()delegated entirely tonaturalCompare. In ASCII,B (66) < F (70), soBarsorted beforeFOO. The fix adds acapGroup()helper that buckets names into three tiers before falling through tonaturalCompare.Before:
After:
Works correctly with both
caseSensitive: true(default) andcaseSensitive: false.Test plan
npm test{ FOO, Bar, baz }ordering intests/lib/rules/sort-destructure-keys.js