Skip to content

Commit cc4037b

Browse files
authored
fix: Prevent invalid range for unit format (#2195)
1 parent a132009 commit cc4037b

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

src/i18n/common.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ const formatBytes = (bytes: number): string => {
1616
Math.floor(Math.log(bytes) / Math.log(1024)),
1717
byteUnits.length - 1
1818
);
19+
1920
const unit = byteUnits[order];
2021

22+
if (!unit) {
23+
return '???';
24+
}
25+
2126
const formatter = new Intl.NumberFormat(undefined, {
2227
notation: 'compact',
2328
style: 'unit',
@@ -32,12 +37,17 @@ const formatByteSpeed = (bytesPerSecond: number): string => {
3237
Math.floor(Math.log(bytesPerSecond) / Math.log(1024)),
3338
byteUnits.length - 1
3439
);
35-
const unit = `${byteUnits[order]}-per-second`;
40+
41+
const unit = byteUnits[order];
42+
43+
if (!unit) {
44+
return '???';
45+
}
3646

3747
const formatter = new Intl.NumberFormat(undefined, {
3848
notation: 'compact',
3949
style: 'unit',
40-
unit,
50+
unit: `${unit}-per-second`,
4151
maximumFractionDigits: 1,
4252
});
4353
return formatter.format(bytesPerSecond / Math.pow(1024, order));

0 commit comments

Comments
 (0)