2018-12-17 02:57:34 +00:00
|
|
|
import { format } from 'date-fns';
|
2018-08-15 04:17:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the human readable filesize for a given number of bytes. This
|
|
|
|
* uses 1024 as the base, so the response is denoted accordingly.
|
|
|
|
*/
|
2018-12-17 02:57:34 +00:00
|
|
|
export function readableSize (bytes: number): string {
|
2018-08-15 04:17:10 +00:00
|
|
|
if (Math.abs(bytes) < 1024) {
|
|
|
|
return `${bytes} Bytes`;
|
|
|
|
}
|
|
|
|
|
2018-12-17 02:57:34 +00:00
|
|
|
let u: number = -1;
|
|
|
|
const units: Array<string> = ['KiB', 'MiB', 'GiB', 'TiB'];
|
2018-08-15 04:17:10 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
bytes /= 1024;
|
|
|
|
u++;
|
|
|
|
} while (Math.abs(bytes) >= 1024 && u < units.length - 1);
|
|
|
|
|
|
|
|
return `${bytes.toFixed(1)} ${units[u]}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format the given date as a human readable string.
|
|
|
|
*/
|
2018-12-17 02:57:34 +00:00
|
|
|
export function formatDate (date: string): string {
|
2018-08-15 04:17:10 +00:00
|
|
|
return format(date, 'MMM D, YYYY [at] HH:MM');
|
|
|
|
}
|