(a: string, b: string)
| 44 | * to lexicographic compare so "0.1.4-beta" sorts before "0.1.4". |
| 45 | */ |
| 46 | export function compareVersions(a: string, b: string): -1 | 0 | 1 { |
| 47 | const [aMain, aPre] = splitVersion(a); |
| 48 | const [bMain, bPre] = splitVersion(b); |
| 49 | for (let i = 0; i < 3; i++) { |
| 50 | const ai = aMain[i] ?? 0; |
| 51 | const bi = bMain[i] ?? 0; |
| 52 | if (ai !== bi) return ai < bi ? -1 : 1; |
| 53 | } |
| 54 | // A version with no pre-release is greater than one with one. |
| 55 | if (aPre === bPre) return 0; |
| 56 | if (aPre === "") return 1; |
| 57 | if (bPre === "") return -1; |
| 58 | return aPre < bPre ? -1 : 1; |
| 59 | } |
| 60 | |
| 61 | function splitVersion(v: string): [number[], string] { |
| 62 | const [main, pre = ""] = v.split("-", 2); |
no test coverage detected