mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-08 22:45:45 +01:00
move fileSize to ui-components so it available for plugins
This commit is contained in:
27
scm-ui-components/packages/ui-components/src/FileSize.js
Normal file
27
scm-ui-components/packages/ui-components/src/FileSize.js
Normal file
@@ -0,0 +1,27 @@
|
||||
// @flow
|
||||
import React from "react";
|
||||
|
||||
type Props = {
|
||||
bytes: number
|
||||
};
|
||||
|
||||
class FileSize extends React.Component<Props> {
|
||||
static format(bytes: number) {
|
||||
if (!bytes) {
|
||||
return "0 B";
|
||||
}
|
||||
|
||||
const units = ["B", "K", "M", "G", "T", "P", "E", "Z", "Y"];
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(1024));
|
||||
|
||||
const size = i === 0 ? bytes : (bytes / 1024 ** i).toFixed(2);
|
||||
return `${size} ${units[i]}`;
|
||||
}
|
||||
|
||||
render() {
|
||||
const formattedBytes = FileSize.format(this.props.bytes);
|
||||
return <span>{formattedBytes}</span>;
|
||||
}
|
||||
}
|
||||
|
||||
export default FileSize;
|
||||
@@ -0,0 +1,10 @@
|
||||
import FileSize from "./FileSize";
|
||||
|
||||
it("should format bytes", () => {
|
||||
expect(FileSize.format(0)).toBe("0 B");
|
||||
expect(FileSize.format(160)).toBe("160 B");
|
||||
expect(FileSize.format(6304)).toBe("6.16 K");
|
||||
expect(FileSize.format(28792588)).toBe("27.46 M");
|
||||
expect(FileSize.format(1369510189)).toBe("1.28 G");
|
||||
expect(FileSize.format(42949672960)).toBe("40.00 G");
|
||||
});
|
||||
@@ -19,6 +19,7 @@ export { default as Paginator } from "./Paginator.js";
|
||||
export { default as LinkPaginator } from "./LinkPaginator.js";
|
||||
export { default as StatePaginator } from "./StatePaginator.js";
|
||||
|
||||
export { default as FileSize } from "./FileSize.js";
|
||||
export { default as ProtectedRoute } from "./ProtectedRoute.js";
|
||||
export { default as Help } from "./Help";
|
||||
export { default as HelpIcon } from "./HelpIcon";
|
||||
|
||||
Reference in New Issue
Block a user