Get corresponding file extension array based on MIME type.
| Argument |
Description |
Type |
Array |
File extension array (without dot) |
string[] |
| Parameter |
Description |
Type |
Default |
mimeType |
MIME type |
string |
Required |
import { getExtensions } from 'ranuts';
const exts = getExtensions('image/jpeg');
console.log(exts); // ['jpeg', 'jpg', 'jpe']
import { getExtensions } from 'ranuts';
const jsExts = getExtensions('application/javascript');
console.log(jsExts); // ['js', 'jsx', 'ts', 'tsx']
import { getExtensions } from 'ranuts';
function isValidImageFile(filename, mimeType) {
const exts = getExtensions(mimeType);
const fileExt = filename.split('.').pop();
return exts.includes(fileExt);
}
console.log(isValidImageFile('photo.jpg', 'image/jpeg')); // true
- Return format: Returned extensions don't include dot (
.), e.g., 'jpg' instead of '.jpg'.
- Multiple extensions: One MIME type may correspond to multiple extensions, returns all matching extensions.
- Empty array: If MIME type doesn't exist, returns empty array.
- Use case: Commonly used for file type validation, file upload checking, etc.