Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING(semver): remove FormatStyle #4182

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 3 additions & 28 deletions semver/format.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { ANY } from "./constants.ts";
import type { FormatStyle, SemVer } from "./types.ts";
import type { SemVer } from "./types.ts";

function formatNumber(value: number) {
if (value === Number.POSITIVE_INFINITY) {
Expand All @@ -12,12 +12,6 @@ function formatNumber(value: number) {
}
}

/**
* Format a SemVer object into a string.
*
* @deprecated (will be removed in 0.213.0) `style` option is deprecated. Use `format(semver)` for full formatting. semver[prop] for getting a part of the version.
*/
export function format(semver: SemVer, style?: FormatStyle): string;
/**
* Format a SemVer object into a string.
*
Expand All @@ -28,8 +22,7 @@ export function format(semver: SemVer, style?: FormatStyle): string;
* @param semver The semantic version to format
* @returns The string representation of a semantic version.
*/
export function format(semver: SemVer): string;
export function format(semver: SemVer, style: FormatStyle = "full"): string {
export function format(semver: SemVer): string {
if (semver === ANY) {
return "*";
}
Expand All @@ -42,23 +35,5 @@ export function format(semver: SemVer, style: FormatStyle = "full"): string {

const primary = `${major}.${minor}.${patch}`;
const release = [primary, pre].filter((v) => v).join("-");
const full = [release, build].filter((v) => v).join("+");
switch (style) {
case "full":
return full;
case "release":
return release;
case "primary":
return primary;
case "build":
return build;
case "pre":
return pre;
case "patch":
return patch;
case "minor":
return minor;
case "major":
return major;
}
return [release, build].filter((v) => v).join("+");
}
89 changes: 18 additions & 71 deletions semver/format_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,92 +4,39 @@ import { assertEquals } from "../assert/mod.ts";
import { format } from "./format.ts";
import { parse } from "./parse.ts";
import { INVALID, MAX, MIN } from "./constants.ts";
import { FormatStyle, SemVer } from "./types.ts";
import { SemVer } from "./types.ts";

Deno.test("format", async (t) => {
const versions: [string, FormatStyle | undefined, string][] = [
["1.2.3", undefined, "1.2.3"],
["1.2.3", "full", "1.2.3"],
["1.2.3", "release", "1.2.3"],
["1.2.3", "primary", "1.2.3"],
["1.2.3", "build", ""],
["1.2.3", "pre", ""],
["1.2.3", "patch", "3"],
["1.2.3", "minor", "2"],
["1.2.3", "major", "1"],

["1.2.3-pre", undefined, "1.2.3-pre"],
["1.2.3-pre", "full", "1.2.3-pre"],
["1.2.3-pre", "release", "1.2.3-pre"],
["1.2.3-pre", "primary", "1.2.3"],
["1.2.3-pre", "build", ""],
["1.2.3-pre", "pre", "pre"],
["1.2.3-pre", "patch", "3"],
["1.2.3-pre", "minor", "2"],
["1.2.3-pre", "major", "1"],

["1.2.3-pre.0", undefined, "1.2.3-pre.0"],
["1.2.3-pre.0", "full", "1.2.3-pre.0"],
["1.2.3-pre.0", "release", "1.2.3-pre.0"],
["1.2.3-pre.0", "primary", "1.2.3"],
["1.2.3-pre.0", "build", ""],
["1.2.3-pre.0", "pre", "pre.0"],
["1.2.3-pre.0", "patch", "3"],
["1.2.3-pre.0", "minor", "2"],
["1.2.3-pre.0", "major", "1"],

["1.2.3+b", undefined, "1.2.3+b"],
["1.2.3+b", "full", "1.2.3+b"],
["1.2.3+b", "release", "1.2.3"],
["1.2.3+b", "primary", "1.2.3"],
["1.2.3+b", "build", "b"],
["1.2.3+b", "pre", ""],
["1.2.3+b", "patch", "3"],
["1.2.3+b", "minor", "2"],
["1.2.3+b", "major", "1"],

["1.2.3+b.0", undefined, "1.2.3+b.0"],
["1.2.3+b.0", "full", "1.2.3+b.0"],
["1.2.3+b.0", "release", "1.2.3"],
["1.2.3+b.0", "primary", "1.2.3"],
["1.2.3+b.0", "build", "b.0"],
["1.2.3+b.0", "pre", ""],
["1.2.3+b.0", "patch", "3"],
["1.2.3+b.0", "minor", "2"],
["1.2.3+b.0", "major", "1"],

["1.2.3-pre.0+b.0", undefined, "1.2.3-pre.0+b.0"],
["1.2.3-pre.0+b.0", "full", "1.2.3-pre.0+b.0"],
["1.2.3-pre.0+b.0", "release", "1.2.3-pre.0"],
["1.2.3-pre.0+b.0", "primary", "1.2.3"],
["1.2.3-pre.0+b.0", "build", "b.0"],
["1.2.3-pre.0+b.0", "pre", "pre.0"],
["1.2.3-pre.0+b.0", "patch", "3"],
["1.2.3-pre.0+b.0", "minor", "2"],
["1.2.3-pre.0+b.0", "major", "1"],
const versions: [string, string][] = [
["1.2.3", "1.2.3"],
["1.2.3-pre", "1.2.3-pre"],
["1.2.3-pre.0", "1.2.3-pre.0"],
["1.2.3+b", "1.2.3+b"],
["1.2.3+b.0", "1.2.3+b.0"],
["1.2.3-pre.0+b.0", "1.2.3-pre.0+b.0"],
];

for (const [version, style, expected] of versions) {
for (const [version, expected] of versions) {
await t.step({
name: `format(${version} ${style} ${expected})`,
name: `format(${version} ${expected})`,
fn: () => {
const v = parse(version)!;
const actual = format(v, style);
const actual = format(v);
assertEquals(actual, expected);
},
});
}

const constantSemVers: [SemVer, FormatStyle | undefined, string][] = [
[MAX, "full", "∞.∞.∞"],
[MIN, "full", "0.0.0"],
[INVALID, "full", "⧞.∞.∞"],
const constantSemVers: [SemVer, string][] = [
[MAX, "∞.∞.∞"],
[MIN, "0.0.0"],
[INVALID, "⧞.∞.∞"],
];
for (const [version, style, expected] of constantSemVers) {
for (const [version, expected] of constantSemVers) {
await t.step({
name: `format(${version} ${style} ${expected})`,
name: `format(${version} ${expected})`,
fn: () => {
const actual = format(version, style);
const actual = format(version);
assertEquals(actual, expected);
},
});
Expand Down
14 changes: 0 additions & 14 deletions semver/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,6 @@ export type ReleaseType =
*/
export type Operator = typeof OPERATORS[number];

/**
* The style to use when formatting a SemVer object into a string
* @deprecated (will be removed in 0.213.0)
*/
export type FormatStyle =
| "full"
| "release"
| "primary"
| "build"
| "pre"
| "patch"
| "minor"
| "major";

/**
* The shape of a valid semantic version comparator
* @example >=0.0.0
Expand Down