Skip to content
Open
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
8 changes: 5 additions & 3 deletions semver/greater_than_range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ function greaterThanComparatorSet(
version: SemVer,
comparatorSet: Comparator[],
): boolean {
// If the comparator set contains wildcard, then the semver is not greater than the range.
if (comparatorSet.some(isWildcardComparator)) return false;
const bounded = comparatorSet.filter(
(comparator) => !isWildcardComparator(comparator),
);
if (bounded.length === 0) return false;
// If the semver satisfies the comparator set, then it's not greater than the range.
if (testComparatorSet(version, comparatorSet)) return false;
// If the semver is less than any of the comparator set, then it's not greater than the range.
if (
comparatorSet.some((comparator) => lessThanComparator(version, comparator))
bounded.some((comparator) => lessThanComparator(version, comparator))
) return false;
return true;
}
Expand Down
1 change: 1 addition & 0 deletions semver/greater_than_range_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Deno.test("greaterThanRange() checks if the semver is greater than the range", a
["=0.7.x", "0.8.2"],
["<0.7.x", "0.7.2"],
["=0.1.0", "1.0.0"],
["* <1.2.1", "2.0.3"],
] as const;

for (const [range, version] of versionGtRange) {
Expand Down
10 changes: 5 additions & 5 deletions semver/less_than_range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ export function lessThanRange(version: SemVer, range: Range): boolean {
}

function lessThanComparatorSet(version: SemVer, comparatorSet: Comparator[]) {
// If the comparator set contains wildcard, then the semver is not greater than the range.
if (comparatorSet.some(isWildcardComparator)) return false;
const bounded = comparatorSet.filter(
(comparator) => !isWildcardComparator(comparator),
);
if (bounded.length === 0) return false;
// If the SemVer satisfies the comparator set, then it's not less than the range.
if (testComparatorSet(version, comparatorSet)) return false;
// If the SemVer is greater than any of the comparator set, then it's not less than the range.
if (
comparatorSet.some((comparator) =>
greaterThanComparator(version, comparator)
)
bounded.some((comparator) => greaterThanComparator(version, comparator))
) return false;
return true;
}
Expand Down
1 change: 1 addition & 0 deletions semver/less_than_range_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Deno.test("lessThanRange() checks if the SemVer is less than the range", async (
[">=0.7.x", "0.7.0-asdf"],
[">=0.7.x", "0.6.2"],
[">1.2.3", "1.3.0-alpha"],
["* >2.1.0", "0.2.1"],
] as const;

for (const [range, version] of versionLtRange) {
Expand Down
2 changes: 2 additions & 0 deletions semver/parse_range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,15 @@ function handleLessThanOperator(groups: RangeRegExpGroups): Comparator[] {
function handleLessThanOrEqualOperator(
groups: RangeRegExpGroups,
): Comparator[] {
const majorIsWildcard = isWildcard(groups.major);
const minorIsWildcard = isWildcard(groups.minor);
const patchIsWildcard = isWildcard(groups.patch);

const major = +groups.major;
const minor = +groups.minor;
const patch = +groups.patch;

if (majorIsWildcard) return [ALL];
if (minorIsWildcard) {
return [{ operator: "<", major: major + 1, minor: 0, patch: 0 }];
}
Expand Down
30 changes: 30 additions & 0 deletions semver/parse_range_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,36 @@ Deno.test("parseRange() handles wildcards", () => {
assertEquals(parseRange("<=1.*.2"), [
[{ operator: "<", major: 2, minor: 0, patch: 0 }],
]);
assertEquals(parseRange("<=*"), [
[{
operator: undefined,
major: NaN,
minor: NaN,
patch: NaN,
prerelease: [],
build: [],
}],
]);
assertEquals(parseRange("<=x"), [
[{
operator: undefined,
major: NaN,
minor: NaN,
patch: NaN,
prerelease: [],
build: [],
}],
]);
assertEquals(parseRange("<=X"), [
[{
operator: undefined,
major: NaN,
minor: NaN,
patch: NaN,
prerelease: [],
build: [],
}],
]);
assertEquals(parseRange(">=1.*.0"), [
[{ operator: ">=", major: 1, minor: 0, patch: 0 }],
]);
Expand Down
Loading