diff --git a/src/utils/minecraft-utils.ts b/src/utils/minecraft-utils.ts index 4037f5c..a807c25 100644 --- a/src/utils/minecraft-utils.ts +++ b/src/utils/minecraft-utils.ts @@ -127,6 +127,11 @@ export function parseVersionNameFromFileVersion(fileVersion: string): string | n } } +export function parseVersionName(version: string): string | null { + const versionCandidates = [...(version.match(/\d+\.\d+(?:\.\d+)?/g) || [])]; + return versionCandidates.filter(x => x.startsWith("1."))[0] || null; +} + export async function getLatestRelease(): Promise { return (await getVersions()).find(x => x.isRelease) || null; } diff --git a/test/minecraft-utils.test.ts b/test/minecraft-utils.test.ts index eaf4dd6..2559e82 100644 --- a/test/minecraft-utils.test.ts +++ b/test/minecraft-utils.test.ts @@ -1,5 +1,5 @@ import { describe, test, expect } from "@jest/globals"; -import { getVersionById, findVersionByName, isSnapshot, parseVersionNameFromFileVersion, getVersions, getLatestRelease, getCompatibleBuilds } from "../src/utils/minecraft-utils"; +import { getVersionById, findVersionByName, isSnapshot, parseVersionName, parseVersionNameFromFileVersion, getVersions, getLatestRelease, getCompatibleBuilds } from "../src/utils/minecraft-utils"; import Version from "../src/utils/version"; describe("getVersionById", () => { @@ -78,6 +78,27 @@ describe("isSnapshot", () => { }); }); +describe("parseVersionName", () => { + test("Minecraft versions are parsed correctly", () => { + expect(parseVersionName("1.17.1")).toStrictEqual("1.17.1"); + expect(parseVersionName("1.16.3")).toStrictEqual("1.16.3"); + expect(parseVersionName("1.17")).toStrictEqual("1.17"); + expect(parseVersionName("1.16")).toStrictEqual("1.16"); + }); + + test("weird formats that contain Minecraft version are parsed correctly", () => { + expect(parseVersionName("1.17-5.0.1-beta+build.29")).toStrictEqual("1.17"); + expect(parseVersionName("[1.16.5, 1.17)")).toStrictEqual("1.16.5"); + expect(parseVersionName(">=1.17")).toStrictEqual("1.17"); + }); + + test("null is returned if version string does not contain Minecraft version", () => { + expect(parseVersionName("5.0.8-beta+build.111")).toBeNull(); + expect(parseVersionName("5.3.3-BETA+ec3b0e5d")).toBeNull(); + expect(parseVersionName("2.0.12")).toBeNull(); + }); +}); + describe("parseVersionNameFromFileVersion", () => { test("Sodium-like versions are parsed correctly", () => { expect(parseVersionNameFromFileVersion("mc1.17.1-0.3.2+build.7")).toStrictEqual("1.17.1");