【JavaScript】byte単位のファイルサイズをGB/MB/KBに変換する

const BYTE_VALUE = 1024;
const BYTE_MAP = {
gb: {
value: Math.pow(BYTE_VALUE, 3),
unit: 'GB',
},
mb: {
value: Math.pow(BYTE_VALUE, 2),
unit: 'MB',
},
kb: {
value: BYTE_VALUE,
unit: 'KB',
},
b: {
value: 1,
unit: 'B',
},
};
function convertByte(size) {
let target = BYTE_MAP.b;
if (size >= BYTE_MAP.gb.value) target = BYTE_MAP.gb;
else if (size >= BYTE_MAP.mb.value) target = BYTE_MAP.mb;
else if (size >= BYTE_MAP.kb.value) target = BYTE_MAP.kb;
return Math.floor(size / target.value).toString() + target.unit;
}