Skip to content

Instantly share code, notes, and snippets.

@rivy
Forked from iwill/semverCompare.js
Created August 3, 2022 02:58
Show Gist options
  • Save rivy/f366f44069dfb99a4e2ba3d8decc3fb0 to your computer and use it in GitHub Desktop.
Save rivy/f366f44069dfb99a4e2ba3d8decc3fb0 to your computer and use it in GitHub Desktop.
JavaScript - Comparison of Semantic Versioning
/**
* 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