Skip to content

Sort retrieved library versions in descending order#153

Open
MridulTailor wants to merge 1 commit intocdnjs:masterfrom
MridulTailor:sort-library-versions
Open

Sort retrieved library versions in descending order#153
MridulTailor wants to merge 1 commit intocdnjs:masterfrom
MridulTailor:sort-library-versions

Conversation

@MridulTailor
Copy link

Type of Change

  • Utilities: Added src/utils/sort.js which is a sortVersions helper that sorts versions in descending order (newest first), handling valid semver, coercible, and non-semver strings. Integrated into libraryVersions in src/utils/kvMetadata.js.
  • Tests: Added src/utils/sort.spec.js with 10 tests covering semver, pre-release, non-semver, mixed, and edge cases.

What issue does this relate to?

Closes #49

What should this PR do?

  • Sort the versions array in API responses in descending order (newest first)
  • Handle valid semver via semver.rcompare
  • Prioritize valid semver over non-semver (e.g. 1.0.0 before 2.0rc1)
  • Fall back to semver.coerce for non-semver strings where possible
  • Fall back to localeCompare with numeric collation for fully non-parseable
    strings (e.g. release-1.10 before release-1.9)

What are the acceptance criteria?

  • The versions field in API responses (e.g. /libraries/highlight.js?fields=versions) is sorted newest-first
  • Libraries with non-semver version strings are handled without errors
  • All 10 new tests pass
  • Full test suite passes (430 tests -> 420 existing tests + 10 new tests)
  • Lint passes with 0 errors

…r) and apply it to library version retrieval
@MridulTailor
Copy link
Author

How i verified these changes work:

❯ curl "http://localhost:8787/libraries/highlight.js?fields=versions"
{"versions":["11.11.1","11.11.0","11.10.0","11.9.0","11.8.0","11.7.0","11.6.0","11.5.1","11.5.0","11.4.0","11.3.1","11.3.0","11.2.0","11.1.0","11.0.1","11.0.0","11.0.0-beta1","11.0.0-beta0","11.0.0-alpha1","11.0.0-alpha0","10.7.3","10.7.2","10.7.1","10.7.0","10.6.0","10.5.0","10.4.1","10.4.0","10.4.0-beta1","10.4.0-beta0","10.3.2","10.3.1","10.3.0","10.2.1","10.2.0","10.1.2","10.1.1","10.1.0","10.0.3","10.0.2","10.0.1","10.0.0","9.18.5","9.18.4","9.18.3","9.18.2","9.18.1","9.18.0","9.17.1","9.17.0","9.16.2","9.16.1","9.15.10","9.15.9","9.15.8","9.15.7","9.15.6","9.15.5","9.15.4","9.15.2","9.15.1","9.14.2","9.14.1","9.13.1","9.13.0","9.12.0","9.11.0","9.10.0","9.9.0","9.8.0","9.7.0","9.6.0","9.5.0","9.4.0","9.3.0","9.2.0","9.1.0","9.0.0","8.9.1","8.9.0","8.8.0","0.0.0-test0","8.7","8.6","8.5","8.4","8.3","8.2","8.1","8.0","7.5","7.4","7.3"]}%

❯ curl "https://api.cdnjs.com/libraries/highlight.js?fields=versions"
{"versions":["0.0.0-test0","10.0.0","10.0.1","10.0.2","10.0.3","10.1.0","10.1.1","10.1.2","10.2.0","10.2.1","10.3.0","10.3.1","10.3.2","10.4.0","10.4.0-beta0","10.4.0-beta1","10.4.1","10.5.0","10.6.0","10.7.0","10.7.1","10.7.2","10.7.3","11.0.0","11.0.0-alpha0","11.0.0-alpha1","11.0.0-beta0","11.0.0-beta1","11.0.1","11.1.0","11.10.0","11.11.0","11.11.1","11.2.0","11.3.0","11.3.1","11.4.0","11.5.0","11.5.1","11.6.0","11.7.0","11.8.0","11.9.0","7.3","7.4","7.5","8.0","8.1","8.2","8.3","8.4","8.5","8.6","8.7","8.8.0","8.9.0","8.9.1","9.0.0","9.1.0","9.10.0","9.11.0","9.12.0","9.13.0","9.13.1","9.14.1","9.14.2","9.15.1","9.15.10","9.15.2","9.15.4","9.15.5","9.15.6","9.15.7","9.15.8","9.15.9","9.16.1","9.16.2","9.17.0","9.17.1","9.18.0","9.18.1","9.18.2","9.18.3","9.18.4","9.18.5","9.2.0","9.3.0","9.4.0","9.5.0","9.6.0","9.7.0","9.8.0","9.9.0"]}%

@MridulTailor MridulTailor marked this pull request as ready for review February 18, 2026 22:33
@MattIPv4 MattIPv4 changed the title Implement a version sorting utility (sort versions in descending order) and apply it to library version retrieval Sort retrieved library versions in descending order Feb 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Version list seems entirely random. Could it be properly sorted?

1 participant

Comments