Skip to content

Instantly share code, notes, and snippets.

@takumifukasawa
Created November 4, 2020 13:57
Show Gist options
  • Save takumifukasawa/971668e0d36c20f1e78dc36a693f8de7 to your computer and use it in GitHub Desktop.
Save takumifukasawa/971668e0d36c20f1e78dc36a693f8de7 to your computer and use it in GitHub Desktop.
typescript: get file size with unit of [Byte ~ GB]
const units = ["Byte", "KB", "MB", "GB"];
/**
* ファイルサイズに応じて、ファイル容量の数値と単位を取得
* 多めに表示するためにceilしている
*
* @export
* @param {number} size
* @param {number} [floating=1]
* @returns
* @returns {([number, string] | [])}
*/
export default function getFileSizeWithUnit(
size: number,
floating: number = 1
): [number, string] | [] {
for (let i = 0; i < units.length; i += 1) {
if (size < 1024 ** (i + 1)) {
const floatingBase = 10 ** floating;
const num =
Math.ceil((size * floatingBase) / Math.max(1, 1024 ** i)) /
floatingBase;
return [num, units[i]];
}
}
console.error("[getFileSizeWithUnit]: too large file size");
return [];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment