25 lines
850 B
TypeScript
25 lines
850 B
TypeScript
/**
|
|
* Formatting file sizes and file names for display.
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
/** Returns `bytes` as a size in B, KB or MB. */
|
|
export function formatBytes(bytes: number): string {
|
|
if (bytes < 1024) return `${bytes} B`;
|
|
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
|
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
}
|
|
|
|
/**
|
|
* Returns the file name `name` as a readable dump title: the extension goes (it
|
|
* is noise in a feed, and the dump already carries its MIME type), and
|
|
* underscore runs become spaces, so `holiday_photo-01.final.jpg` reads as
|
|
* `holiday photo-01.final`. Names that are *only* an extension (`.gitignore`)
|
|
* are left alone.
|
|
*/
|
|
export function titleFromFileName(name: string): string {
|
|
const withoutExt = name.replace(/(?!^)\.[^.]+$/, "");
|
|
return withoutExt.replace(/_+/g, " ").trim() || name;
|
|
}
|