VersionType refactoring

This commit is contained in:
Kir_Antipov 2023-01-26 06:40:31 +00:00
parent 5381af88ee
commit 17bc59ff61
2 changed files with 115 additions and 14 deletions

View file

@ -1,19 +1,77 @@
enum VersionType { import { Enum, EnumOptions } from "@/utils/enum";
Alpha = "alpha",
Beta = "beta", /**
Release = "release" * 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 { * Options for configuring the behavior of the VersionType enum.
if (name.match(/[+-_]alpha/i)) { *
return VersionType.Alpha; * @partial
} else if (name.match(/[+-_]beta/i)) { */
return VersionType.Beta; const VersionTypeOptions: EnumOptions = {
} else { /**
return VersionType.Release; * 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;
} }
export default VersionType; if (fileName.match(/[+-_]beta/i)) {
return VersionType.BETA;
}
return VersionType.RELEASE;
}
/**
* 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<typeof VersionTypeValues>;

View file

@ -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);
}
});
});
});