(a, b)
| 7 | * equality: 0 if identical, non-zero otherwise. |
| 8 | */ |
| 9 | export function compareVersions(a, b) { |
| 10 | if (!a || !b) return 0; |
| 11 | const normalize = (v) => v.replace(/^v/, ''); |
| 12 | const na = normalize(a); |
| 13 | const nb = normalize(b); |
| 14 | const isPrerelease = (v) => v.split('.').some((p) => !/^\d+$/.test(p)); |
| 15 | if (isPrerelease(na) || isPrerelease(nb)) { |
| 16 | return na === nb ? 0 : 1; |
| 17 | } |
| 18 | const pa = na.split('.').map(Number); |
| 19 | const pb = nb.split('.').map(Number); |
| 20 | for (let i = 0; i < Math.max(pa.length, pb.length); i++) { |
| 21 | const diff = (pa[i] || 0) - (pb[i] || 0); |
| 22 | if (diff !== 0) return diff; |
| 23 | } |
| 24 | return 0; |
| 25 | } |
no test coverage detected