34 lines
1.0 KiB
TypeScript
34 lines
1.0 KiB
TypeScript
import * as z from "zod";
|
|
|
|
export const lunarisVersionSchema = z.string().regex(/^\d+(?:\.\d+)+$/u);
|
|
|
|
export interface LunarisVersionChange {
|
|
previousVersion: string;
|
|
targetVersion: string;
|
|
}
|
|
|
|
export function parseLunarisVersionChange(
|
|
message: string,
|
|
): LunarisVersionChange | null {
|
|
const match = message.match(
|
|
/^Version Change:\s*(\d+(?:\.\d+)+)\s*→\s*(\d+(?:\.\d+)+)\s*$/imu,
|
|
);
|
|
if (!match) return null;
|
|
return {
|
|
previousVersion: lunarisVersionSchema.parse(match[1]),
|
|
targetVersion: lunarisVersionSchema.parse(match[2]),
|
|
};
|
|
}
|
|
|
|
export function compareLunarisVersions(left: string, right: string): number {
|
|
const leftParts = lunarisVersionSchema.parse(left).split(".").map(Number);
|
|
const rightParts = lunarisVersionSchema.parse(right).split(".").map(Number);
|
|
const length = Math.max(leftParts.length, rightParts.length);
|
|
|
|
for (let index = 0; index < length; index += 1) {
|
|
const difference = (leftParts[index] ?? 0) - (rightParts[index] ?? 0);
|
|
if (difference !== 0) return Math.sign(difference);
|
|
}
|
|
return 0;
|
|
}
|