/
range_max.ts
67 lines (64 loc) · 2.18 KB
/
range_max.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { INVALID, MAX } from "./constants.ts";
import { satisfies } from "./satisfies.ts";
import type { Comparator, Range, SemVer } from "./types.ts";
import { greaterThan } from "./greater_than.ts";
import { isWildcardComparator } from "./_shared.ts";
function comparatorMax(comparator: Comparator): SemVer {
const semver = comparator;
if (isWildcardComparator(comparator)) return MAX;
switch (comparator.operator) {
case "!=":
case ">":
case ">=":
return MAX;
case undefined:
case "=":
case "<=":
return {
major: semver.major,
minor: semver.minor,
patch: semver.patch,
prerelease: semver.prerelease,
build: semver.build,
};
case "<": {
const patch = semver.patch - 1;
const minor = patch >= 0 ? semver.minor : semver.minor - 1;
const major = minor >= 0 ? semver.major : semver.major - 1;
// if you try to do <0.0.0 it will Give you -∞.∞.∞
// which means no SemVer can compare successfully to it.
if (major < 0) return INVALID;
return {
major,
minor: minor >= 0 ? minor : Number.POSITIVE_INFINITY,
patch: patch >= 0 ? patch : Number.POSITIVE_INFINITY,
prerelease: [],
build: [],
};
}
}
}
/**
* @deprecated This will be removed in 1.0.0. Use {@linkcode greaterThanRange} or
* {@linkcode lessThanRange} for comparing ranges and semvers. The maximum
* version of a range is often not well defined, and therefore this API
* shouldn't be used. See
* {@link https://github.com/denoland/deno_std/issues/4365} for details.
*
* The maximum valid SemVer for a given range or INVALID
* @param range The range to calculate the max for
* @returns A valid SemVer or INVALID
*/
export function rangeMax(range: Range): SemVer {
let max;
for (const comparators of range) {
for (const comparator of comparators) {
const candidate = comparatorMax(comparator);
if (!satisfies(candidate, range)) continue;
max = (max && greaterThan(max, candidate)) ? max : candidate;
}
}
return max ?? INVALID;
}