Made method that parses Minecraft version

...from version name
This commit is contained in:
Kir_Antipov 2021-12-06 15:52:15 +03:00
parent dc7721454f
commit 90929a7f48
2 changed files with 27 additions and 1 deletions

View file

@ -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<MinecraftVersion | null> {
return (await getVersions()).find(x => x.isRelease) || null;
}

View file

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