File size: 981 Bytes
676fc08 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | import { saveAs } from "file-saver";
export function downloadFile(
content: string,
filename: string,
fileType: string
) {
// Prepending a BOM sequence at the beginning of the text file to encoded as UTF-8.
// const BOM = new Uint8Array([0xef, 0xbb, 0xbf]);
const file = new File([content], filename, { type: fileType });
saveAs(file);
}
export function formatSize(
size: number,
pointLength = 2,
units?: string[]
): string {
if (typeof size === "undefined") return "0";
if (typeof units === "undefined")
units = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
let unit;
while ((unit = units.shift() as string) && size >= 1024) size = size / 1024;
return (
(unit === units[0]
? size
: size
.toFixed(pointLength === undefined ? 2 : pointLength)
.replace(".00", "")) +
" " +
unit
);
}
export function getTextByteSize(str: string): number {
return new TextEncoder().encode(str).length;
}
|