Skip to content

Commit

Permalink
fix: make <=11 work the same as in npm
Browse files Browse the repository at this point in the history
The current version of `<=` in this crate does not fully match npm.

For example, `<=11` currently is equivalent to `<=11.0.0-0` however in npm `<=11` is equivalent to `<12` (or `<=11.MAX_SAFE_INTEGER.MAX_SAFE_INTEGER`)

Example application:
```rust
use nodejs_semver::{Range, Version};

fn main() {
    let range = "<=11";
    let version = "11.0.0";
    let req: Range = range.parse().expect(&format!("err: {}", range));
    let version: Version = version.parse().expect(&format!("err: {}", version));
    println!("result: {}", version.satisfies(&req));
    println!("result: {}", req.satisfies(&version));
}
```

running the above example without this patch:
```
result: false
result: false
```

running the above example with this patch:
```
result: true
result: true
```
  • Loading branch information
JakeChampion authored Sep 27, 2023
1 parent fb12896 commit f419421
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use nom::multi::{many_till, separated_list0};
use nom::sequence::{delimited, preceded, terminated, tuple};
use nom::{Err, IResult};

use crate::{extras, number, Identifier, SemverError, SemverErrorKind, SemverParseError, Version};
use crate::{extras, number, Identifier, SemverError, SemverErrorKind, SemverParseError, Version, MAX_SAFE_INTEGER};

#[derive(Clone, Debug, Eq, PartialEq, Hash)]
struct BoundSet {
Expand Down Expand Up @@ -633,6 +633,17 @@ fn primitive(input: &str) -> IResult<&str, Option<BoundSet>, SemverParseError<&s
build,
pre_release,
})),
(
LessThanEquals,
Partial {
major,
minor: None,
patch: None,
..
},
) => BoundSet::at_most(Predicate::Including(
(major.unwrap_or(0), MAX_SAFE_INTEGER, MAX_SAFE_INTEGER).into(),
)),
(
LessThanEquals,
Partial {
Expand All @@ -642,7 +653,7 @@ fn primitive(input: &str) -> IResult<&str, Option<BoundSet>, SemverParseError<&s
..
},
) => BoundSet::at_most(Predicate::Including(
(major.unwrap_or(0), minor.unwrap_or(0), 0, 0).into(),
(major.unwrap_or(0), minor.unwrap_or(0), MAX_SAFE_INTEGER).into(),
)),
(LessThanEquals, partial) => {
BoundSet::at_most(Predicate::Including(partial.into()))
Expand Down

0 comments on commit f419421

Please sign in to comment.