From a74f5162525ac492ca010ea0168f056a679318c9 Mon Sep 17 00:00:00 2001 From: Art <4998038+Alorel@users.noreply.github.com> Date: Thu, 21 Sep 2023 19:43:34 +0100 Subject: [PATCH] fix: Favour v1.0 to 1.0 in last tag resolution --- src/lib/semver.mts | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) 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;