Mimes to Extensions

A helpful utility that converts mimes to react-dropzone compatible accept format.

ts
1export function mimeToExtensions(mimes: string[]) { 2 return mimes.reduce((acc, mime) => { 3 const extension = "." + mime.split("/")[1]; 4 return { 5 ...acc, 6 [mime]: [extension], 7 }; 8 }, {}); 9}

Usage

tsx
1useDropzone({ 2 accept: mimeToExtensions(["image/jpeg", "image/jpg", "image/png"]) 3})

Converts ["image/jpeg", "image/jpg", "image/png"] to:

json
1{ 2 'image/jpeg': ['.jpeg'], 3 'image/jpg': ['.jpg'], 4 'image/png': ['.png'] 5}

Loved this post?

Comments

Interested in
working
with me?

Let's Connect

Stay in touch 👇

© 2021 - 2025 itsrakesh. v2.