From 59e9693705510e7c07e629627925a46cd79574cc Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Sun, 26 Mar 2023 09:13:56 +0000 Subject: [PATCH] Made enum for Minecraft version types --- src/games/minecraft/minecraft-version-type.ts | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/games/minecraft/minecraft-version-type.ts diff --git a/src/games/minecraft/minecraft-version-type.ts b/src/games/minecraft/minecraft-version-type.ts new file mode 100644 index 0000000..53978d1 --- /dev/null +++ b/src/games/minecraft/minecraft-version-type.ts @@ -0,0 +1,95 @@ +import { VersionType } from "@/utils/versioning"; +import { Enum, EnumOptions } from "@/utils/enum"; + +/** + * Represents Minecraft version types. + * + * @partial + */ +enum MinecraftVersionTypeValues { + /** + * Represents the release version type of Minecraft. + */ + RELEASE = "release", + + /** + * Represents the snapshot version type of Minecraft. + */ + SNAPSHOT = "snapshot", + + /** + * Represents the old beta version type of Minecraft. + */ + OLD_BETA = "old_beta", + + /** + * Represents the old alpha version type of Minecraft. + */ + OLD_ALPHA = "old_alpha", +} + +/** + * Options for configuring the behavior of the MinecraftVersionType enum. + * + * @partial + */ +const MinecraftVersionTypeOptions: EnumOptions = { + /** + * The case should be ignored while parsing the version type. + */ + ignoreCase: true, + + /** + * Non-word characters should be ignored while parsing the version type. + */ + ignoreNonWordCharacters: true, +}; + +/** + * Converts a `MinecraftVersionType` value to a corresponding `VersionType` value. + * + * @param type - The Minecraft version type to convert. + * @param version - The Minecraft version string, used for additional checks when the type is `Snapshot`. + * + * @returns The corresponding `VersionType` value. + */ +function toVersionType(type: MinecraftVersionType, version?: string): VersionType { + switch (type) { + case MinecraftVersionType.SNAPSHOT: + return version?.match(/-pre|-rc|-beta|Pre-[Rr]elease|[Rr]elease Candidate/) + ? VersionType.BETA + : VersionType.ALPHA; + + case MinecraftVersionType.OLD_BETA: + return VersionType.BETA; + + case MinecraftVersionType.OLD_ALPHA: + return VersionType.ALPHA; + + default: + return VersionType.RELEASE; + } +} + +/** + * A collection of methods to work with MinecraftVersionType. + * + * @partial + */ +const MinecraftVersionTypeMethods = { + toVersionType, +}; + +/** + * Represents Minecraft version types. + */ +export const MinecraftVersionType = Enum.create( + MinecraftVersionTypeValues, + MinecraftVersionTypeOptions, + MinecraftVersionTypeMethods, +); + +/** + * Represents Minecraft version types. + */ +export type MinecraftVersionType = Enum;