Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Respect numeric separators with natural sorting #108

Open
2 tasks done
nnmrts opened this issue Feb 22, 2024 · 0 comments
Open
2 tasks done

Feature: Respect numeric separators with natural sorting #108

nnmrts opened this issue Feb 22, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@nnmrts
Copy link

nnmrts commented Feb 22, 2024

What rule do you want to change?

sort-array-includes, sort-maps, sort-objects

Describe the problem

When using numeric separators in numbers as keys, the natural order obviously breaks. For example, while 100 will be sorted after 2, 1_000 or 1_000_000 or "10,000" will not.

Code example

This issue is actually kind of two in one:

  • Number literals with numeric separators like 1_000 or 1_000_000
    • This is hopefully simpler to implement, just ignore the underscores.
    • Example:
     const ordinals = new Map([
     	[1, "first"],
     	[2, "second"],
     	[3, "third"],
     	[100, "hundredth"],
     	[1_000, "thousandth"],
     	[1_000_000, "millionth"]
     ]);
  • Regular number strings with arbitrary separators like "10,000" or "1.000.000" or "100 000"
    • This probably harder to implement but maybe could be done with a configurable option to specify which characters between numbers in keys should be ignored in a string. Or maybe just a simple on an off switch for common separators.
    • Example:
     const ordinals = {
     	"1": "first",
     	"2": "second",
     	"3": "third",
     	"100": "hundredth",
     	"1,000": "thousandth",
     	"1 000 000": "millionth",
     }

Additional comments

I think natural sorting for the rules sort-array-includes, sort-maps and sort-objects should respect number literal separators by default, and for strings there should be some kind of option.

Especially sort-array-includes and sort-maps would profit from this since they have no partition-by-comment or partition-by-new-line options, so the only "workarounds" right now are either to not use separators at all (which are enforced by unicorn/numeric-separators-style in my case at least) or to disable the rule.

Validations

  • Read the docs.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
@nnmrts nnmrts added the enhancement New feature or request label Feb 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant