Skip to content

Commit

Permalink
Review PrettyBytes typing
Browse files Browse the repository at this point in the history
  • Loading branch information
JBWatenbergScality committed May 2, 2023
1 parent f735f63 commit 27fdf56
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
32 changes: 0 additions & 32 deletions src/lib/components/prettybytes/PrettyBytes.component.ts

This file was deleted.

38 changes: 38 additions & 0 deletions src/lib/components/prettybytes/PrettyBytes.component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import prettyBytes from 'pretty-bytes';
type Props = {
bytes?: number | null;
decimals?: number | null;
};

function PrettyBytes({ bytes, decimals = 0 }: Props) {
if (bytes === undefined || bytes === null) return <>-</>;
let fractionDigits = decimals;
if (
!Number.isFinite(decimals) ||
(decimals !== undefined && decimals !== null && decimals < 0) ||
bytes < 1024
)
fractionDigits = 0;
if (!Number.isFinite(bytes) || !bytes || bytes < 0)
return bytes === Infinity ||
bytes === -Infinity ||
(bytes === 0 && !Object.is(bytes, -0)) ? (
<>0 B</>
) : (
<>-</>
);
return (
<>
{prettyBytes(bytes, {
locale: 'en',
binary: true,
minimumFractionDigits: fractionDigits ?? undefined,
maximumFractionDigits: fractionDigits ?? undefined,
})
.replace(/,/g, ' ')
.replace(/kiB/g, 'KiB')}
</>
);
}

export { PrettyBytes };

0 comments on commit 27fdf56

Please sign in to comment.