diff --git a/src/lib/semver.mts b/src/lib/semver.mts index f62562b..7fcdf2e 100644 --- a/src/lib/semver.mts +++ b/src/lib/semver.mts @@ -18,14 +18,13 @@ class SemVer { this.#patch = fmtNum(patch); } - public static cmp(a: SemVer | undefined, b: SemVer | undefined): 1 | 0 | -1 { - if (a == null) { - return b == null ? 0 : -1; - } else if (b == null) { - return -1; - } - - return cmpNum(a.major, b.major) ?? cmpNum(a.#minor, b.#minor) ?? cmpNum(a.#patch, b.#patch) ?? 0; + /** Sorts in descending order, i.e. a higher version will come before a lower version */ + public static cmp(a: SemVer, b: SemVer): 1 | 0 | -1 { + return cmpNum(a.major, b.major) + ?? cmpNum(a.#minor, b.#minor) + ?? cmpNum(a.#patch, b.#patch) + ?? cmpPrefixed(a.prefixed, b.prefixed) + ?? 0; } public static parse(from: string, allowStrippingVPrefix = false): SemVer | undefined { @@ -175,6 +174,14 @@ function fmtNum(num: number | string | undefined): number | undefined { } } +function cmpPrefixed(a: boolean, b: boolean): 1 | -1 | undefined { + if (a && !b) { + return -1; + } else if (!a && b) { + return 1; + } +} + function cmpNum(a: number | undefined, b: number | undefined): 1 | -1 | undefined { if (a == null) { return b == null ? undefined : 1;