file-upload-formatter-lite
v1.0.0
Published
A lightweight, browser-side utility to format uploaded files into a structured, clean metadata object.
Maintainers
Readme
File Upload Formatter Lite
A lightweight, browser-side utility to format uploaded files into a structured, clean metadata object.
Features
- Safe Filenames: Slugifies filenames (removes spaces, special chars).
- File Size Formatting: Auto-converts bytes to KB, MB, GB.
- Hashed Filenames: Generates unique UUID-based filenames.
- No Backend Dependencies: Works entirely in the browser.
- TypeScript Support: Fully typed.
Installation
npm install file-upload-formatter-liteUsage
1. Import the utility
import { formatUploadedFile } from 'file-upload-formatter-lite';2. Use in your application
const handleFileUpload = (event) => {
const file = event.target.files[0];
const formattedData = formatUploadedFile(file);
console.log(formattedData);
};Output Object Structure
{
originalName: "My Document.pdf",
safeName: "my-document.pdf",
extension: ".pdf",
mimeType: "application/pdf",
sizeInBytes: 5242880,
sizeInKB: "5120.00 KB",
sizeInMB: "5.00 MB",
sizeInGB: "0.00 GB",
prettySize: "5 MB",
hashedName: "a1b2c3d4-e5f6-7890-1234-567890abcdef.pdf",
file: File // The original File object
}API
formatUploadedFile(file: File): FormattedFile | null
Takes a native File object and returns a structured object with metadata.
formatSize(bytes: number): string
Auto-formats bytes to the best fitting unit (e.g., "2.3 MB").
slugify(filename: string): string
Converts a filename into a safe, slugified string.
generateHashedName(filename: string): string
Generates a unique filename using UUID or a timestamp fallback.
License
ISC
