Skip to content

Commit

Permalink
Further improve natural sorting of classes (#13532)
Browse files Browse the repository at this point in the history
* skip initial character we just saw

Thanks Richard for noticing!

Co-authored-by: Richard van Velzen <richard@frank.nl>

* prevent calling `charCodeAt()` if we already computed the value

* update changelog

---------

Co-authored-by: Richard van Velzen <richard@frank.nl>
  • Loading branch information
RobinMalfait and rvanvelzen committed Apr 16, 2024
1 parent cd0c308 commit cc8c069
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -14,7 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Changed

- Use `rem` units for breakpoints by default instead of `px` ([#13469](https://github.com/tailwindlabs/tailwindcss/pull/13469))
- Use natural sorting when sorting classes ([#13507](https://github.com/tailwindlabs/tailwindcss/pull/13507))
- Use natural sorting when sorting classes ([#13507](https://github.com/tailwindlabs/tailwindcss/pull/13507), [#13532](https://github.com/tailwindlabs/tailwindcss/pull/13532))

## [4.0.0-alpha.14] - 2024-04-09

Expand Down
10 changes: 6 additions & 4 deletions packages/tailwindcss/src/utils/compare.ts
Expand Up @@ -20,15 +20,17 @@ export function compare(a: string, z: string) {
// If both are numbers, compare them as numbers instead of strings.
if (aCode >= ZERO && aCode <= NINE && zCode >= ZERO && zCode <= NINE) {
let aStart = i
let aEnd = i
let aEnd = i + 1
let zStart = i
let zEnd = i
let zEnd = i + 1

// Consume the number
while (a.charCodeAt(aEnd) >= ZERO && a.charCodeAt(aEnd) <= NINE) aEnd++
aCode = a.charCodeAt(aEnd)
while (aCode >= ZERO && aCode <= NINE) aCode = a.charCodeAt(++aEnd)

// Consume the number
while (z.charCodeAt(zEnd) >= ZERO && z.charCodeAt(zEnd) <= NINE) zEnd++
zCode = z.charCodeAt(zEnd)
while (zCode >= ZERO && zCode <= NINE) zCode = z.charCodeAt(++zEnd)

let aNumber = a.slice(aStart, aEnd)
let zNumber = z.slice(zStart, zEnd)
Expand Down

0 comments on commit cc8c069

Please sign in to comment.