Files
mariani_mobile/utils/version.ts

27 lines
942 B
TypeScript

/**
* Compare two version strings.
* Returns true if 'latest' is greater than 'current' (an update is available).
*/
export const isUpdateAvailable = (currentVersion: string | undefined, latestVersion: string) => {
if (!currentVersion || !latestVersion) return false;
// Split strings into an array of numbers: "1.2.10" -> [1, 2, 10]
const currentParts = currentVersion.split('.').map(Number);
const latestParts = latestVersion.split('.').map(Number);
const maxLength = Math.max(currentParts.length, latestParts.length);
for (let i = 0; i < maxLength; i++) {
// If a part is missing, we consider it as 0 (e.g. "1.0" -> [1, 0, 0])
const current = currentParts[i] || 0;
const latest = latestParts[i] || 0;
if (current < latest) {
return true; // It needs an update
}
if (current > latest) {
return false;
}
}
return false;
};