X-Git-Url: http://git.squeep.com/?p=squeep-api-dingus;a=blobdiff_plain;f=lib%2Fmime-helper.js;fp=lib%2Fmime-helper.js;h=687d27b7c33bf9baca52c9e47df45313dc7cb542;hp=086dc6a4bf16b8a59765b8f60e97d23fe96f849f;hb=031c170bdaf1d9c331e6f6fc701ce6540c0e6941;hpb=df1c89ca311a06417104e6be0ade6617cbfae60e diff --git a/lib/mime-helper.js b/lib/mime-helper.js index 086dc6a..687d27b 100644 --- a/lib/mime-helper.js +++ b/lib/mime-helper.js @@ -4,15 +4,23 @@ const mimeDb = require('mime-db'); const defaultType = 'application/octet-stream'; + +/** + * Expects mime-db source data as object: + * mime-type: + * source: + * charset: + * compressible: + * extensions: + * Populate `extension` as an index from extensions to mime-types. + */ const extension = {}; for (const [mimeType, entry] of Object.entries(mimeDb)) { - if (entry.extensions) { - entry.extensions.forEach((ext) => { - if (!(ext in extension)) { - extension[ext] = []; - } - extension[ext].push(mimeType); - }); + for (const ext of entry?.extensions || []) { + if (!(ext in extension)) { + extension[ext] = []; + } + extension[ext].push(mimeType); } } @@ -20,8 +28,9 @@ for (const [mimeType, entry] of Object.entries(mimeDb)) { * Return a suitable type for a file extension. * @param {string} ext file extension * @param {string} def type to return if no match + * @returns {string} */ -const extensionToMime = (ext, def = defaultType) => extension[ext] && extension[ext][extension[ext].length - 1] || def; +const extensionToMime = (ext, def = defaultType) => extension?.[ext]?.[0] || def; module.exports = { extensionToMime,