UNPKG

lamers-mime

Version:

Minimalist MIME type mapping package

25 lines (24 loc) 954 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.mimeType = mimeType; exports.extension = extension; const mimetypes_1 = require("./mimetypes"); const path_1 = require("path"); /** * This function returns the MIME type of a file based on its extension. * If the MIME type is not found, it returns 'application/octet-stream'. * @param {string} filename - The name of the file. */ function mimeType(filename) { const ext = (0, path_1.extname)(filename).replace(/^\./, '').toLowerCase(); const mime = Object.keys(mimetypes_1.MIMETYPES).find(key => mimetypes_1.MIMETYPES[key].includes(ext)); return mime || 'application/octet-stream'; } /** * This function returns the extension(s) of a file based on its MIME type. * If the extension is not found, it returns null. * @param {string} mime - The MIME type of the file. */ function extension(mime) { return mimetypes_1.MIMETYPES[mime] || null; }