From d50f67b65c2f39c1d6624155a573f14c5884805f Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Fri, 24 Mar 2023 05:48:31 +0000 Subject: [PATCH] Made an interface that represents a game version provider --- src/games/game-version-provider.ts | 35 +++++++++++++++++++ .../unit/games/game-version-provider.spec.ts | 15 ++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/games/game-version-provider.ts create mode 100644 tests/unit/games/game-version-provider.spec.ts diff --git a/src/games/game-version-provider.ts b/src/games/game-version-provider.ts new file mode 100644 index 0000000..04ee3a5 --- /dev/null +++ b/src/games/game-version-provider.ts @@ -0,0 +1,35 @@ +import { Awaitable } from "@/utils/types"; +import { GameVersion } from "./game-version"; +import { MINECRAFT, MINECRAFT_VERSION_PROVIDER } from "./minecraft"; + +/** + * Represents a provider for game version. + */ +export interface GameVersionProvider { + /** + * Returns an array of game versions that satisfy the given version range. + * + * @param versions - A version range. + * + * @returns An array of game versions that satisfy the given version range. + */ + (versions: string[]): Awaitable; +} + +/** + * A map of game version providers keyed by game name. + */ +const GAME_VERSION_PROVIDERS: ReadonlyMap = new Map([ + [MINECRAFT, MINECRAFT_VERSION_PROVIDER], +]); + +/** + * Returns the game version provider for the given game name. + * + * @param name - The name of the game. + * + * @returns The {@link GameVersionProvider} for the given game name, or `undefined` if it does not exist. + */ +export function getGameVersionProviderByName(name: string): GameVersionProvider | undefined { + return GAME_VERSION_PROVIDERS.get(name); +} diff --git a/tests/unit/games/game-version-provider.spec.ts b/tests/unit/games/game-version-provider.spec.ts new file mode 100644 index 0000000..a2b1340 --- /dev/null +++ b/tests/unit/games/game-version-provider.spec.ts @@ -0,0 +1,15 @@ +import { getGameVersionProviderByName } from "@/games/game-version-provider"; + +describe("getGameVersionProviderByName", () => { + test("returns the correct provider for a given game name", async () => { + const provider = getGameVersionProviderByName("minecraft"); + + expect(provider).toBeDefined(); + }); + + test("returns undefined for a non-existing game name", async () => { + const provider = getGameVersionProviderByName("unknown-game"); + + expect(provider).toBeUndefined(); + }); +});