Node.js: formattare le dimensioni dei file
In Node.js possiamo formattare le dimensioni dei file espresse in byte.
In Node.js possiamo formattare le dimensioni dei file espresse in byte.
La soluzione รจ la seguente:
'use strict';
const toHumanFileSize = (bytes) => {
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes === 0) return 'n/a';
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10);
if (i === 0) return `${bytes} ${sizes[i]})`;
return `${(bytes / (1024 ** i)).toFixed(1)} ${sizes[i]}`;
};
module.exports = toHumanFileSize;
Commenti