From a2fd2d41382fbe8c205651163104cefc03d57a6c Mon Sep 17 00:00:00 2001 From: Trim21 Date: Thu, 20 Feb 2025 16:28:57 +0800 Subject: [PATCH] feat: add Version.compare to WASM Version (#1080) --- js-rattler/crate/version.rs | 10 +++++++++- js-rattler/package-lock.json | 5 +++-- js-rattler/src/Version.test.ts | 8 ++++++++ js-rattler/src/Version.ts | 9 +++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/js-rattler/crate/version.rs b/js-rattler/crate/version.rs index ef31067f4..4fa4f4f47 100644 --- a/js-rattler/crate/version.rs +++ b/js-rattler/crate/version.rs @@ -1,6 +1,6 @@ use crate::JsResult; use rattler_conda_types::{Version, VersionBumpType}; -use std::str::FromStr; +use std::{cmp::Ordering, str::FromStr}; use wasm_bindgen::prelude::wasm_bindgen; #[wasm_bindgen] @@ -130,4 +130,12 @@ impl JsVersion { pub fn equals(&self, other: &Self) -> bool { self.as_ref() == other.as_ref() } + + pub fn compare(&self, other: &Self) -> i8 { + match self.as_ref().cmp(other.as_ref()) { + Ordering::Less => -1, + Ordering::Equal => 0, + Ordering::Greater => 1, + } + } } diff --git a/js-rattler/package-lock.json b/js-rattler/package-lock.json index 973d0a287..599bac165 100644 --- a/js-rattler/package-lock.json +++ b/js-rattler/package-lock.json @@ -1,12 +1,13 @@ { - "name": "rattler", + "name": "@baszalmstra/rattler", "version": "0.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "rattler", + "name": "@baszalmstra/rattler", "version": "0.1.0", + "license": "BSD-3-Clause", "devDependencies": { "@jest/globals": "^29.7.0", "@wasm-tool/wasm-pack-plugin": "1.7.0", diff --git a/js-rattler/src/Version.test.ts b/js-rattler/src/Version.test.ts index 5a3912a65..436b3af69 100644 --- a/js-rattler/src/Version.test.ts +++ b/js-rattler/src/Version.test.ts @@ -251,4 +251,12 @@ describe("Version", () => { expect(new Version("1.2.3a").withAlpha().toString()).toBe("1.2.3a"); }); }); + + describe("compare", () => { + it("should compare version with expected order", () => { + expect(new Version("1.2.3").compare(new Version("1.2.3"))).toBe(0); + expect(new Version("1.2.0").compare(new Version("1.2.3"))).toBe(-1); + expect(new Version("1.2.4").compare(new Version("1.2.3"))).toBe(1); + }); + }); }); diff --git a/js-rattler/src/Version.ts b/js-rattler/src/Version.ts index 92afc12fb..42a28b1c0 100644 --- a/js-rattler/src/Version.ts +++ b/js-rattler/src/Version.ts @@ -195,4 +195,13 @@ export class Version { public withAlpha(): Version { return Version.fromNative(this.native.with_alpha()); } + + /** + * Compare two versions. + * + * Returns `-1` if this < other, `0` if this == other, `1` if this > other + */ + public compare(other: Version): number { + return this.native.compare(other.native); + } }