feat(accounts): highlight negative disk usage with recalculate hint
This commit is contained in:
@@ -68,6 +68,15 @@ describe('bytesToHuman', () => {
|
||||
it('should convert 1099511627776 bytes to 1 TB', () => {
|
||||
expect(bytesToHuman(1099511627776)).toEqual({ value: 1, unit: 'TB' });
|
||||
});
|
||||
|
||||
it('should format negative byte counts with a minus sign', () => {
|
||||
expect(bytesToHuman(-9515272)).toEqual({ value: -9.07, unit: 'MB' });
|
||||
});
|
||||
|
||||
it('should treat non-finite byte counts as empty usage', () => {
|
||||
expect(bytesToHuman(Number.NaN)).toEqual({ value: 0, unit: 'B' });
|
||||
expect(bytesToHuman(Number.POSITIVE_INFINITY)).toEqual({ value: 0, unit: 'B' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('humanToBytes', () => {
|
||||
@@ -108,6 +117,10 @@ describe('formatSize', () => {
|
||||
it('should format large values in TB', () => {
|
||||
expect(formatSize(1099511627776)).toBe('1 TB');
|
||||
});
|
||||
|
||||
it('should format negative byte counts with a minus sign', () => {
|
||||
expect(formatSize(-9515272)).toBe('-9.07 MB');
|
||||
});
|
||||
});
|
||||
|
||||
describe('msToHuman', () => {
|
||||
|
||||
@@ -15,15 +15,18 @@ const SIZE_FACTORS: Record<string, number> = {
|
||||
};
|
||||
|
||||
export function bytesToHuman(bytes: number): { value: number; unit: string } {
|
||||
if (bytes === 0) return { value: 0, unit: 'B' };
|
||||
if (!Number.isFinite(bytes) || bytes === 0) return { value: 0, unit: 'B' };
|
||||
|
||||
const sign = bytes < 0 ? -1 : 1;
|
||||
const abs = Math.abs(bytes);
|
||||
|
||||
for (let i = SIZE_UNITS.length - 1; i >= 0; i--) {
|
||||
const unit = SIZE_UNITS[i];
|
||||
const factor = SIZE_FACTORS[unit];
|
||||
const v = bytes / factor;
|
||||
const v = abs / factor;
|
||||
if (v >= 1) {
|
||||
const rounded = Math.round(v * 100) / 100;
|
||||
return { value: rounded, unit };
|
||||
return { value: sign * rounded, unit };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user