2024-04-03 06:22:18 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-10 02:43:44 +00:00
|
|
|
// This module is browser compatible.
|
2024-04-03 06:22:18 +00:00
|
|
|
|
|
|
|
import type { Comparator, Range, SemVer } from "./types.ts";
|
|
|
|
import { testComparatorSet } from "./_test_comparator_set.ts";
|
|
|
|
import { isWildcardComparator } from "./_shared.ts";
|
|
|
|
import { compare } from "./compare.ts";
|
|
|
|
|
2024-05-27 10:03:20 +00:00
|
|
|
/**
|
|
|
|
* Check if the SemVer is greater than the range.
|
|
|
|
*
|
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
|
|
|
* import { parse, parseRange, greaterThanRange } from "@std/semver";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assert } from "@std/assert";
|
2024-05-27 10:03:20 +00:00
|
|
|
*
|
2024-08-19 04:33:42 +00:00
|
|
|
* const version1 = parse("1.2.3");
|
|
|
|
* const version2 = parse("1.2.4");
|
2024-05-27 10:03:20 +00:00
|
|
|
* const range = parseRange(">=1.2.3 <1.2.4");
|
2024-06-28 05:47:34 +00:00
|
|
|
*
|
2024-08-19 04:33:42 +00:00
|
|
|
* assert(!greaterThanRange(version1, range));
|
|
|
|
* assert(greaterThanRange(version2, range));
|
2024-05-27 10:03:20 +00:00
|
|
|
* ```
|
|
|
|
*
|
2024-08-19 04:33:42 +00:00
|
|
|
* @param version The version to check.
|
2024-05-27 10:03:20 +00:00
|
|
|
* @param range The range to check against.
|
|
|
|
* @returns `true` if the semver is greater than the range, `false` otherwise.
|
|
|
|
*/
|
2024-08-19 04:33:42 +00:00
|
|
|
export function greaterThanRange(version: SemVer, range: Range): boolean {
|
2024-04-03 06:22:18 +00:00
|
|
|
return range.every((comparatorSet) =>
|
2024-08-19 04:33:42 +00:00
|
|
|
greaterThanComparatorSet(version, comparatorSet)
|
2024-04-03 06:22:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function greaterThanComparatorSet(
|
2024-08-19 04:33:42 +00:00
|
|
|
version: SemVer,
|
2024-04-03 06:22:18 +00:00
|
|
|
comparatorSet: Comparator[],
|
|
|
|
): boolean {
|
|
|
|
// If the comparator set contains wildcard, then the semver is not greater than the range.
|
|
|
|
if (comparatorSet.some(isWildcardComparator)) return false;
|
|
|
|
// If the semver satisfies the comparator set, then it's not greater than the range.
|
2024-08-19 04:33:42 +00:00
|
|
|
if (testComparatorSet(version, comparatorSet)) return false;
|
2024-04-03 06:22:18 +00:00
|
|
|
// If the semver is less than any of the comparator set, then it's not greater than the range.
|
|
|
|
if (
|
2024-08-19 04:33:42 +00:00
|
|
|
comparatorSet.some((comparator) => lessThanComparator(version, comparator))
|
2024-04-03 06:22:18 +00:00
|
|
|
) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-08-19 04:33:42 +00:00
|
|
|
function lessThanComparator(version: SemVer, comparator: Comparator): boolean {
|
|
|
|
const cmp = compare(version, comparator);
|
2024-04-03 06:22:18 +00:00
|
|
|
switch (comparator.operator) {
|
|
|
|
case "=":
|
|
|
|
case undefined:
|
|
|
|
return cmp < 0;
|
|
|
|
case "!=":
|
|
|
|
return false;
|
|
|
|
case ">":
|
|
|
|
return cmp <= 0;
|
|
|
|
case "<":
|
|
|
|
return false;
|
|
|
|
case ">=":
|
|
|
|
return cmp < 0;
|
|
|
|
case "<=":
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|