All files / src Version.ts

92.85% Statements 65/70
100% Branches 19/19
78.26% Functions 18/23
92.64% Lines 63/68

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247    2x   2x 2x               2x 46x 46x 46x 46x 46x                                 46x 46x 46x 46x 46x   46x 34x     46x 31x     46x 28x     46x 21x     46x 21x                                                                                                       11x 11x 11x         3x 3x         3x               9x 1x     8x         13x               9x 1x     8x         13x         7x   7x 7x   7x                       3x 3x   3x 1x         2x   2x                     2x                     1x                 14x   10x 10x   2x 2x   2x 2x     14x 2x   12x     14x 2x   12x        
import { IVersion } from './interfaces';
import { UpdateType } from './UpdateType';
import { UpdateTypes } from './UpdatesTypes';
 
const alphanumericValuesSeparatedByDots = /\w+(\.\w+)*/i;
const defaultIndexValue = 0;
 
/**
 * A simple utility class wrapping up some methods to manage your project's version from within the code.
 *
 * @export
 * @implements {IVersion}
 */
export class Version implements IVersion {
  #major: number;
  #minor: number;
  #patch: number;
  #preRelease: string | null;
  #build: string | null;
 
  /**
   * Creates an instance of {@link Version}.
   * @param major [default=1]
   * @param minor [default=0]
   * @param patch [default=0]
   * @param preRelease [default=null]
   * @param build [default=null]
   */
  constructor(
    major: number = 1,
    minor: number = 0,
    patch: number = 0,
    preRelease?: string | null,
    build?: string | null
  ) {
    this.#major = major;
    this.#minor = minor;
    this.#patch = patch;
    this.#preRelease = preRelease ?? null;
    this.#build = build ?? null;
 
    Object.defineProperty(this, 'major', {
      get: () => this.#major,
      enumerable: true,
    });
    Object.defineProperty(this, 'minor', {
      get: () => this.#minor,
      enumerable: true,
    });
    Object.defineProperty(this, 'patch', {
      get: () => this.#patch,
      enumerable: true,
    });
    Object.defineProperty(this, 'preRelease', {
      get: () => this.#preRelease,
      enumerable: true,
    });
    Object.defineProperty(this, 'build', {
      get: () => this.#build,
      enumerable: true,
    });
  }
 
  /**
   * Gets the [major] value.
   *
   * @readonly
   */
  get major(): number {
    return this.#major;
  }
 
  /**
   * Gets the [minor] value.
   *
   * @readonly
   */
  get minor(): number {
    return this.#minor;
  }
 
  /**
   * Gets the [patch] value.
   *
   * @readonly
   */
  get patch(): number {
    return this.#patch;
  }
 
  /**
   * Gets the [pre-release] value.
   *
   * @readonly
   */
  get preRelease(): string | null {
    return this.#preRelease;
  }
 
  /**
   * Gets the [build] value.
   *
   * @readonly
   */
  get build(): string | null {
    return this.#build;
  }
 
  /** @inheritdoc */
  incrementMajor(): void {
    this.#major++;
    this.#minor = defaultIndexValue;
    this.#patch = defaultIndexValue;
  }
 
  /** @inheritdoc */
  incrementMinor(): void {
    this.#minor++;
    this.#patch = defaultIndexValue;
  }
 
  /** @inheritdoc */
  incrementPatch(): void {
    this.#patch++;
  }
 
  /**
   * @inheritdoc
   * @throws The pre-release suffix must be alphanumeric values separated by dots!
   */
  setPreRelease(preRelease: string): void {
    if (!preRelease.match(alphanumericValuesSeparatedByDots))
      throw new Error(
        'The pre-release suffix must be alphanumeric values separated by dots!'
      );
    this.#preRelease = preRelease;
  }
 
  /** @inheritdoc */
  resetPreRelease(): void {
    this.#preRelease = null;
  }
 
  /**
   * @inheritdoc
   * @throws The build suffix must be alphanumeric values separated by dots!
   */
  setBuild(build: string): void {
    if (!build.match(alphanumericValuesSeparatedByDots))
      throw new Error(
        'The build suffix must be alphanumeric values separated by dots!'
      );
    this.#build = build;
  }
 
  /** @inheritdoc */
  resetBuild(): void {
    this.#build = null;
  }
 
  /** @inheritdoc */
  toString(): string {
    let version = `${this.#major}.${this.#minor}.${this.#patch}`;
 
    if (this.#preRelease) version += `-${this.#preRelease}`;
    if (this.#build) version += `+${this.#build}`;
 
    return version;
  }
 
  /**
   * Parses a string to detect if it fits in the [Semantic Versioning](https://semver.org) format.
   *
   * @param version the version as a string in the [SemVer](https://semver.org) format.
   * @returns an instance of {@link Version} based on the input
   * @throws The value [version] does not follow the semantic versioning format: `<major>.<minor>.<patch>-<pre-release>+<build>` where pre-release and build are optional.
   */
  static fromString(version: string): Version {
    const regexp =
      /^(\d+)\.(\d+)\.(\d+)(?:-(\w+(?:\.\w+)*))?(?:\+(\w+(?:\.\w+)*))?$/g;
    const match = [...version.matchAll(regexp)];
 
    if (match.length === 0) {
      throw new Error(
        `The value ${version} does not follow the semantic versioning format: "\<major\>.\<minor\>.\<patch\>-\<pre-release\>+\<build\>" where pre-release and build are optional.`
      );
    }
 
    const [_, major, minor, patch, preRelease, build] = [...match][0];
 
    return new Version(
      Number(major),
      Number(minor),
      Number(patch),
      preRelease,
      build
    );
  }
 
  /** @inheritdoc */
  toJSON(): string {
    return this.toString();
  }
 
  /**
   * Revives an instance of {@link Version} when it has been stringified via `JSON.stringify`.
   *
   * @param key the property (should be null or undefined)
   * @param value the value for this property (should be the version as a string)
   * @returns the newly created instance of {@link Version}
   */
  static fromJSON(key: string | null | undefined, value: string): Version {
    return Version.fromString(value);
  }
 
  /** @inheritdoc */
  update(
    type: UpdateType,
    preRelease?: string | null,
    build?: string | null
  ): void {
    switch (type) {
      case UpdateTypes.MAJOR:
        this.incrementMajor();
        break;
      case UpdateTypes.MINOR:
        this.incrementMinor();
        break;
      case UpdateTypes.PATCH:
        this.incrementPatch();
        break;
    }
 
    if (preRelease) {
      this.setPreRelease(preRelease);
    } else {
      this.resetPreRelease();
    }
 
    if (build) {
      this.setBuild(build);
    } else {
      this.resetBuild();
    }
  }
}