@nasriya/mimex
Version:
A lightweight MIME type and file extension manager for Node.js and web applications. Mimex provides fast lookup, extension-to-MIME and MIME-to-extension resolution, and easy registration of custom types.
59 lines (58 loc) • 2.15 kB
JavaScript
import { mimexData, mimes, extensions } from "./mimeTypes.js";
class MimeX {
/**
* Retrieves the MIME type associated with the given file extension.
* @param extension - The file extension (with or without leading dot).
* @returns The MIME type associated with the given file extension, or undefined if not found.
*/
getMimes(extension) {
// Normalize extension to start with a dot
const ext = (extension.startsWith(".") ? extension : `.${extension}`);
if (!extensions.includes(ext)) {
return undefined;
}
const item = mimexData.find(item => item.extensions.includes(ext));
return item.mimes;
}
/**
* Retrieves the file extensions associated with the given MIME type.
* @param mime - The MIME type.
* @returns The file extensions associated with the given MIME type, or undefined if not found.
*/
getExtensions(mime) {
if (!mimes.includes(mime)) {
return undefined;
}
const item = mimexData.find(item => item.mimes.includes(mime));
return item?.extensions;
}
/**
* Checks if the given string is a valid MIME type supported by Mimex.
* @param mime - The MIME type to check.
* @returns True if the MIME type is supported, false otherwise.
*/
isMime(mime) {
return mimes.includes(mime);
}
/**
* Checks if the given string is a valid file extension supported by Mimex.
* @param extension - The file extension to check.
* @returns True if the file extension is supported, false otherwise.
*/
isExtension(extension) {
const ext = (extension.startsWith(".") ? extension : `.${extension}`);
return extensions.includes(ext);
}
/**
* Retrieves the list of supported MIME types.
* @returns The list of supported MIME types.
*/
get mimes() { return mimes; }
/**
* Retrieves the list of supported file extensions.
* @returns The list of supported file extensions.
*/
get extensions() { return extensions; }
}
const mimex = new MimeX();
export default mimex;