diff --git a/packages/lib/helpers.js b/packages/lib/helpers.js index 17464ec5..c3fe23fb 100644 --- a/packages/lib/helpers.js +++ b/packages/lib/helpers.js @@ -90,6 +90,24 @@ function escapeRegExp(text) { return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); } +/** + * Format byte count for human readable display + * + * Shortens a large number of bytes using the kB/MB/GB/TB prefixes. + * @param {number} bytes - Count of bytes to format. + * @returns {string} formatted text. + */ +function formatBytes(bytes) { + if (bytes === 0) { + return "0 Bytes"; + } + + let units = [" Bytes", " kB", " MB", " GB", " TB"]; + let factor = 1000; + let power = Math.min(Math.floor(Math.log(bytes) / Math.log(factor)), units.length); + return (power > 0 ? (bytes / factor ** power).toFixed(2) : bytes) + units[power]; +} + module.exports = { basicType, @@ -97,4 +115,5 @@ module.exports = { timeout, readStream, escapeRegExp, + formatBytes, }; diff --git a/packages/web_ui/src/components/SavesList.jsx b/packages/web_ui/src/components/SavesList.jsx index 133cffac..686c567c 100644 --- a/packages/web_ui/src/components/SavesList.jsx +++ b/packages/web_ui/src/components/SavesList.jsx @@ -6,7 +6,7 @@ import { import CaretLeftOutlined from "@ant-design/icons/CaretLeftOutlined"; import LeftOutlined from "@ant-design/icons/LeftOutlined"; -import { libLink } from "@clusterio/lib"; +import { libLink, libHelpers } from "@clusterio/lib"; import { useAccount } from "../model/account"; import ControlContext from "./ControlContext"; @@ -179,17 +179,6 @@ function TransferModal(props) { ; } -function formatBytes(bytes) { - if (bytes === 0) { - return "0 Bytes"; - } - - let units = [" Bytes", " kB", " MB", " GB", " TB"]; - let factor = 1000; - let power = Math.min(Math.floor(Math.log(bytes) / Math.log(factor)), units.length); - return (power > 0 ? (bytes / factor ** power).toFixed(2) : bytes) + units[power]; -} - export default function SavesList(props) { let account = useAccount(); let control = useContext(ControlContext); @@ -214,7 +203,7 @@ export default function SavesList(props) { title: "Size", key: "size", responsive: ["sm"], - render: save => formatBytes(save.size), + render: save => libHelpers.formatBytes(save.size), align: "right", sorter: (a, b) => a.size - b.size, },