diff --git a/src/utils/versioning/version-type.ts b/src/utils/versioning/version-type.ts index 37addf0..0bd402c 100644 --- a/src/utils/versioning/version-type.ts +++ b/src/utils/versioning/version-type.ts @@ -1,19 +1,77 @@ -enum VersionType { - Alpha = "alpha", - Beta = "beta", - Release = "release" +import { Enum, EnumOptions } from "@/utils/enum"; + +/** + * Represents different version types for software releases. + * + * @partial + */ +enum VersionTypeValues { + /** + * Alpha version, usually for early testing and development. + */ + ALPHA = "alpha", + + /** + * Beta version, typically for more advanced testing and bug fixing. + */ + BETA = "beta", + + /** + * Release version, the stable and final version of the software. + */ + RELEASE = "release", } -namespace VersionType { - export function fromName(name: string): VersionType { - if (name.match(/[+-_]alpha/i)) { - return VersionType.Alpha; - } else if (name.match(/[+-_]beta/i)) { - return VersionType.Beta; - } else { - return VersionType.Release; - } +/** + * Options for configuring the behavior of the VersionType enum. + * + * @partial + */ +const VersionTypeOptions: EnumOptions = { + /** + * The case should be ignored while parsing the version type. + */ + ignoreCase: true, +}; + +/** + * Parses the provided file name and returns the corresponding {@link VersionType}. + * + * @param fileName - The file name string to parse. + * + * @returns The detected {@link VersionType} based on the input file name. + */ +function parseFromFileName(fileName: string): VersionType { + if (fileName.match(/[+-_]alpha/i)) { + return VersionType.ALPHA; } + + if (fileName.match(/[+-_]beta/i)) { + return VersionType.BETA; + } + + return VersionType.RELEASE; } -export default VersionType; +/** + * A collection of methods to work with VersionType. + * + * @partial + */ +const VersionTypeMethods = { + parseFromFileName, +}; + +/** + * Represents different version types for software releases. + */ +export const VersionType = Enum.create( + VersionTypeValues, + VersionTypeOptions, + VersionTypeMethods, +); + +/** + * Represents different version types for software releases. + */ +export type VersionType = Enum; diff --git a/tests/unit/utils/versioning/version-type.spec.ts b/tests/unit/utils/versioning/version-type.spec.ts new file mode 100644 index 0000000..55f2c42 --- /dev/null +++ b/tests/unit/utils/versioning/version-type.spec.ts @@ -0,0 +1,43 @@ +import { VersionType } from "@/utils/versioning/version-type"; + +describe("VersionType", () => { + describe("parseFromFileName", () => { + test("version type is correctly parsed from the filename", () => { + expect(VersionType.parseFromFileName("sodium-fabric-mc1.17.1-0.3.2+build.7")).toStrictEqual(VersionType.RELEASE); + expect(VersionType.parseFromFileName("fabric-api-0.40.1+1.18_experimental")).toStrictEqual(VersionType.RELEASE); + expect(VersionType.parseFromFileName("TechReborn-5.0.8-beta+build.111")).toStrictEqual(VersionType.BETA); + expect(VersionType.parseFromFileName("TechReborn-1.17-5.0.1-beta+build.29")).toStrictEqual(VersionType.BETA); + expect(VersionType.parseFromFileName("Terra-forge-5.3.3-BETA+ec3b0e5d")).toStrictEqual(VersionType.BETA); + expect(VersionType.parseFromFileName("Terra-forge-5.3.3-alpha+ec3b0e5d")).toStrictEqual(VersionType.ALPHA); + expect(VersionType.parseFromFileName("modmenu-2.0.12")).toStrictEqual(VersionType.RELEASE); + expect(VersionType.parseFromFileName("enhancedblockentities-0.5+1.17")).toStrictEqual(VersionType.RELEASE); + expect(VersionType.parseFromFileName("sync-mc1.17.x-1.2")).toStrictEqual(VersionType.RELEASE); + }); + }); + + describe("parse", () => { + test("parses all its own formatted values", () => { + for (const value of VersionType.values()) { + expect(VersionType.parse(VersionType.format(value))).toBe(value); + } + }); + + test("parses all friendly names of its own values", () => { + for (const value of VersionType.values()) { + expect(VersionType.parse(VersionType.friendlyNameOf(value))).toBe(value); + } + }); + + test("parses all its own formatted values in lowercase", () => { + for (const value of VersionType.values()) { + expect(VersionType.parse(VersionType.format(value).toLowerCase())).toBe(value); + } + }); + + test("parses all its own formatted values in UPPERCASE", () => { + for (const value of VersionType.values()) { + expect(VersionType.parse(VersionType.format(value).toUpperCase())).toBe(value); + } + }); + }); +});