feat: add date, file size and file name formatters
This commit is contained in:
parent
71305f3202
commit
ab27087a79
17
app/lib/format.ts
Normal file
17
app/lib/format.ts
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
const dateFormatter = new Intl.DateTimeFormat('en-GB', { day: 'numeric', month: 'short', year: 'numeric' });
|
||||||
|
|
||||||
|
export function formatDate(value: string): string {
|
||||||
|
const date = new Date(value);
|
||||||
|
return Number.isNaN(date.getTime()) ? '' : dateFormatter.format(date);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function formatFileSize(bytes: number): string {
|
||||||
|
if (bytes < 1024 * 1024) return `${Math.max(1, Math.round(bytes / 1024))} KB`;
|
||||||
|
return `${(bytes / 1024 / 1024).toFixed(2)} MB`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const fileTypeLabel = (file: File) => (file.type.split('/').pop() ?? '').toUpperCase();
|
||||||
|
|
||||||
|
export function toFileName(value: string): string {
|
||||||
|
return value.trim().replace(/\s+/g, '_');
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user