Files
SCM-Manager/scm-ui-components/packages/ui-components/src/FileSize.js

28 lines
579 B
JavaScript
Raw Normal View History

2018-09-27 16:32:37 +02:00
// @flow
import React from "react";
type Props = {
bytes: number
};
class FileSize extends React.Component<Props> {
static format(bytes: number) {
2018-09-27 16:32:37 +02:00
if (!bytes) {
return "0 B";
2018-09-27 16:32:37 +02:00
}
const units = ["B", "K", "M", "G", "T", "P", "E", "Z", "Y"];
const i = Math.floor(Math.log(bytes) / Math.log(1000));
2018-09-27 16:32:37 +02:00
const size = i === 0 ? bytes : (bytes / 1000 ** i).toFixed(2);
2018-09-27 16:32:37 +02:00
return `${size} ${units[i]}`;
}
render() {
const formattedBytes = FileSize.format(this.props.bytes);
return <span>{formattedBytes}</span>;
}
}
export default FileSize;