2024-01-01 21:11:32 +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-03-01 06:43:28 +00:00
|
|
|
import { isWildcardComparator } from "./_shared.ts";
|
|
|
|
import { compare } from "./compare.ts";
|
2024-04-24 19:38:52 +00:00
|
|
|
import { satisfies } from "./satisfies.ts";
|
2024-02-09 23:30:39 +00:00
|
|
|
import type { Comparator, Range } from "./types.ts";
|
2023-06-12 03:47:03 +00:00
|
|
|
|
2024-03-01 06:43:28 +00:00
|
|
|
function comparatorIntersects(
|
2024-08-19 04:33:42 +00:00
|
|
|
comparator1: Comparator,
|
|
|
|
comparator2: Comparator,
|
2024-03-01 06:43:28 +00:00
|
|
|
): boolean {
|
2024-08-19 04:33:42 +00:00
|
|
|
const op0 = comparator1.operator;
|
|
|
|
const op1 = comparator2.operator;
|
2024-03-01 06:43:28 +00:00
|
|
|
|
2024-03-07 02:15:39 +00:00
|
|
|
if (op0 === undefined) {
|
2024-08-19 04:33:42 +00:00
|
|
|
// if comparator1 is empty comparator, then returns true
|
|
|
|
if (isWildcardComparator(comparator1)) return true;
|
|
|
|
return satisfies(comparator1, [[comparator2]]);
|
2024-03-01 06:43:28 +00:00
|
|
|
}
|
2024-03-07 02:15:39 +00:00
|
|
|
if (op1 === undefined) {
|
2024-08-19 04:33:42 +00:00
|
|
|
if (isWildcardComparator(comparator2)) return true;
|
|
|
|
return satisfies(comparator2, [[comparator1]]);
|
2024-03-01 06:43:28 +00:00
|
|
|
}
|
|
|
|
|
2024-08-19 04:33:42 +00:00
|
|
|
const cmp = compare(comparator1, comparator2);
|
2024-03-01 06:43:28 +00:00
|
|
|
|
|
|
|
const sameDirectionIncreasing = (op0 === ">=" || op0 === ">") &&
|
|
|
|
(op1 === ">=" || op1 === ">");
|
|
|
|
const sameDirectionDecreasing = (op0 === "<=" || op0 === "<") &&
|
|
|
|
(op1 === "<=" || op1 === "<");
|
|
|
|
const sameSemVer = cmp === 0;
|
|
|
|
const differentDirectionsInclusive = (op0 === ">=" || op0 === "<=") &&
|
|
|
|
(op1 === ">=" || op1 === "<=");
|
|
|
|
const oppositeDirectionsLessThan = cmp === -1 &&
|
|
|
|
(op0 === ">=" || op0 === ">") &&
|
|
|
|
(op1 === "<=" || op1 === "<");
|
|
|
|
const oppositeDirectionsGreaterThan = cmp === 1 &&
|
|
|
|
(op0 === "<=" || op0 === "<") &&
|
|
|
|
(op1 === ">=" || op1 === ">");
|
|
|
|
|
|
|
|
return sameDirectionIncreasing ||
|
|
|
|
sameDirectionDecreasing ||
|
|
|
|
(sameSemVer && differentDirectionsInclusive) ||
|
|
|
|
oppositeDirectionsLessThan ||
|
|
|
|
oppositeDirectionsGreaterThan;
|
|
|
|
}
|
|
|
|
|
2024-02-09 23:30:39 +00:00
|
|
|
function rangesSatisfiable(ranges: Range[]): boolean {
|
2023-06-12 03:47:03 +00:00
|
|
|
return ranges.every((r) => {
|
|
|
|
// For each OR at least one AND must be satisfiable
|
2024-02-09 23:30:39 +00:00
|
|
|
return r.some((comparators) => comparatorsSatisfiable(comparators));
|
2023-06-12 03:47:03 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-01-05 02:17:15 +00:00
|
|
|
function comparatorsSatisfiable(comparators: Comparator[]): boolean {
|
2023-06-12 03:47:03 +00:00
|
|
|
// Comparators are satisfiable if they all intersect with each other
|
|
|
|
for (let i = 0; i < comparators.length - 1; i++) {
|
2024-08-19 04:33:42 +00:00
|
|
|
const comparator1 = comparators[i]!;
|
|
|
|
for (const comparator2 of comparators.slice(i + 1)) {
|
|
|
|
if (!comparatorIntersects(comparator1, comparator2)) {
|
2023-06-12 03:47:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-06-28 05:47:34 +00:00
|
|
|
* The ranges intersect every range of AND comparators intersects with a least
|
|
|
|
* one range of OR ranges.
|
2024-05-27 10:03:20 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
|
|
|
* import { parseRange, rangeIntersects } 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 range1 = parseRange(">=1.0.0 <2.0.0");
|
|
|
|
* const range2 = parseRange(">=1.0.0 <1.2.3");
|
|
|
|
* const range3 = parseRange(">=1.2.3 <2.0.0");
|
2024-05-27 10:03:20 +00:00
|
|
|
*
|
2024-08-19 04:33:42 +00:00
|
|
|
* assert(rangeIntersects(range1, range2));
|
|
|
|
* assert(rangeIntersects(range1, range3));
|
|
|
|
* assert(!rangeIntersects(range2, range3));
|
2024-05-27 10:03:20 +00:00
|
|
|
* ```
|
|
|
|
*
|
2024-08-19 04:33:42 +00:00
|
|
|
* @param range1 range 0
|
|
|
|
* @param range2 range 1
|
2024-05-27 10:03:20 +00:00
|
|
|
* @returns returns true if the given ranges intersect, false otherwise
|
2023-06-12 03:47:03 +00:00
|
|
|
*/
|
2024-08-19 04:33:42 +00:00
|
|
|
export function rangeIntersects(range1: Range, range2: Range): boolean {
|
|
|
|
return rangesSatisfiable([range1, range2]) &&
|
|
|
|
range1.some((range10) => {
|
|
|
|
return range2.some((r11) => {
|
|
|
|
return range10.every((comparator1) => {
|
|
|
|
return r11.every((comparator2) =>
|
|
|
|
comparatorIntersects(comparator1, comparator2)
|
|
|
|
);
|
2024-01-14 21:19:00 +00:00
|
|
|
});
|
2023-06-12 03:47:03 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|