All files / semver / increment.ts

100.00% Branches 39/39
100.00% Lines 126/126
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
 
 
x25
 
 
x106
x318
 
x106
x106
x256
x256
x304
x304
x304
x358
x358
 
x106
 
x106
x106
 
x106
x106
x106
 
x106
 
 
x106
 
x235
 
x106
 
 
x149
x149
x149
x149
x160
x160
x724
x181
 
x145
x106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x25
x25
x25
x25
 
x147
x147
x147
 
x147
x147
x155
x155
x155
x155
x155
x155
x155
x147
x155
x155
x155
x155
x155
x155
x155
x147
x155
x155
x155
x155
x155
x155
x155
x335
 
x188
x188
x188
x188
x188
x188
x188
x188
x188
x188
x308
 
 
 
x161
x161
x161
x161
x161
x161
x161
x161
x161
x161
x161
x161
x307
 
 
 
x160
x160
x160
x160
x160
x160
x160
x160
x160
x160
x160
x160
x307
 
 
 
 
x160
x160
x160
x160
x160
x160
x160
x160
x160
x160
x310
 
 
 
 
x163
x163
x163
x163
x163
x163
x163
x163
x147
x148
x148
 
x147
x147



















































































































































































































// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.
import { parseBuild, parsePrerelease } from "./_shared.ts";
import type { ReleaseType, SemVer } from "./types.ts";

function bumpPrereleaseNumber(prerelease: ReadonlyArray<string | number> = []) {
  const values = [...prerelease];

  let index = values.length;
  while (index >= 0) {
    const value = values[index];
    if (typeof value === "number") {
      values[index] = value + 1;
      break;
    }
    index -= 1;
  }
  // if no number was bumped
  if (index === -1) values.push(0);

  return values;
}

function bumpPrerelease(
  currentPrerelease: ReadonlyArray<string | number> = [],
  newPrerelease: string | undefined,
): (string | number)[] {
  const bumpedPrerelease = bumpPrereleaseNumber(currentPrerelease);

  // If the identifier is not provided, return the bumped prerelease
  if (!newPrerelease) return bumpedPrerelease;

  let newIdentifiers = parsePrerelease(newPrerelease);

  if (newIdentifiers.every((id) => typeof id === "string")) {
    // When the give prerelease has no number and are all included in the existing prerelease
    // it should just bump the number
    if (
      newIdentifiers.every((id, i) => id === bumpedPrerelease[i]) &&
      typeof bumpedPrerelease[newIdentifiers.length] === "number"
    ) {
      return bumpedPrerelease;
    }
    newIdentifiers = [...newIdentifiers, 0];
  }

  return newIdentifiers;
}

/** Options for {@linkcode increment}. */
export interface IncrementOptions {
  /** The pre-release 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.
 *
 * `premajor`, `preminor` and `prepatch` will bump the version up to the next version,
 * based on the type, and will also add prerelease metadata.
 *
 * If called from a non-prerelease version, the `prerelease` will work the same as
 * `prepatch`. The patch version is incremented and then is made into a prerelease. If
 * the input version is already a prerelease it will simply increment the prerelease
 * metadata.
 *
 * If a `prerelease` option is specified without a number then a number will be added.
 * For example `pre` will result in `pre.0`. If the existing version already has a
 * prerelease with a number and its the same prerelease identifier then the number
 * will be incremented. If the identifier differs from the new identifier then the new
 * identifier is applied and the number is reset to `0`.
 *
 * If a prerelease is specified with a number then that exact prerelease will be used.
 *
 * If the input version has build metadata it will be preserved on the resulting version
 * unless a new build parameter is specified. Specifying `""` will unset existing build
 * metadata.
 *
 * @example Usage
 * ```ts
 * import { increment, parse } from "@std/semver";
 * import { assertEquals } from "@std/assert";
 *
 * const version = parse("1.2.3");
 * assertEquals(increment(version, "major"), parse("2.0.0"));
 * assertEquals(increment(version, "minor"), parse("1.3.0"));
 * assertEquals(increment(version, "patch"), parse("1.2.4"));
 * assertEquals(increment(version, "prerelease"), parse("1.2.4-0"));
 *
 * assertEquals(increment(parse("1.2.3-beta.0"), "prerelease"), parse("1.2.3-beta.1"));
 * assertEquals(increment(parse("1.2.3-beta.3"), "prerelease", { prerelease: "rc" }), parse("1.2.3-rc.0"));
 * assertEquals(increment(parse("1.2.3-beta.3"), "prerelease", { prerelease: "rc.1" }), parse("1.2.3-rc.1"));
 * ```
 *
 * @param version The version to increment
 * @param release The type of increment to perform
 * @param options Additional options
 * @returns The new version
 */
export function increment(
  version: SemVer,
  release: ReleaseType,
  options: IncrementOptions = {},
): SemVer {
  const build = options.build !== undefined
    ? parseBuild(options.build)
    : version.build ?? [];

  switch (release) {
    case "premajor":
      return {
        major: version.major + 1,
        minor: 0,
        patch: 0,
        prerelease: bumpPrerelease(version.prerelease, options.prerelease),
        build,
      };
    case "preminor":
      return {
        major: version.major,
        minor: version.minor + 1,
        patch: 0,
        prerelease: bumpPrerelease(version.prerelease, options.prerelease),
        build,
      };
    case "prepatch":
      return {
        major: version.major,
        minor: version.minor,
        patch: version.patch + 1,
        prerelease: bumpPrerelease(version.prerelease, options.prerelease),
        build,
      };
    case "prerelease": {
      // If the input is a non-prerelease version, this acts the same as prepatch.
      const isPrerelease = (version.prerelease ?? []).length === 0;
      const patch = isPrerelease ? version.patch + 1 : version.patch;
      return {
        major: version.major,
        minor: version.minor,
        patch,
        prerelease: bumpPrerelease(version.prerelease, options.prerelease),
        build,
      };
    }
    case "major": {
      // If this is a pre-major version, bump up to the same major version. Otherwise increment major.
      // 1.0.0-5 bumps to 1.0.0
      // 1.1.0 bumps to 2.0.0
      const isPrerelease = (version.prerelease ?? []).length === 0;
      const major = isPrerelease || version.minor !== 0 || version.patch !== 0
        ? version.major + 1
        : version.major;
      return {
        major,
        minor: 0,
        patch: 0,
        prerelease: [],
        build,
      };
    }
    case "minor": {
      // If this is a pre-minor version, bump up to the same minor version. Otherwise increment minor.
      // 1.2.0-5 bumps to 1.2.0
      // 1.2.1 bumps to 1.3.0
      const isPrerelease = (version.prerelease ?? []).length === 0;
      const minor = isPrerelease || version.patch !== 0
        ? version.minor + 1
        : version.minor;
      return {
        major: version.major,
        minor,
        patch: 0,
        prerelease: [],
        build,
      };
    }
    case "patch": {
      // If this is not a pre-release version, it will increment the patch.
      // 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
      const isPrerelease = (version.prerelease ?? []).length === 0;
      const patch = isPrerelease ? version.patch + 1 : version.patch;
      return {
        major: version.major,
        minor: version.minor,
        patch,
        prerelease: [],
        build,
      };
    }
    case "pre": {
      // 1.0.0 "pre" would become 1.0.0-0
      // 1.0.0-0 would become 1.0.0-1
      // 1.0.0-beta.0 would be come 1.0.0-beta.1
      // switching the pre identifier resets the number to 0
      return {
        major: version.major,
        minor: version.minor,
        patch: version.patch,
        prerelease: bumpPrerelease(version.prerelease, options.prerelease),
        build,
      };
    }
    default:
      throw new TypeError(
        `Cannot increment version: invalid argument ${release}`,
      );
  }
}