Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Version.compare to wasm Version #1080

Merged
merged 4 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion js-rattler/crate/version.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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,
}
}
}
5 changes: 3 additions & 2 deletions js-rattler/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions js-rattler/src/Version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
9 changes: 9 additions & 0 deletions js-rattler/src/Version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}