feat(semver): optional SemVer build and prerelease properties (#4024)

Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
This commit is contained in:
Tim Reichen 2023-12-28 11:00:18 +01:00 committed by GitHub
parent 8497153a9b
commit 81bcbc1ca1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 828 additions and 131 deletions

View File

@ -13,8 +13,8 @@ export function compareNumber(
}
export function checkIdentifier(
v1: ReadonlyArray<string | number>,
v2: ReadonlyArray<string | number>,
v1: ReadonlyArray<string | number> = [],
v2: ReadonlyArray<string | number> = [],
): 1 | 0 | -1 {
// NOT having a prerelease is > having one
// But NOT having a build is < having one
@ -28,8 +28,8 @@ export function checkIdentifier(
}
export function compareIdentifier(
v1: ReadonlyArray<string | number>,
v2: ReadonlyArray<string | number>,
v1: ReadonlyArray<string | number> = [],
v2: ReadonlyArray<string | number> = [],
): 1 | 0 | -1 {
let i = 0;
do {

View File

@ -17,7 +17,7 @@ export function comparatorMin(semver: SemVer, operator: Operator): SemVer {
switch (operator) {
case ">":
return semver.prerelease.length > 0
return semver.prerelease && semver.prerelease.length > 0
? increment(semver, "pre")
: increment(semver, "patch");
case "!=":

View File

@ -14,7 +14,7 @@ export function difference(
let prefix = "";
let defaultResult: ReleaseType | undefined = undefined;
if (s0 && s1) {
if (s0.prerelease.length || s1.prerelease.length) {
if ((s0.prerelease ?? []).length || (s1.prerelease ?? []).length) {
prefix = "pre";
defaultResult = "prerelease";
}

View File

@ -30,8 +30,8 @@ export function format(semver: SemVer, style: FormatStyle = "full"): string {
const major = formatNumber(semver.major);
const minor = formatNumber(semver.minor);
const patch = formatNumber(semver.patch);
const pre = semver.prerelease.join(".");
const build = semver.build.join(".");
const pre = semver.prerelease?.join(".") ?? "";
const build = semver.build?.join(".") ?? "";
const primary = `${major}.${minor}.${patch}`;
const release = [primary, pre].filter((v) => v).join("-");

View File

@ -2,7 +2,7 @@
import type { ReleaseType, SemVer } from "./types.ts";
function pre(
prerelease: ReadonlyArray<string | number>,
prerelease: ReadonlyArray<string | number> = [],
identifier: string | undefined,
) {
let values = [...prerelease];
@ -37,7 +37,7 @@ function pre(
}
function parseBuild(
build: string[],
build: string[] = [],
metadata: string | undefined,
) {
return metadata === undefined ? build : metadata.split(".").filter((m) => m);
@ -107,7 +107,7 @@ export function increment(
// If the input is a non-prerelease version, this acts the same as
// prepatch.
case "prerelease":
if (version.prerelease.length === 0) {
if ((version.prerelease ?? []).length === 0) {
result = {
major: version.major,
minor: version.minor,
@ -134,7 +134,7 @@ export function increment(
if (
version.minor !== 0 ||
version.patch !== 0 ||
version.prerelease.length === 0
(version.prerelease ?? []).length === 0
) {
result = {
major: version.major + 1,
@ -161,7 +161,7 @@ export function increment(
// 1.2.1 bumps to 1.3.0
if (
version.patch !== 0 ||
version.prerelease.length === 0
(version.prerelease ?? []).length === 0
) {
result = {
major: version.major,
@ -186,7 +186,7 @@ export function increment(
// If it is a pre-release it will bump up to the same patch version.
// 1.2.0-5 patches to 1.2.0
// 1.2.0 patches to 1.2.1
if (version.prerelease.length === 0) {
if ((version.prerelease ?? []).length === 0) {
result = {
major: version.major,
minor: version.minor,

File diff suppressed because it is too large Load Diff

View File

@ -25,7 +25,7 @@ export function isSemVer(value: unknown): value is SemVer {
if (value === INVALID) return true;
if (value === ANY) return true;
const { major, minor, patch, build, prerelease } = value as Record<
const { major, minor, patch, build = [], prerelease = [] } = value as Record<
string,
unknown
>;

View File

@ -16,8 +16,6 @@ Deno.test({
[false],
[0],
["1.2.3"],
[{ major: 0, minor: 0, patch: 0, prerelease: [] }],
[{ major: 0, minor: 0, patch: 0, build: [] }],
[{ major: 0, minor: 0, build: [], prerelease: [] }],
[{ major: 0, patch: 0, build: [], prerelease: [] }],
[{ minor: 0, patch: 0, build: [], prerelease: [] }],
@ -47,6 +45,18 @@ Deno.test({
fn: async (t) => {
let i = 0;
const versions: [unknown][] = [
[{ major: 0, minor: 0, patch: 0 }],
[{ major: 0, minor: 0, patch: 0, prerelease: [] }],
[{ major: 0, minor: 0, patch: 0, build: [] }],
[{ major: 0, minor: 0, patch: 0, prerelease: undefined }],
[{ major: 0, minor: 0, patch: 0, build: undefined }],
[{
major: 0,
minor: 0,
patch: 0,
prerelease: undefined,
build: undefined,
}],
[{ major: 0, minor: 0, patch: 0, build: [], prerelease: [] }],
[{ extra: 1, major: 0, minor: 0, patch: 0, build: [], prerelease: [] }],
[{ major: 0, minor: 0, patch: 0, build: ["abc"], prerelease: [] }],

View File

@ -65,8 +65,8 @@ export interface SemVer {
major: number;
minor: number;
patch: number;
prerelease: (string | number)[];
build: string[];
prerelease?: (string | number)[];
build?: string[];
}
type SemVerRangeAnd = SemVerComparator[];