You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
1008 B
31 lines
1008 B
/* Solution inspired by https://stackoverflow.com/a/20732091/2594227 answer */
|
|
const UNITS = ['B', 'KB', 'MB', 'GB', 'TB'] as const
|
|
// eslint-disable-next-line import/prefer-default-export
|
|
export type DisplaySizeUnit = typeof UNITS[number];
|
|
|
|
/**
|
|
* Changes size in bytes to string. If unit is not provided - tries to find the best match.
|
|
*
|
|
* @param {number} size
|
|
* @param {'B' | 'KB' | 'MB' | 'GB' | 'TB'} [unit]
|
|
* @returns {string}
|
|
* @memberof module:@adminjs/design-system
|
|
*/
|
|
const humanFileSize = (size: number | string, unit?: DisplaySizeUnit): string => {
|
|
let foundUnitIndex: number | null = null
|
|
if (unit) {
|
|
foundUnitIndex = UNITS.findIndex((u) => u === unit)
|
|
}
|
|
const unitIndex = foundUnitIndex || Math.min(
|
|
Math.floor(Math.log(+size) / Math.log(1024)),
|
|
UNITS.length,
|
|
)
|
|
|
|
const calculatedSize = (+size / (1024 ** unitIndex))
|
|
const guessedUnit = ['B', 'kB', 'MB', 'GB', 'TB'][unitIndex]
|
|
|
|
return `${Math.round(calculatedSize)} ${guessedUnit}`
|
|
}
|
|
|
|
export { humanFileSize }
|