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): replace prerelease and buildmetadata arguments with options object #5471

Merged
merged 1 commit into from
Jul 18, 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
28 changes: 17 additions & 11 deletions semver/increment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ function bumpPrerelease(
return values;
}

/** Options for {@linkcode increment}. */
export interface IncrementOptions {
/** The pre-release metadata of the new version. */
prerelease?: string;
/** The build metadata of the new version. */
build?: string;
}

/**
* Returns the new SemVer resulting from an increment by release type.
*
Expand Down Expand Up @@ -73,18 +81,16 @@ function bumpPrerelease(
*
* @param version The version to increment
* @param release The type of increment to perform
* @param prerelease The pre-release metadata of the new version
* @param buildmetadata The build metadata of the new version
* @param options Additional options
* @returns The new version
*/
export function increment(
version: SemVer,
release: ReleaseType,
prerelease?: string,
buildmetadata?: string,
options?: IncrementOptions,
): SemVer {
const build = buildmetadata !== undefined
? parseBuild(buildmetadata)
const build = options?.build !== undefined
? parseBuild(options?.build)
: version.build;

switch (release) {
Expand All @@ -93,23 +99,23 @@ export function increment(
major: version.major + 1,
minor: 0,
patch: 0,
prerelease: bumpPrerelease(version.prerelease, prerelease),
prerelease: bumpPrerelease(version.prerelease, options?.prerelease),
build,
};
case "preminor":
return {
major: version.major,
minor: version.minor + 1,
patch: 0,
prerelease: bumpPrerelease(version.prerelease, prerelease),
prerelease: bumpPrerelease(version.prerelease, options?.prerelease),
build,
};
case "prepatch":
return {
major: version.major,
minor: version.minor,
patch: version.patch + 1,
prerelease: bumpPrerelease(version.prerelease, prerelease),
prerelease: bumpPrerelease(version.prerelease, options?.prerelease),
build,
};
case "prerelease": {
Expand All @@ -120,7 +126,7 @@ export function increment(
major: version.major,
minor: version.minor,
patch,
prerelease: bumpPrerelease(version.prerelease, prerelease),
prerelease: bumpPrerelease(version.prerelease, options?.prerelease),
build,
};
}
Expand Down Expand Up @@ -180,7 +186,7 @@ export function increment(
major: version.major,
minor: version.minor,
patch: version.patch,
prerelease: bumpPrerelease(version.prerelease, prerelease),
prerelease: bumpPrerelease(version.prerelease, options?.prerelease),
build,
};
}
Expand Down
4 changes: 2 additions & 2 deletions semver/increment_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -963,11 +963,11 @@ Deno.test("increment()", async (t) => {
],
];

for (const [version, op, identifier, metadata, expected] of versions) {
for (const [version, op, prerelease, build, expected] of versions) {
await t.step({
name: `${op} ${format(version)}`,
fn: () => {
const actual = increment(version, op, identifier, metadata);
const actual = increment(version, op, { prerelease, build });
assertEquals(format(actual), expected);
},
});
Expand Down
2 changes: 1 addition & 1 deletion semver/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
* import { increment, parse } from "@std/semver";
* import { assertEquals } from "@std/assert";
*
* assertEquals(increment(parse("1.2.3"), "prerelease", "alpha"), parse("1.2.4-alpha.0"));
* assertEquals(increment(parse("1.2.3"), "prerelease", { prerelease: "alpha" }), parse("1.2.4-alpha.0"));
* ```
*
* ### Build Metadata
Expand Down