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-05 00:00:55 +00:00
|
|
|
|
2024-03-01 06:43:28 +00:00
|
|
|
import {
|
|
|
|
COMPARATOR_REGEXP,
|
|
|
|
OPERATOR_XRANGE_REGEXP,
|
2024-03-05 00:00:55 +00:00
|
|
|
parseBuild,
|
2024-03-01 06:43:28 +00:00
|
|
|
parseNumber,
|
2024-03-05 00:00:55 +00:00
|
|
|
parsePrerelease,
|
2024-03-01 06:43:28 +00:00
|
|
|
XRANGE,
|
|
|
|
} from "./_shared.ts";
|
2024-07-30 02:31:20 +00:00
|
|
|
import { ALL, ANY } from "./_constants.ts";
|
2024-03-05 00:00:55 +00:00
|
|
|
import type { Comparator, Operator, Range } from "./types.ts";
|
2023-06-12 03:47:03 +00:00
|
|
|
|
2024-03-01 06:43:28 +00:00
|
|
|
type ComparatorRegExpGroup = {
|
|
|
|
operator: Operator;
|
|
|
|
major: string;
|
|
|
|
minor: string;
|
|
|
|
patch: string;
|
|
|
|
prerelease: string;
|
|
|
|
buildmetadata: string;
|
|
|
|
};
|
|
|
|
|
2024-07-30 02:31:20 +00:00
|
|
|
function parseComparator(comparator: string): Comparator | null {
|
2024-03-01 06:43:28 +00:00
|
|
|
const match = comparator.match(COMPARATOR_REGEXP);
|
|
|
|
const groups = match?.groups;
|
|
|
|
|
2024-07-30 02:31:20 +00:00
|
|
|
if (!groups) return null;
|
2024-03-01 06:43:28 +00:00
|
|
|
|
|
|
|
const { operator, prerelease, buildmetadata } =
|
|
|
|
groups as ComparatorRegExpGroup;
|
|
|
|
|
|
|
|
const semver = groups.major
|
|
|
|
? {
|
2024-08-23 03:39:56 +00:00
|
|
|
major: parseNumber(
|
|
|
|
groups.major,
|
|
|
|
`Cannot parse comparator ${comparator}: invalid major version`,
|
|
|
|
),
|
2024-03-01 06:43:28 +00:00
|
|
|
minor: parseNumber(
|
|
|
|
groups.minor!,
|
2024-08-23 03:39:56 +00:00
|
|
|
`Cannot parse comparator ${comparator}: invalid minor version`,
|
2024-03-01 06:43:28 +00:00
|
|
|
),
|
|
|
|
patch: parseNumber(
|
|
|
|
groups.patch!,
|
2024-08-23 03:39:56 +00:00
|
|
|
`Cannot parse comparator ${comparator}: invalid patch version`,
|
2024-03-01 06:43:28 +00:00
|
|
|
),
|
|
|
|
prerelease: prerelease ? parsePrerelease(prerelease) : [],
|
|
|
|
build: buildmetadata ? parseBuild(buildmetadata) : [],
|
|
|
|
}
|
|
|
|
: ANY;
|
|
|
|
|
2024-03-28 04:18:55 +00:00
|
|
|
return { operator: operator || undefined, ...semver };
|
2024-03-01 06:43:28 +00:00
|
|
|
}
|
|
|
|
|
2024-02-20 20:57:21 +00:00
|
|
|
function isWildcard(id?: string): boolean {
|
2023-12-30 23:58:32 +00:00
|
|
|
return !id || id.toLowerCase() === "x" || id === "*";
|
2023-06-12 03:47:03 +00:00
|
|
|
}
|
|
|
|
|
2024-03-01 06:43:28 +00:00
|
|
|
type RangeRegExpGroups = {
|
2023-12-30 23:58:32 +00:00
|
|
|
operator: string;
|
|
|
|
major: string;
|
|
|
|
minor: string;
|
|
|
|
patch: string;
|
|
|
|
prerelease?: string;
|
|
|
|
build?: string;
|
|
|
|
};
|
2023-06-12 03:47:03 +00:00
|
|
|
|
2024-03-05 00:00:55 +00:00
|
|
|
function handleLeftHyphenRangeGroups(
|
|
|
|
leftGroup: RangeRegExpGroups,
|
|
|
|
): Comparator | undefined {
|
|
|
|
if (isWildcard(leftGroup.major)) return;
|
|
|
|
if (isWildcard(leftGroup.minor)) {
|
|
|
|
return {
|
|
|
|
operator: ">=",
|
|
|
|
major: +leftGroup.major,
|
|
|
|
minor: 0,
|
|
|
|
patch: 0,
|
|
|
|
prerelease: [],
|
|
|
|
build: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isWildcard(leftGroup.patch)) {
|
|
|
|
return {
|
|
|
|
operator: ">=",
|
|
|
|
major: +leftGroup.major,
|
|
|
|
minor: +leftGroup.minor,
|
|
|
|
patch: 0,
|
|
|
|
prerelease: [],
|
|
|
|
build: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
operator: ">=",
|
|
|
|
major: +leftGroup.major,
|
|
|
|
minor: +leftGroup.minor,
|
|
|
|
patch: +leftGroup.patch,
|
|
|
|
prerelease: leftGroup.prerelease
|
|
|
|
? parsePrerelease(leftGroup.prerelease)
|
|
|
|
: [],
|
|
|
|
build: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
function handleRightHyphenRangeGroups(
|
|
|
|
rightGroups: RangeRegExpGroups,
|
|
|
|
): Comparator | undefined {
|
|
|
|
if (isWildcard(rightGroups.major)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isWildcard(rightGroups.minor)) {
|
|
|
|
return {
|
|
|
|
operator: "<",
|
|
|
|
major: +rightGroups.major! + 1,
|
|
|
|
minor: 0,
|
|
|
|
patch: 0,
|
|
|
|
prerelease: [],
|
|
|
|
build: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (isWildcard(rightGroups.patch)) {
|
|
|
|
return {
|
|
|
|
operator: "<",
|
|
|
|
major: +rightGroups.major,
|
|
|
|
minor: +rightGroups.minor! + 1,
|
|
|
|
patch: 0,
|
|
|
|
prerelease: [],
|
|
|
|
build: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (rightGroups.prerelease) {
|
|
|
|
return {
|
|
|
|
operator: "<=",
|
|
|
|
major: +rightGroups.major,
|
|
|
|
minor: +rightGroups.minor,
|
|
|
|
patch: +rightGroups.patch,
|
|
|
|
prerelease: parsePrerelease(rightGroups.prerelease),
|
|
|
|
build: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
operator: "<=",
|
|
|
|
major: +rightGroups.major,
|
|
|
|
minor: +rightGroups.minor,
|
|
|
|
patch: +rightGroups.patch,
|
|
|
|
prerelease: rightGroups.prerelease
|
|
|
|
? parsePrerelease(rightGroups.prerelease)
|
|
|
|
: [],
|
|
|
|
build: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
function parseHyphenRange(range: string): Comparator[] | undefined {
|
2024-01-02 02:58:16 +00:00
|
|
|
const leftMatch = range.match(new RegExp(`^${XRANGE}`));
|
2023-12-18 08:12:55 +00:00
|
|
|
const leftGroup = leftMatch?.groups;
|
2024-03-05 00:00:55 +00:00
|
|
|
if (!leftGroup) return;
|
2023-12-18 08:12:55 +00:00
|
|
|
const leftLength = leftMatch[0].length;
|
2024-03-05 00:00:55 +00:00
|
|
|
|
2023-12-18 08:12:55 +00:00
|
|
|
const hyphenMatch = range.slice(leftLength).match(/^\s+-\s+/);
|
2024-03-05 00:00:55 +00:00
|
|
|
if (!hyphenMatch) return;
|
2023-12-18 08:12:55 +00:00
|
|
|
const hyphenLength = hyphenMatch[0].length;
|
2024-03-05 00:00:55 +00:00
|
|
|
|
2023-12-18 08:12:55 +00:00
|
|
|
const rightMatch = range.slice(leftLength + hyphenLength).match(
|
2024-01-02 02:58:16 +00:00
|
|
|
new RegExp(`^${XRANGE}\\s*$`),
|
2023-12-18 08:12:55 +00:00
|
|
|
);
|
|
|
|
const rightGroups = rightMatch?.groups;
|
2024-03-05 00:00:55 +00:00
|
|
|
if (!rightGroups) return;
|
2023-06-12 03:47:03 +00:00
|
|
|
|
2024-03-05 00:00:55 +00:00
|
|
|
const from = handleLeftHyphenRangeGroups(leftGroup as RangeRegExpGroups);
|
|
|
|
const to = handleRightHyphenRangeGroups(rightGroups as RangeRegExpGroups);
|
|
|
|
return [from, to].filter(Boolean) as Comparator[];
|
2023-06-12 03:47:03 +00:00
|
|
|
}
|
2024-03-05 00:00:55 +00:00
|
|
|
|
2024-03-01 06:43:28 +00:00
|
|
|
function handleCaretOperator(groups: RangeRegExpGroups): Comparator[] {
|
2023-12-30 23:58:32 +00:00
|
|
|
const majorIsWildcard = isWildcard(groups.major);
|
|
|
|
const minorIsWildcard = isWildcard(groups.minor);
|
|
|
|
const patchIsWildcard = isWildcard(groups.patch);
|
2023-06-12 03:47:03 +00:00
|
|
|
|
2023-12-30 23:58:32 +00:00
|
|
|
const major = +groups.major;
|
|
|
|
const minor = +groups.minor;
|
|
|
|
const patch = +groups.patch;
|
|
|
|
|
2024-01-15 22:24:05 +00:00
|
|
|
if (majorIsWildcard) return [ALL];
|
2023-12-30 23:58:32 +00:00
|
|
|
if (minorIsWildcard) {
|
|
|
|
return [
|
2024-01-15 22:24:05 +00:00
|
|
|
{ operator: ">=", major, minor: 0, patch: 0 },
|
|
|
|
{ operator: "<", major: major + 1, minor: 0, patch: 0 },
|
2023-12-30 23:58:32 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
if (patchIsWildcard) {
|
|
|
|
if (major === 0) {
|
|
|
|
return [
|
2024-01-15 22:24:05 +00:00
|
|
|
{ operator: ">=", major, minor, patch: 0 },
|
|
|
|
{ operator: "<", major, minor: minor + 1, patch: 0 },
|
2023-12-30 23:58:32 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
return [
|
2024-01-15 22:24:05 +00:00
|
|
|
{ operator: ">=", major, minor, patch: 0 },
|
|
|
|
{ operator: "<", major: major + 1, minor: 0, patch: 0 },
|
2023-12-30 23:58:32 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-01-15 22:24:05 +00:00
|
|
|
const prerelease = parsePrerelease(groups.prerelease ?? "");
|
2023-12-30 23:58:32 +00:00
|
|
|
if (major === 0) {
|
|
|
|
if (minor === 0) {
|
|
|
|
return [
|
2024-01-15 22:24:05 +00:00
|
|
|
{ operator: ">=", major, minor, patch, prerelease },
|
|
|
|
{ operator: "<", major, minor, patch: patch + 1 },
|
2023-12-30 23:58:32 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
return [
|
2024-01-15 22:24:05 +00:00
|
|
|
{ operator: ">=", major, minor, patch, prerelease },
|
|
|
|
{ operator: "<", major, minor: minor + 1, patch: 0 },
|
2023-12-30 23:58:32 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
return [
|
2024-01-15 22:24:05 +00:00
|
|
|
{ operator: ">=", major, minor, patch, prerelease },
|
|
|
|
{ operator: "<", major: major + 1, minor: 0, patch: 0 },
|
2023-12-30 23:58:32 +00:00
|
|
|
];
|
|
|
|
}
|
2024-03-01 06:43:28 +00:00
|
|
|
function handleTildeOperator(groups: RangeRegExpGroups): Comparator[] {
|
2023-12-30 23:58:32 +00:00
|
|
|
const majorIsWildcard = isWildcard(groups.major);
|
|
|
|
const minorIsWildcard = isWildcard(groups.minor);
|
|
|
|
const patchIsWildcard = isWildcard(groups.patch);
|
|
|
|
|
|
|
|
const major = +groups.major;
|
|
|
|
const minor = +groups.minor;
|
|
|
|
const patch = +groups.patch;
|
|
|
|
|
2024-01-15 22:24:05 +00:00
|
|
|
if (majorIsWildcard) return [ALL];
|
2023-12-30 23:58:32 +00:00
|
|
|
if (minorIsWildcard) {
|
|
|
|
return [
|
2024-01-15 22:24:05 +00:00
|
|
|
{ operator: ">=", major, minor: 0, patch: 0 },
|
|
|
|
{ operator: "<", major: major + 1, minor: 0, patch: 0 },
|
2023-12-30 23:58:32 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
if (patchIsWildcard) {
|
|
|
|
return [
|
2024-01-15 22:24:05 +00:00
|
|
|
{ operator: ">=", major, minor, patch: 0 },
|
|
|
|
{ operator: "<", major, minor: minor + 1, patch: 0 },
|
2023-12-30 23:58:32 +00:00
|
|
|
];
|
|
|
|
}
|
2024-01-15 22:24:05 +00:00
|
|
|
const prerelease = parsePrerelease(groups.prerelease ?? "");
|
2023-12-30 23:58:32 +00:00
|
|
|
return [
|
2024-01-15 22:24:05 +00:00
|
|
|
{ operator: ">=", major, minor, patch, prerelease },
|
|
|
|
{ operator: "<", major, minor: minor + 1, patch: 0 },
|
2023-12-30 23:58:32 +00:00
|
|
|
];
|
|
|
|
}
|
2024-03-01 06:43:28 +00:00
|
|
|
function handleLessThanOperator(groups: RangeRegExpGroups): Comparator[] {
|
2023-12-30 23:58:32 +00:00
|
|
|
const majorIsWildcard = isWildcard(groups.major);
|
|
|
|
const minorIsWildcard = isWildcard(groups.minor);
|
|
|
|
const patchIsWildcard = isWildcard(groups.patch);
|
|
|
|
|
|
|
|
const major = +groups.major;
|
|
|
|
const minor = +groups.minor;
|
|
|
|
const patch = +groups.patch;
|
|
|
|
|
2024-01-15 22:24:05 +00:00
|
|
|
if (majorIsWildcard) return [{ operator: "<", major: 0, minor: 0, patch: 0 }];
|
2023-12-30 23:58:32 +00:00
|
|
|
if (minorIsWildcard) {
|
2024-01-15 22:24:05 +00:00
|
|
|
if (patchIsWildcard) return [{ operator: "<", major, minor: 0, patch: 0 }];
|
|
|
|
return [{ operator: "<", major, minor, patch: 0 }];
|
2023-12-30 23:58:32 +00:00
|
|
|
}
|
2024-01-15 22:24:05 +00:00
|
|
|
if (patchIsWildcard) return [{ operator: "<", major, minor, patch: 0 }];
|
|
|
|
const prerelease = parsePrerelease(groups.prerelease ?? "");
|
|
|
|
const build = parseBuild(groups.build ?? "");
|
|
|
|
return [{ operator: "<", major, minor, patch, prerelease, build }];
|
2023-12-30 23:58:32 +00:00
|
|
|
}
|
2024-03-01 06:43:28 +00:00
|
|
|
function handleLessThanOrEqualOperator(
|
|
|
|
groups: RangeRegExpGroups,
|
|
|
|
): Comparator[] {
|
2023-12-30 23:58:32 +00:00
|
|
|
const minorIsWildcard = isWildcard(groups.minor);
|
|
|
|
const patchIsWildcard = isWildcard(groups.patch);
|
|
|
|
|
|
|
|
const major = +groups.major;
|
|
|
|
const minor = +groups.minor;
|
|
|
|
const patch = +groups.patch;
|
|
|
|
|
|
|
|
if (minorIsWildcard) {
|
2024-01-15 22:24:05 +00:00
|
|
|
if (patchIsWildcard) {
|
|
|
|
return [{ operator: "<", major: major + 1, minor: 0, patch: 0 }];
|
|
|
|
}
|
|
|
|
return [{ operator: "<", major, minor: minor + 1, patch: 0 }];
|
2023-12-30 23:58:32 +00:00
|
|
|
}
|
2024-01-15 22:24:05 +00:00
|
|
|
if (patchIsWildcard) {
|
|
|
|
return [{ operator: "<", major, minor: minor + 1, patch: 0 }];
|
|
|
|
}
|
|
|
|
const prerelease = parsePrerelease(groups.prerelease ?? "");
|
|
|
|
const build = parseBuild(groups.build ?? "");
|
|
|
|
return [{ operator: "<=", major, minor, patch, prerelease, build }];
|
2023-12-30 23:58:32 +00:00
|
|
|
}
|
2024-03-01 06:43:28 +00:00
|
|
|
function handleGreaterThanOperator(groups: RangeRegExpGroups): Comparator[] {
|
2023-12-30 23:58:32 +00:00
|
|
|
const majorIsWildcard = isWildcard(groups.major);
|
|
|
|
const minorIsWildcard = isWildcard(groups.minor);
|
|
|
|
const patchIsWildcard = isWildcard(groups.patch);
|
|
|
|
|
|
|
|
const major = +groups.major;
|
|
|
|
const minor = +groups.minor;
|
|
|
|
const patch = +groups.patch;
|
|
|
|
|
2024-01-15 22:24:05 +00:00
|
|
|
if (majorIsWildcard) return [{ operator: "<", major: 0, minor: 0, patch: 0 }];
|
2024-02-18 06:15:41 +00:00
|
|
|
|
2023-12-30 23:58:32 +00:00
|
|
|
if (minorIsWildcard) {
|
2024-02-18 06:15:41 +00:00
|
|
|
return [{ operator: ">=", major: major + 1, minor: 0, patch: 0 }];
|
2023-12-30 23:58:32 +00:00
|
|
|
}
|
2024-01-15 22:24:05 +00:00
|
|
|
if (patchIsWildcard) {
|
2024-02-18 06:15:41 +00:00
|
|
|
return [{ operator: ">=", major, minor: minor + 1, patch: 0 }];
|
2024-01-15 22:24:05 +00:00
|
|
|
}
|
|
|
|
const prerelease = parsePrerelease(groups.prerelease ?? "");
|
|
|
|
const build = parseBuild(groups.build ?? "");
|
|
|
|
return [{ operator: ">", major, minor, patch, prerelease, build }];
|
2023-12-30 23:58:32 +00:00
|
|
|
}
|
2024-03-01 06:43:28 +00:00
|
|
|
function handleGreaterOrEqualOperator(groups: RangeRegExpGroups): Comparator[] {
|
2023-12-30 23:58:32 +00:00
|
|
|
const majorIsWildcard = isWildcard(groups.major);
|
|
|
|
const minorIsWildcard = isWildcard(groups.minor);
|
|
|
|
const patchIsWildcard = isWildcard(groups.patch);
|
|
|
|
|
|
|
|
const major = +groups.major;
|
|
|
|
const minor = +groups.minor;
|
|
|
|
const patch = +groups.patch;
|
|
|
|
|
2024-01-15 22:24:05 +00:00
|
|
|
if (majorIsWildcard) return [ALL];
|
2023-12-30 23:58:32 +00:00
|
|
|
if (minorIsWildcard) {
|
2024-01-15 22:24:05 +00:00
|
|
|
if (patchIsWildcard) return [{ operator: ">=", major, minor: 0, patch: 0 }];
|
|
|
|
return [{ operator: ">=", major, minor, patch: 0 }];
|
2023-12-30 23:58:32 +00:00
|
|
|
}
|
2024-01-15 22:24:05 +00:00
|
|
|
if (patchIsWildcard) return [{ operator: ">=", major, minor, patch: 0 }];
|
|
|
|
const prerelease = parsePrerelease(groups.prerelease ?? "");
|
|
|
|
const build = parseBuild(groups.build ?? "");
|
|
|
|
return [{ operator: ">=", major, minor, patch, prerelease, build }];
|
2023-12-30 23:58:32 +00:00
|
|
|
}
|
2024-03-01 06:43:28 +00:00
|
|
|
function handleEqualOperator(groups: RangeRegExpGroups): Comparator[] {
|
2023-12-30 23:58:32 +00:00
|
|
|
const majorIsWildcard = isWildcard(groups.major);
|
|
|
|
const minorIsWildcard = isWildcard(groups.minor);
|
|
|
|
const patchIsWildcard = isWildcard(groups.patch);
|
|
|
|
|
|
|
|
const major = +groups.major;
|
|
|
|
const minor = +groups.minor;
|
|
|
|
const patch = +groups.patch;
|
|
|
|
|
2024-01-15 22:24:05 +00:00
|
|
|
if (majorIsWildcard) return [ALL];
|
2023-12-30 23:58:32 +00:00
|
|
|
if (minorIsWildcard) {
|
|
|
|
return [
|
2024-01-15 22:24:05 +00:00
|
|
|
{ operator: ">=", major, minor: 0, patch: 0 },
|
|
|
|
{ operator: "<", major: major + 1, minor: 0, patch: 0 },
|
2023-12-30 23:58:32 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
if (patchIsWildcard) {
|
|
|
|
return [
|
2024-01-15 22:24:05 +00:00
|
|
|
{ operator: ">=", major, minor, patch: 0 },
|
|
|
|
{ operator: "<", major, minor: minor + 1, patch: 0 },
|
2023-12-30 23:58:32 +00:00
|
|
|
];
|
|
|
|
}
|
2024-01-15 22:24:05 +00:00
|
|
|
const prerelease = parsePrerelease(groups.prerelease ?? "");
|
|
|
|
const build = parseBuild(groups.build ?? "");
|
2024-02-18 06:43:09 +00:00
|
|
|
return [{ operator: undefined, major, minor, patch, prerelease, build }];
|
2023-12-30 23:58:32 +00:00
|
|
|
}
|
|
|
|
|
2024-07-30 02:31:20 +00:00
|
|
|
function parseOperatorRange(string: string): Comparator | Comparator[] | null {
|
2024-03-01 06:43:28 +00:00
|
|
|
const groups = string.match(OPERATOR_XRANGE_REGEXP)
|
|
|
|
?.groups as RangeRegExpGroups;
|
2023-12-30 23:58:32 +00:00
|
|
|
if (!groups) return parseComparator(string);
|
|
|
|
|
|
|
|
switch (groups.operator) {
|
|
|
|
case "^":
|
|
|
|
return handleCaretOperator(groups);
|
|
|
|
case "~":
|
|
|
|
case "~>":
|
|
|
|
return handleTildeOperator(groups);
|
|
|
|
case "<":
|
|
|
|
return handleLessThanOperator(groups);
|
|
|
|
case "<=":
|
|
|
|
return handleLessThanOrEqualOperator(groups);
|
|
|
|
case ">":
|
|
|
|
return handleGreaterThanOperator(groups);
|
|
|
|
case ">=":
|
|
|
|
return handleGreaterOrEqualOperator(groups);
|
|
|
|
case "=":
|
|
|
|
case "":
|
|
|
|
return handleEqualOperator(groups);
|
|
|
|
default:
|
2024-08-23 03:39:56 +00:00
|
|
|
throw new Error(
|
|
|
|
`Cannot parse version range: '${groups.operator}' is not a valid operator`,
|
|
|
|
);
|
2023-12-30 23:58:32 +00:00
|
|
|
}
|
2023-06-12 03:47:03 +00:00
|
|
|
}
|
2024-07-30 02:31:20 +00:00
|
|
|
function parseOperatorRanges(string: string): (Comparator | null)[] {
|
2024-03-05 00:00:55 +00:00
|
|
|
return string.split(/\s+/).flatMap(parseOperatorRange);
|
|
|
|
}
|
2023-06-12 03:47:03 +00:00
|
|
|
|
|
|
|
/**
|
2024-06-28 05:47:34 +00:00
|
|
|
* Parses a range string into a {@linkcode Range} object.
|
2024-05-27 10:03:20 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
|
|
|
* import { parseRange } from "@std/semver/parse-range";
|
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 { assertEquals } from "@std/assert";
|
2024-05-27 10:03:20 +00:00
|
|
|
*
|
|
|
|
* const range = parseRange(">=1.0.0 <2.0.0 || >=3.0.0");
|
|
|
|
* assertEquals(range, [
|
|
|
|
* [
|
|
|
|
* { operator: ">=", major: 1, minor: 0, patch: 0, prerelease: [], build: [] },
|
|
|
|
* { operator: "<", major: 2, minor: 0, patch: 0, prerelease: [], build: [] },
|
|
|
|
* ],
|
|
|
|
* [
|
|
|
|
* { operator: ">=", major: 3, minor: 0, patch: 0, prerelease: [], build: [] },
|
|
|
|
* ]
|
|
|
|
* ]);
|
|
|
|
* ```
|
|
|
|
*
|
2024-06-28 05:47:34 +00:00
|
|
|
* @throws {TypeError} If the input range is invalid.
|
2024-08-19 04:33:42 +00:00
|
|
|
* @param value The range set string
|
2024-06-28 05:47:34 +00:00
|
|
|
* @returns A valid SemVer range
|
2023-06-12 03:47:03 +00:00
|
|
|
*/
|
2024-08-19 04:33:42 +00:00
|
|
|
export function parseRange(value: string): Range {
|
|
|
|
const result = value
|
2024-03-05 00:00:55 +00:00
|
|
|
// remove spaces between operators and versions
|
2024-07-30 02:31:20 +00:00
|
|
|
.replaceAll(/(?<=<|>|=|~|\^)(\s+)/g, "")
|
2023-06-12 03:47:03 +00:00
|
|
|
.split(/\s*\|\|\s*/)
|
2024-03-05 00:00:55 +00:00
|
|
|
.map((string) => parseHyphenRange(string) || parseOperatorRanges(string));
|
2024-07-30 02:31:20 +00:00
|
|
|
if (result.some((r) => r.includes(null))) {
|
2024-08-23 03:39:56 +00:00
|
|
|
throw new TypeError(
|
|
|
|
`Cannot parse version range: range "${value}" is invalid`,
|
|
|
|
);
|
2024-07-30 02:31:20 +00:00
|
|
|
}
|
|
|
|
return result as Range;
|
2023-06-12 03:47:03 +00:00
|
|
|
}
|