mirror of
https://github.com/lone-cloud/gerbil
synced 2026-06-03 19:54:44 -07:00
42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
import { KOBOLDAI_URLS } from '@/constants';
|
|
|
|
export const formatDownloadSize = (size: number, url?: string): string => {
|
|
if (!size) return '';
|
|
|
|
const isApproximateSize = url?.includes(KOBOLDAI_URLS.DOMAIN);
|
|
|
|
return isApproximateSize
|
|
? `~${formatFileSizeInMB(size)}`
|
|
: formatFileSizeInMB(size);
|
|
};
|
|
|
|
export const compareVersions = (versionA: string, versionB: string): number => {
|
|
const cleanVersion = (version: string): string =>
|
|
version.replace(/^v/, '').replace(/[^0-9.]/g, '');
|
|
|
|
const parseVersion = (version: string): number[] =>
|
|
cleanVersion(version)
|
|
.split('.')
|
|
.map((num) => parseInt(num, 10) || 0);
|
|
|
|
const a = parseVersion(versionA);
|
|
const b = parseVersion(versionB);
|
|
const maxLength = Math.max(a.length, b.length);
|
|
|
|
for (let i = 0; i < maxLength; i++) {
|
|
const aVal = a[i] || 0;
|
|
const bVal = b[i] || 0;
|
|
|
|
if (aVal !== bVal) {
|
|
return aVal - bVal;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
};
|
|
|
|
const formatFileSizeInMB = (bytes: number) => {
|
|
if (bytes === 0) return '0 MB';
|
|
const mb = bytes / (1024 * 1024);
|
|
return parseFloat(mb.toFixed(1)) + ' MB';
|
|
};
|