Made an interface that represents a game version provider

This commit is contained in:
Kir_Antipov 2023-03-24 05:48:31 +00:00
parent f67c962726
commit d50f67b65c
2 changed files with 50 additions and 0 deletions

View file

@ -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<GameVersion[]>;
}
/**
* A map of game version providers keyed by game name.
*/
const GAME_VERSION_PROVIDERS: ReadonlyMap<string, GameVersionProvider> = 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);
}

View file

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