Last active
October 2, 2024 09:14
-
-
Save iwill/a83038623ba4fef6abb9efca87ae9ccb to your computer and use it in GitHub Desktop.
JavaScript - Comparison of Semantic Versioning
This file contains 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
/** | |
* Semantic Versioning Comparing | |
* #see https://semver.org/ | |
* #see https://stackoverflow.com/a/65687141/456536 | |
* #see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/Collator#options | |
*/ | |
function semverCompare(a, b) { | |
if (a.startsWith(b + "-")) return -1 | |
if (b.startsWith(a + "-")) return 1 | |
return a.localeCompare(b, undefined, { numeric: true, sensitivity: "case", caseFirst: "upper" }) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test function
Test cases
The results with ❌ are known issues which I prefer not to fix.
See also the comparing results from semvercompare.azurewebsites.net