versions.ts 660 B

123456789101112131415161718192021
  1. /**
  2. * Semantic Versioning Comparing
  3. * #see https://semver.org/
  4. * #see https://stackoverflow.com/a/65687141/456536
  5. * #see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/Collator#options
  6. * Taken from https://gist.github.com/iwill/a83038623ba4fef6abb9efca87ae9ccb
  7. * returns -1 for smaller, 0 for equals, and 1 for greater than
  8. */
  9. export function semverCompare(a: string, b: string): number {
  10. if (a.startsWith(b + '-')) {
  11. return -1;
  12. }
  13. if (b.startsWith(a + '-')) {
  14. return 1;
  15. }
  16. return a.localeCompare(b, undefined, {
  17. numeric: true,
  18. sensitivity: 'case',
  19. caseFirst: 'upper',
  20. });
  21. }