Skip to content

Commit

Permalink
feat: add Version.compare to WASM Version (#1080)
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 authored Feb 20, 2025
1 parent 2899cdd commit a2fd2d4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
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);
}
}

0 comments on commit a2fd2d4

Please sign in to comment.