diff --git a/semver/format_range.ts b/semver/format_range.ts new file mode 100644 index 000000000000..ac2fb98d02c2 --- /dev/null +++ b/semver/format_range.ts @@ -0,0 +1,14 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. +import type { SemVerRange } from "./types.ts"; +import { comparatorFormat } from "./comparator_format.ts"; + +/** + * Formats the range into a string + * @example >=0.0.0 || <1.0.0 + * @param range The range to format + * @returns A string representation of the range + */ +export function formatRange(range: SemVerRange): string { + return range.ranges.map((c) => c.map((c) => comparatorFormat(c)).join(" ")) + .join("||"); +} diff --git a/semver/format_range_test.ts b/semver/format_range_test.ts new file mode 100644 index 000000000000..f6757e8f9d3a --- /dev/null +++ b/semver/format_range_test.ts @@ -0,0 +1,73 @@ +// Copyright Isaac Z. Schlueter and Contributors. All rights reserved. ISC license. +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. +import { assertEquals } from "../assert/mod.ts"; +import { formatRange } from "./format_range.ts"; +import { parseRange } from "./parse_range.ts"; + +Deno.test({ + name: "formatRange()", + fn: async (t) => { + const versions: [string, string][] = [ + ["1.0.0 - 2.0.0", ">=1.0.0 <=2.0.0"], + ["1.0.0", "1.0.0"], + [">=*", "*"], + ["", "*"], + ["*", "*"], + [">=1.0.0", ">=1.0.0"], + [">1.0.0", ">1.0.0"], + ["<=2.0.0", "<=2.0.0"], + ["1", ">=1.0.0 <2.0.0"], + ["<=2.0.0", "<=2.0.0"], + ["<=2.0.0", "<=2.0.0"], + ["<2.0.0", "<2.0.0"], + ["<2.0.0", "<2.0.0"], + [">=0.1.97", ">=0.1.97"], + [">=0.1.97", ">=0.1.97"], + ["0.1.20 || 1.2.4", "0.1.20||1.2.4"], + [">=0.2.3 || <0.0.1", ">=0.2.3||<0.0.1"], + [">=0.2.3 || <0.0.1", ">=0.2.3||<0.0.1"], + [">=0.2.3 || <0.0.1", ">=0.2.3||<0.0.1"], + ["||", "*||*"], + ["2.x.x", ">=2.0.0 <3.0.0"], + ["1.2.x", ">=1.2.0 <1.3.0"], + ["1.2.x || 2.x", ">=1.2.0 <1.3.0||>=2.0.0 <3.0.0"], + ["1.2.x || 2.x", ">=1.2.0 <1.3.0||>=2.0.0 <3.0.0"], + ["x", "*"], + ["2.*.*", ">=2.0.0 <3.0.0"], + ["1.2.*", ">=1.2.0 <1.3.0"], + ["1.2.* || 2.*", ">=1.2.0 <1.3.0||>=2.0.0 <3.0.0"], + ["2", ">=2.0.0 <3.0.0"], + ["2.3", ">=2.3.0 <2.4.0"], + ["~2.4", ">=2.4.0 <2.5.0"], + ["~2.4", ">=2.4.0 <2.5.0"], + ["~>3.2.1", ">=3.2.1 <3.3.0"], + ["~1", ">=1.0.0 <2.0.0"], + ["~>1", ">=1.0.0 <2.0.0"], + ["~1.0", ">=1.0.0 <1.1.0"], + ["^0", ">=0.0.0 <1.0.0"], + ["^0.1", ">=0.1.0 <0.2.0"], + ["^1.0", ">=1.0.0 <2.0.0"], + ["^1.2", ">=1.2.0 <2.0.0"], + ["^0.0.1", ">=0.0.1 <0.0.2"], + ["^0.0.1-beta", ">=0.0.1-beta <0.0.2"], + ["^0.1.2", ">=0.1.2 <0.2.0"], + ["^1.2.3", ">=1.2.3 <2.0.0"], + ["^1.2.3-beta.4", ">=1.2.3-beta.4 <2.0.0"], + ["<1", "<1.0.0"], + [">=1", ">=1.0.0"], + ["<1.2", "<1.2.0"], + ["1", ">=1.0.0 <2.0.0"], + ]; + + for (const [r, expected] of versions) { + await t.step({ + name: `${r} -> ${expected}`, + fn: () => { + const range = parseRange(r); + const actual = formatRange(range); + assertEquals(actual, expected); + }, + }); + } + }, +}); diff --git a/semver/range_format.ts b/semver/range_format.ts index 9dabafd67ce4..73c7e2bff1ea 100644 --- a/semver/range_format.ts +++ b/semver/range_format.ts @@ -1,14 +1,16 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. -import type { SemVerRange } from "./types.ts"; -import { comparatorFormat } from "./comparator_format.ts"; + +import { formatRange } from "./format_range.ts"; +import { SemVerRange } from "./types.ts"; /** * Formats the range into a string * @example >=0.0.0 || <1.0.0 * @param range The range to format * @returns A string representation of the range + * + * @deprecated (will be removed after 0.213.0) Use {@linkcode formatRange} instead. */ export function rangeFormat(range: SemVerRange): string { - return range.ranges.map((c) => c.map((c) => comparatorFormat(c)).join(" ")) - .join("||"); + return formatRange(range); } diff --git a/semver/range_test.ts b/semver/range_test.ts index 59029572e0c9..eb13f708b498 100644 --- a/semver/range_test.ts +++ b/semver/range_test.ts @@ -1,7 +1,6 @@ // Copyright Isaac Z. Schlueter and Contributors. All rights reserved. ISC license. // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. -import { assert, assertEquals } from "../assert/mod.ts"; -import { rangeFormat } from "./range_format.ts"; +import { assert } from "../assert/mod.ts"; import { parse } from "./parse.ts"; import { parseRange } from "./parse_range.ts"; import { testRange } from "./test_range.ts"; @@ -217,71 +216,3 @@ Deno.test("negativeUnlockedPrereleaseRange", function () { assert(!found, `${v} satisfied by ${r} unexpectedly`); } }); - -Deno.test({ - name: "validRange", - fn: async (t) => { - const versions: [string, string][] = [ - ["1.0.0 - 2.0.0", ">=1.0.0 <=2.0.0"], - ["1.0.0", "1.0.0"], - [">=*", "*"], - ["", "*"], - ["*", "*"], - [">=1.0.0", ">=1.0.0"], - [">1.0.0", ">1.0.0"], - ["<=2.0.0", "<=2.0.0"], - ["1", ">=1.0.0 <2.0.0"], - ["<=2.0.0", "<=2.0.0"], - ["<=2.0.0", "<=2.0.0"], - ["<2.0.0", "<2.0.0"], - ["<2.0.0", "<2.0.0"], - [">=0.1.97", ">=0.1.97"], - [">=0.1.97", ">=0.1.97"], - ["0.1.20 || 1.2.4", "0.1.20||1.2.4"], - [">=0.2.3 || <0.0.1", ">=0.2.3||<0.0.1"], - [">=0.2.3 || <0.0.1", ">=0.2.3||<0.0.1"], - [">=0.2.3 || <0.0.1", ">=0.2.3||<0.0.1"], - ["||", "*||*"], - ["2.x.x", ">=2.0.0 <3.0.0"], - ["1.2.x", ">=1.2.0 <1.3.0"], - ["1.2.x || 2.x", ">=1.2.0 <1.3.0||>=2.0.0 <3.0.0"], - ["1.2.x || 2.x", ">=1.2.0 <1.3.0||>=2.0.0 <3.0.0"], - ["x", "*"], - ["2.*.*", ">=2.0.0 <3.0.0"], - ["1.2.*", ">=1.2.0 <1.3.0"], - ["1.2.* || 2.*", ">=1.2.0 <1.3.0||>=2.0.0 <3.0.0"], - ["2", ">=2.0.0 <3.0.0"], - ["2.3", ">=2.3.0 <2.4.0"], - ["~2.4", ">=2.4.0 <2.5.0"], - ["~2.4", ">=2.4.0 <2.5.0"], - ["~>3.2.1", ">=3.2.1 <3.3.0"], - ["~1", ">=1.0.0 <2.0.0"], - ["~>1", ">=1.0.0 <2.0.0"], - ["~1.0", ">=1.0.0 <1.1.0"], - ["^0", ">=0.0.0 <1.0.0"], - ["^0.1", ">=0.1.0 <0.2.0"], - ["^1.0", ">=1.0.0 <2.0.0"], - ["^1.2", ">=1.2.0 <2.0.0"], - ["^0.0.1", ">=0.0.1 <0.0.2"], - ["^0.0.1-beta", ">=0.0.1-beta <0.0.2"], - ["^0.1.2", ">=0.1.2 <0.2.0"], - ["^1.2.3", ">=1.2.3 <2.0.0"], - ["^1.2.3-beta.4", ">=1.2.3-beta.4 <2.0.0"], - ["<1", "<1.0.0"], - [">=1", ">=1.0.0"], - ["<1.2", "<1.2.0"], - ["1", ">=1.0.0 <2.0.0"], - ]; - - for (const [r, expected] of versions) { - await t.step({ - name: `${r} -> ${expected}`, - fn: () => { - const range = parseRange(r); - const actual = rangeFormat(range); - assertEquals(actual, expected); - }, - }); - } - }, -});