Skip to content

Commit

Permalink
deprecation(semver): rename rangeFormat() to formatRange() (#4090)
Browse files Browse the repository at this point in the history
Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
  • Loading branch information
timreichen and iuioiua committed Jan 8, 2024
1 parent 2943c6c commit 3d74dbd
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 74 deletions.
14 changes: 14 additions & 0 deletions semver/format_range.ts
Original file line number Diff line number Diff line change
@@ -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("||");
}
73 changes: 73 additions & 0 deletions semver/format_range_test.ts
Original file line number Diff line number Diff line change
@@ -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);
},
});
}
},
});
10 changes: 6 additions & 4 deletions semver/range_format.ts
Original file line number Diff line number Diff line change
@@ -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);
}
71 changes: 1 addition & 70 deletions semver/range_test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);
},
});
}
},
});

0 comments on commit 3d74dbd

Please sign in to comment.