From a7e07e008d853f9f644d69e88cd3748f536b9b8b Mon Sep 17 00:00:00 2001 From: Eldar Dadashov Date: Wed, 24 Jun 2026 00:43:06 +0300 Subject: [PATCH] fix(web): clamp unit index in formatFileSize/formatTime to avoid undefined unit --- web/utils/format.spec.ts | 6 ++++++ web/utils/format.ts | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/web/utils/format.spec.ts b/web/utils/format.spec.ts index 2796854e347..cd5b9b6c24e 100644 --- a/web/utils/format.spec.ts +++ b/web/utils/format.spec.ts @@ -64,6 +64,9 @@ describe('formatFileSize', () => { it('should format petabytes correctly', () => { expect(formatFileSize(1500000000000000)).toBe('1.33 PB') }) + it('should clamp to PB instead of an undefined unit for very large sizes', () => { + expect(formatFileSize(1024 ** 6)).toBe('1024.00 PB') + }) }) describe('formatTime', () => { it('should return the input if it is falsy', () => { @@ -81,6 +84,9 @@ describe('formatTime', () => { it('should handle large numbers', () => { expect(formatTime(7200)).toBe('2.00 h') }) + it('should clamp to hours instead of an undefined unit for very large durations', () => { + expect(formatTime(216000)).toBe('60.00 h') + }) }) describe('formatNumberAbbreviated', () => { it('should return number as string when less than 1000', () => { diff --git a/web/utils/format.ts b/web/utils/format.ts index 1e6df2ea8c4..1632b8a1796 100644 --- a/web/utils/format.ts +++ b/web/utils/format.ts @@ -75,7 +75,7 @@ export const formatFileSize = (fileSize: number) => { return fileSize const units = ['', 'K', 'M', 'G', 'T', 'P'] let index = 0 - while (fileSize >= 1024 && index < units.length) { + while (fileSize >= 1024 && index < units.length - 1) { fileSize = fileSize / 1024 index++ } @@ -94,7 +94,7 @@ export const formatTime = (seconds: number) => { return seconds const units = ['sec', 'min', 'h'] let index = 0 - while (seconds >= 60 && index < units.length) { + while (seconds >= 60 && index < units.length - 1) { seconds = seconds / 60 index++ }