Files
rustfs-console/lib/functions.ts
T
安正超 6f1193f6e4 chore: replace codebase with Next.js version (console-new) (#46)
* chore: replace codebase with Next.js version (console-new)

- Migrate from Nuxt/Vue to Next.js/React
- Same repo, dev-pr branch has shared history with main for PR

* chore: wip

* ci: pin pnpm to latest in workflows, packageManager 10.19.0

* ci: use pnpm version from package.json only (fix version mismatch)

* fix: CI - add root page redirect, fix TS types and lint (api-client, aws4fetch, upload-task, hooks)

* fix(ci): add type-check script for TypeScript Check job

* fix(ci): resolve Code Formatting job - ESLint errors (setState-in-effect, refs, static-components, preserve-memoization)

* fix(ci): add lint:fix script for quality workflow Auto-fix step

* fix(ci): add SVG module type declaration for TypeScript Check
2026-02-01 21:23:31 +08:00

52 lines
1.5 KiB
TypeScript

const UNITS = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
const K8S_UNITS = ["B", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"]
export function niceBytes(x: string, isK8sUnits = false): string {
const n = Number.parseInt(x, 10) || 0
return formatBytes(n, isK8sUnits)
}
export function formatBytes(n: number, isK8sUnits = false): string {
const l = Math.floor(Math.log(n) / Math.log(1024)) || 0
if (l < 0) {
return "0 B"
}
const value = n / 1024 ** l
return `${value.toFixed(1)} ${isK8sUnits ? K8S_UNITS[l] : UNITS[l]}`
}
export function getBytes(value: string, unit: string, fromK8s = false): string {
return convertToBytes(value, unit, fromK8s).toString(10)
}
function convertToBytes(value: string, unit: string, fromK8s = false): number {
const vl = Number.parseFloat(value)
const unitsTake = fromK8s ? K8S_UNITS : UNITS
const powFactor = unitsTake.findIndex((element) => element === unit)
if (powFactor === -1) {
return 0
}
return vl * 1024 ** powFactor
}
export function makeRandomString(length = 20): string {
let initstr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
const arr = initstr.split("")
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
const current = arr[i]
const swapWith = arr[j]
if (current !== undefined && swapWith !== undefined) {
arr[i] = swapWith
arr[j] = current
}
}
initstr = arr.join("")
return initstr.slice(0, length)
}