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 less than the range.
|
|
|
|
*
|
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
|
|
|
* import { parse, parseRange, lessThanRange } 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.0.0");
|
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(!lessThanRange(version1, range));
|
|
|
|
* assert(lessThanRange(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 less than the range, `false` otherwise.
|
|
|
|
*/
|
2024-08-19 04:33:42 +00:00
|
|
|
export function lessThanRange(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
|
|
|
lessThanComparatorSet(version, comparatorSet)
|
2024-04-03 06:22:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-08-19 04:33:42 +00:00
|
|
|
function lessThanComparatorSet(version: SemVer, comparatorSet: Comparator[]) {
|
2024-04-03 06:22:18 +00:00
|
|
|
// If the comparator set contains wildcard, then the semver is not greater than the range.
|
|
|
|
if (comparatorSet.some(isWildcardComparator)) return false;
|
2024-05-27 10:03:20 +00:00
|
|
|
// If the SemVer satisfies the comparator set, then it's not less than the range.
|
2024-08-19 04:33:42 +00:00
|
|
|
if (testComparatorSet(version, comparatorSet)) return false;
|
2024-05-27 10:03:20 +00:00
|
|
|
// If the SemVer is greater than any of the comparator set, then it's not less than the range.
|
2024-04-03 06:22:18 +00:00
|
|
|
if (
|
|
|
|
comparatorSet.some((comparator) =>
|
2024-08-19 04:33:42 +00:00
|
|
|
greaterThanComparator(version, comparator)
|
2024-04-03 06:22:18 +00:00
|
|
|
)
|
|
|
|
) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function greaterThanComparator(
|
2024-08-19 04:33:42 +00:00
|
|
|
version: SemVer,
|
2024-04-03 06:22:18 +00:00
|
|
|
comparator: Comparator,
|
|
|
|
): boolean {
|
2024-08-19 04:33:42 +00:00
|
|
|
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 false;
|
|
|
|
case "<":
|
|
|
|
return cmp >= 0;
|
|
|
|
case ">=":
|
|
|
|
return false;
|
|
|
|
case "<=":
|
|
|
|
return cmp > 0;
|
|
|
|
}
|
|
|
|
}
|