mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-15 15:24:52 +09:00
Add MajorMinorVersion type and dedupe the game version regex
Address review feedback: introduce a MajorMinorVersion type with isMajorMinorVersion, rename majorMinorVersion to normaliseMajorMinorVersion, and type ModInfo.factorioVersion as MajorMinorVersion so already-reduced values are not re-reduced (ModPackViewPage uses mod.info.factorioVersion directly). Reuse the single gameVersionRegExp in both GameVersionSchema and normaliseGameVersion instead of duplicating the pattern. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
97a6c32838
commit
7c2793752d
6 files changed
+47
-21
No files matched your search
@@ -10,8 +10,8 @@ import { ModRecord } from "./ModPack";
|
||||
import {
|
||||
FullVersion, integerFullVersion,
|
||||
integerPartialVersion,
|
||||
GameVersionSchema, normaliseGameVersion, majorMinorVersion,
|
||||
PartialVersion,
|
||||
GameVersionSchema, normaliseGameVersion, normaliseMajorMinorVersion,
|
||||
MajorMinorVersion,
|
||||
ModVersionEquality,
|
||||
} from "./version";
|
||||
|
||||
@@ -175,7 +175,7 @@ export default class ModInfo {
|
||||
* Major version of Factorio this mod supports.
|
||||
* Sourced from info.json.
|
||||
*/
|
||||
factorioVersion = "0.12" as PartialVersion;
|
||||
factorioVersion = "0.12" as MajorMinorVersion;
|
||||
|
||||
/**
|
||||
* Integer representation of the factorioVersion
|
||||
@@ -297,7 +297,7 @@ export default class ModInfo {
|
||||
if (factorioVersion === undefined) {
|
||||
throw new Error(`Invalid factorio_version "${json.factorio_version}"`);
|
||||
}
|
||||
modInfo.factorioVersion = majorMinorVersion(factorioVersion);
|
||||
modInfo.factorioVersion = normaliseMajorMinorVersion(factorioVersion);
|
||||
}
|
||||
|
||||
// Parse the dependencies
|
||||
|
||||
@@ -60,7 +60,7 @@ export function normaliseFullVersion(version: PartialVersion) {
|
||||
* optional, and any trailing content is ignored. The pattern is intentionally
|
||||
* not anchored at the end.
|
||||
*/
|
||||
const gameVersionRegExp = /^\s*\d+\.\s*\d+(?:\.\s*\d+)?/;
|
||||
const gameVersionRegExp = /^\s*(\d+)\.\s*(\d+)(?:\.\s*(\d+))?/;
|
||||
export const GameVersionSchema = Type.String({ pattern: gameVersionRegExp.source });
|
||||
|
||||
/**
|
||||
@@ -72,7 +72,7 @@ export const GameVersionSchema = Type.String({ pattern: gameVersionRegExp.source
|
||||
* could be read.
|
||||
*/
|
||||
export function normaliseGameVersion(input: string): FullVersion | undefined {
|
||||
const match = /^\s*(\d+)\.\s*(\d+)(?:\.\s*(\d+))?/.exec(input);
|
||||
const match = gameVersionRegExp.exec(input);
|
||||
if (match === null) {
|
||||
return undefined;
|
||||
}
|
||||
@@ -83,7 +83,7 @@ export function normaliseGameVersion(input: string): FullVersion | undefined {
|
||||
* Matches valid factorio versions and mod dependencies specifications where 2 or 3 parts are specified.
|
||||
*/
|
||||
const partialVersionRegExp = /^\d+\.\d+(?:\.\d+)?$/;
|
||||
export type PartialVersion = FullVersion | `${number}.${number}`;
|
||||
export type PartialVersion = FullVersion | MajorMinorVersion;
|
||||
export const PartialVersionSchema = Type.Unsafe<PartialVersion>(
|
||||
Type.String({ pattern: partialVersionRegExp.source })
|
||||
);
|
||||
@@ -99,11 +99,24 @@ export function integerPartialVersion(version: PartialVersion) {
|
||||
}
|
||||
|
||||
/**
|
||||
* The major.minor of a version, as used by the mod portal's version filter.
|
||||
* Matches a major.minor version, as used by the mod portal's version filter.
|
||||
*/
|
||||
export function majorMinorVersion(version: PartialVersion) {
|
||||
const majorMinorVersionRegExp = /^\d+\.\d+$/;
|
||||
export type MajorMinorVersion = `${number}.${number}`;
|
||||
export const MajorMinorVersionSchema = Type.Unsafe<MajorMinorVersion>(
|
||||
Type.String({ pattern: majorMinorVersionRegExp.source })
|
||||
);
|
||||
|
||||
export function isMajorMinorVersion(input: string): input is MajorMinorVersion {
|
||||
return majorMinorVersionRegExp.test(input);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reduce a version to its major.minor, as used by the mod portal's version filter.
|
||||
*/
|
||||
export function normaliseMajorMinorVersion(version: PartialVersion): MajorMinorVersion {
|
||||
const [major, minor] = version.split(".");
|
||||
return `${major}.${minor}` as `${number}.${number}`;
|
||||
return `${major}.${minor}` as MajorMinorVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user