Skip to content

Commit b5d0244

Browse files
nuloneclaude
andcommitted
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 <noreply@anthropic.com>
1 parent 55a3056 commit b5d0244

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

src/filesystem/__tests__/lib.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ describe('Lib Functions', () => {
6565
});
6666

6767
it('handles negative numbers', () => {
68-
// Negative numbers will result in NaN for the log calculation
69-
expect(formatSize(-1024)).toContain('NaN');
68+
// Negative numbers should return '0 B' as file sizes cannot be negative
69+
expect(formatSize(-1)).toBe('0 B');
70+
expect(formatSize(-1024)).toBe('0 B');
71+
expect(formatSize(-1000000)).toBe('0 B');
7072
expect(formatSize(-0)).toBe('0 B');
7173
});
7274

src/filesystem/lib.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export interface SearchResult {
4343
// Pure Utility Functions
4444
export function formatSize(bytes: number): string {
4545
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
46-
if (bytes === 0) return '0 B';
46+
if (bytes <= 0) return '0 B';
4747

4848
const i = Math.floor(Math.log(bytes) / Math.log(1024));
4949

0 commit comments

Comments
 (0)