From 2ea00923562d471bd92603c4acbfe253a367bdbb Mon Sep 17 00:00:00 2001 From: lunov Date: Sat, 10 Jan 2026 19:35:23 +0700 Subject: [PATCH] fix(filesystem): handle negative values in formatSize() Return '0 B' for negative byte values instead of 'NaN B'. File sizes cannot be negative, so this is a safe default. Co-Authored-By: Claude Opus 4.5 --- src/filesystem/__tests__/lib.test.ts | 6 ++++-- src/filesystem/lib.ts | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/filesystem/__tests__/lib.test.ts b/src/filesystem/__tests__/lib.test.ts index bfe8987bfd..2ff098e0af 100644 --- a/src/filesystem/__tests__/lib.test.ts +++ b/src/filesystem/__tests__/lib.test.ts @@ -65,8 +65,10 @@ describe('Lib Functions', () => { }); it('handles negative numbers', () => { - // Negative numbers will result in NaN for the log calculation - expect(formatSize(-1024)).toContain('NaN'); + // Negative numbers should return '0 B' as file sizes cannot be negative + expect(formatSize(-1)).toBe('0 B'); + expect(formatSize(-1024)).toBe('0 B'); + expect(formatSize(-1000000)).toBe('0 B'); expect(formatSize(-0)).toBe('0 B'); }); diff --git a/src/filesystem/lib.ts b/src/filesystem/lib.ts index 240ca0d476..5fa9287663 100644 --- a/src/filesystem/lib.ts +++ b/src/filesystem/lib.ts @@ -43,7 +43,7 @@ export interface SearchResult { // Pure Utility Functions export function formatSize(bytes: number): string { const units = ['B', 'KB', 'MB', 'GB', 'TB']; - if (bytes === 0) return '0 B'; + if (bytes <= 0) return '0 B'; const i = Math.floor(Math.log(bytes) / Math.log(1024));