Skip to content

Commit 43bf268

Browse files
committed
fix(progress): clamp computed percentage
1 parent 364a678 commit 43bf268

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

src/__tests__/utils.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ describe('computeProgress', () => {
8585
expect(computeProgress(1000, 1000)).toBe(100);
8686
});
8787

88+
test('caps progress at 100 when received exceeds total', () => {
89+
expect(computeProgress(1200, 1000)).toBe(100);
90+
});
91+
92+
test('floors progress at 0 when received is negative', () => {
93+
expect(computeProgress(-100, 1000)).toBe(0);
94+
});
95+
8896
test('returns 50 for half progress', () => {
8997
expect(computeProgress(500, 1000)).toBe(50);
9098
});

src/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ export const assertWeb = () => {
109109
};
110110

111111
export const computeProgress = (received: number, total: number): number =>
112-
total > 0 ? Math.floor((received / total) * 100) : 0;
112+
total > 0
113+
? Math.min(100, Math.max(0, Math.floor((received / total) * 100)))
114+
: 0;
113115

114116
export const fetchWithTimeout = (
115117
url: string,

0 commit comments

Comments
 (0)