Skip to content

Commit

Permalink
BREAKING(semver): replace prerelease and buildmetadata arguments …
Browse files Browse the repository at this point in the history
…with options object (#5471)

BREAKING(semver): replace `prerelease` and `build` arguments with options object
  • Loading branch information
iuioiua authored Jul 18, 2024
1 parent 8023016 commit 702f048
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
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

0 comments on commit 702f048

Please sign in to comment.