enh(mimetypes) MimeType handling extended functionality

This commit is contained in:
gh0sTedBuddy
2024-04-01 22:23:23 +01:00
parent 548cfb02e0
commit ecf46d083f

View File

@@ -1,4 +1,14 @@
export default new Map([ /**
* A custom class that wraps a Map and provides additional functionality for MIME types.
*/
export default class MimeTypes {
/**
* The underlying Map object that stores the MIME types.
*
* @type {Map<string, string>}
* @private
*/
static mimeTypes = new Map([
['text', 'text/plain'], ['text', 'text/plain'],
['html', 'text/html'], ['html', 'text/html'],
['css', 'text/css'], ['css', 'text/css'],
@@ -21,4 +31,82 @@ export default new Map([
['bin', 'application/octet-stream'], ['bin', 'application/octet-stream'],
['json', 'application/json'], ['json', 'application/json'],
['jsonld', 'application/ld+json'], ['jsonld', 'application/ld+json'],
]); ['webp', 'image/webp'],
['avif', 'image/avif'],
['ico', 'image/x-icon'],
['webm', 'video/webm'],
['wav', 'audio/wav'],
['aac', 'audio/aac'],
['flac', 'audio/flac'],
['7z', 'application/x-7z-compressed'],
['rar', 'application/x-rar-compressed'],
['tar', 'application/x-tar'],
['xml', 'application/xml'],
['yaml', 'application/x-yaml'],
['wasm', 'application/wasm'],
['otf', 'font/otf'],
['ttf', 'font/ttf'],
['woff', 'font/woff'],
['woff2', 'font/woff2'],
]);
/**
* Checks if a MIME type exists for a given file extension.
* File extensions are case-insensitive.
*
* @param {string} extension - The file extension.
* @returns {boolean} True if the MIME type exists, false otherwise.
* @static
*/
static has(extension) {
return MimeTypes.mimeTypes.has(extension.toLowerCase());
}
/**
* Retrieves the MIME type for a given file extension.
* File extensions are case-insensitive.
*
* @param {string} extension - The file extension.
* @returns {string|undefined} The corresponding MIME type or undefined if not found.
*/
static get(extension) {
return MimeTypes.mimeTypes.get(extension.toLowerCase());
}
/**
* Sets the MIME type for a given file extension.
* File extensions are case-insensitive.
*
* @param {string} extension - The file extension.
* @param {string} mimeType - The MIME type to set.
*/
static set(extension, mimeType) {
MimeTypes.mimeTypes.set(extension.toLowerCase(), mimeType);
}
/**
* Retrieves an iterator of MIME types belonging to a specific category.
*
* @param {string} category - The category to filter by (e.g., 'image', 'audio').
* @returns {IterableIterator<string>} An iterator of MIME types belonging to the specified category.
*/
static *getByCategory(category) {
for (const mimeType of MimeTypes.mimeTypes.values()) {
if (mimeType.startsWith(`${category}/`)) {
yield mimeType;
}
}
}
/**
* Adds a new MIME type for a given file extension.
* File extensions are case-insensitive.
*
* @param {string} extension - The file extension.
* @param {string} mimeType - The MIME type to add.
* @static
*/
static add(extension, mimeType) {
MimeTypes.mimeTypes.set(extension.toLowerCase(), mimeType);
}
}