mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
4df9f27f13
* initial commit * Update semver/comparator_format.ts Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com> * bump deprecation note --------- Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
import type { Operator, SemVer } from "./types.ts";
|
|
import { ANY, INVALID, MAX } from "./constants.ts";
|
|
|
|
/**
|
|
* The maximum version that could match this comparator.
|
|
*
|
|
* If an invalid comparator is given such as <0.0.0 then
|
|
* an out of range semver will be returned.
|
|
* @returns the version, the MAX version or the next smallest patch version
|
|
*
|
|
* @deprecated (will be removed in 0.214.0) Use {@linkcode rangeMax} instead.
|
|
*/
|
|
export function comparatorMax(semver: SemVer, operator: Operator): SemVer {
|
|
if (semver === ANY) {
|
|
return MAX;
|
|
}
|
|
switch (operator) {
|
|
case "!=":
|
|
case "!==":
|
|
case ">":
|
|
case ">=":
|
|
return MAX;
|
|
case "":
|
|
case "=":
|
|
case "==":
|
|
case "===":
|
|
case "<=":
|
|
return semver;
|
|
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;
|
|
} else {
|
|
return {
|
|
major,
|
|
minor: minor >= 0 ? minor : Number.POSITIVE_INFINITY,
|
|
patch: patch >= 0 ? patch : Number.POSITIVE_INFINITY,
|
|
prerelease: [],
|
|
build: [],
|
|
};
|
|
}
|
|
}
|
|
}
|
|
}
|