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

feat(semver): Add support for build metadata #3126

Conversation

justinmchase
Copy link
Contributor

@justinmchase justinmchase commented Jan 19, 2023

Overview

Currently the build metadata is parsed but is not printed out in any form. A semver which is parsed will not be identical to
the printed output:

console.log(semver.parse("1.2.3+4").format()) // 1.2.3

Additionally, once set, there is no way to alter the metadata on the SemVer instance without formatting into a new string and re-parsing:

const build = "5";
const v0 = semver.parse("1.2.3+4").increment("pre");
const v1 = semver.parse(`${v0}+${build}`);

Therefore this PR attempts to address these two issues with the following APIs:

  1. Add a new, optional, options object parameter onto the .format() function which contains a single field style. The style will dictate the format of the output of the function, it will not affect the format internal mutated fields this.version or this.raw, to preserve backwards compatibility and not require an extensive refactor.
  2. The .increment() function will now have a new, optional, metadata: string parameter which will set the this.build field with the new metadata or reset it to empty. Incrementing with no build data will not retain the previous build metadata.
console.log(semver.parse("1.2.3+4").format({ style: "full" )) // 1.2.3+4

const build = "5";
const v0 = semver.parse("1.2.3+4").increment("pre", undefined, build);
console.log(v0.format({ style: "full" })) // 1.2.3-0+5

Related To

Notes

  • According to the semver spec the build metadata must not be used during the comparison calculations. For this reason the internal this.version and this.raw fields are preserved as they were since those fields can be part of the comparison calculations. The only modifications are to the output of the format function.
  • Currently the parsing functions do actually parse the build metadata correctly, so there was no need to modify this. It handles it safely it simply does not output it or support modification correctly.

@CLAassistant
Copy link

CLAassistant commented Jan 19, 2023

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Looks nice enhancement. Thanks for your contribution!

import * as semver from "./mod.ts";

Deno.test("format", async (t) => {
const versions: [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice exhausive tests!

@kt3k kt3k merged commit 07f059f into denoland:main Jan 26, 2023
bartlomieju pushed a commit to crowlKats/deno_std that referenced this pull request Jan 27, 2023
@justinmchase justinmchase deleted the feature/full-version-formatting-and-incrementing branch January 29, 2023 23:43
bartlomieju pushed a commit to bartlomieju/deno_std that referenced this pull request Feb 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

semver.format function is dropping build data
3 participants