From ecf46d083f85484a60aa04284ed93670d4d04a60 Mon Sep 17 00:00:00 2001 From: gh0sTedBuddy Date: Mon, 1 Apr 2024 22:23:23 +0100 Subject: [PATCH] enh(mimetypes) MimeType handling extended functionality --- src/IO/MimeTypeMap.js | 136 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 112 insertions(+), 24 deletions(-) diff --git a/src/IO/MimeTypeMap.js b/src/IO/MimeTypeMap.js index 4dff0aa..801e357 100644 --- a/src/IO/MimeTypeMap.js +++ b/src/IO/MimeTypeMap.js @@ -1,24 +1,112 @@ -export default new Map([ - ['text', 'text/plain'], - ['html', 'text/html'], - ['css', 'text/css'], - ['csv', 'text/csv'], - ['js', 'text/javascript'], - ['gif', 'image/gif'], - ['jpeg', 'image/jpeg'], - ['png', 'image/png'], - ['svg', 'image/svg+xml'], - ['midi', 'audio/midi'], - ['mp3', 'audio/mpeg'], - ['oga', 'audio/ogg'], - ['mp4', 'video/mp4'], - ['mpeg', 'video/mpeg'], - ['ogv', 'video/ogg'], - ['zip', 'application/zip'], - ['pdf', 'application/pdf'], - ['epub', 'application/epub+zip'], - ['gz', 'application/gzip'], - ['bin', 'application/octet-stream'], - ['json', 'application/json'], - ['jsonld', 'application/ld+json'], -]); +/** + * 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} + * @private + */ + static mimeTypes = new Map([ + ['text', 'text/plain'], + ['html', 'text/html'], + ['css', 'text/css'], + ['csv', 'text/csv'], + ['js', 'text/javascript'], + ['gif', 'image/gif'], + ['jpeg', 'image/jpeg'], + ['png', 'image/png'], + ['svg', 'image/svg+xml'], + ['midi', 'audio/midi'], + ['mp3', 'audio/mpeg'], + ['oga', 'audio/ogg'], + ['mp4', 'video/mp4'], + ['mpeg', 'video/mpeg'], + ['ogv', 'video/ogg'], + ['zip', 'application/zip'], + ['pdf', 'application/pdf'], + ['epub', 'application/epub+zip'], + ['gz', 'application/gzip'], + ['bin', 'application/octet-stream'], + ['json', 'application/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} 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); + } +}