From e62ae8bbcca11fff14b33e127966f0117f78400d Mon Sep 17 00:00:00 2001 From: "Mitchell, Kevin" Date: Wed, 11 Nov 2015 15:48:55 -0500 Subject: [PATCH] Update to provide new `extensions` method to expose full list of valid extensions for a mimetype --- README.md | 15 +++++++++++++++ build/test.js | 13 ++++++++++++- mime.js | 21 +++++++++++++++------ 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 506fbe55..b3307799 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,15 @@ mime.extension('text/html'); // => 'html' mime.extension('application/octet-stream'); // => 'bin' ``` +### mime.extensions(type) +Get all extensions for `type` + +```js +mime.extensions('text/html'); // => ['html', 'htm'
] +mime.extensions('application/x-latex'); // => ['latex'] +mime.extensions('image/jpeg'); // => ["jpeg","jpg","jpe"] +``` + ### mime.charsets.lookup() Map mime-type to charset @@ -80,6 +89,12 @@ The first entry in the extensions array is returned by `mime.extension()`. E.g. mime.extension('text/x-some-format'); // => 'x-sf' ``` +The full extensions array is returned by `mime.extensions()`. E.g. + +```js +mime.extensions('text/x-some-format'); // => ['x-sf', 'x-sft', 'x-sfml'] +``` + ### mime.load(filepath) Load mappings from an Apache ".types" format file diff --git a/build/test.js b/build/test.js index 58b9ba7c..7c91c27c 100644 --- a/build/test.js +++ b/build/test.js @@ -22,7 +22,7 @@ assert.equal('application/octet-stream', mime.lookup('text.nope')); // unrecogni assert.equal('fallback', mime.lookup('text.fallback', 'fallback')); // alternate default // -// Test extensions +// Test extension // assert.equal('txt', mime.extension(mime.types.text)); @@ -36,6 +36,17 @@ assert.equal('html', mime.extension('text/html ; charset=UTF-8')); assert.equal('html', mime.extension('text/html;charset=UTF-8')); assert.equal('html', mime.extension('text/Html;charset=UTF-8')); assert.equal(undefined, mime.extension('unrecognized')); +assert.equal('jpeg', mime.extension('image/jpeg')) + +// +// Test extensions +// + +assert.deepEqual(['html', 'htm'], mime.extensions('text/html')); +assert.deepEqual(['bin','dms','lrf','mar','so','dist','distz','pkg','bpk','dump','elc','deploy','buffer'], mime.extensions('application/octet-stream')) +assert.deepEqual(['jpeg','jpg','jpe'], mime.extensions('image/jpeg')) +assert.deepEqual(['latex'], mime.extensions('application/x-latex')); +assert.equal(undefined, mime.extensions('fake/mimetype')); // // Test node.types lookups diff --git a/mime.js b/mime.js index 341b6a5c..e713f3a3 100644 --- a/mime.js +++ b/mime.js @@ -6,7 +6,7 @@ function Mime() { this.types = Object.create(null); // Map of mime type -> extension - this.extensions = Object.create(null); + this.exts = Object.create(null); } /** @@ -31,8 +31,8 @@ Mime.prototype.define = function (map) { } // Default extension is the first one we encounter - if (!this.extensions[type]) { - this.extensions[type] = exts[0]; + if (!this.exts[type]) { + this.exts[type] = exts; } } }; @@ -72,12 +72,21 @@ Mime.prototype.lookup = function(path, fallback) { return this.types[ext] || fallback || this.default_type; }; + /** - * Return file extension associated with a mime type + * Return file extensions associated with a mime type */ -Mime.prototype.extension = function(mimeType) { +Mime.prototype.extensions = function(mimeType) { var type = mimeType.match(/^\s*([^;\s]*)(?:;|\s|$)/)[1].toLowerCase(); - return this.extensions[type]; + return this.exts[type]; +}; + +/** + * Return default file extension associated with a mime type + */ +Mime.prototype.extension = function(mimeType) { + var extensions = this.extensions(mimeType); + return extensions && extensions[0]; }; // Default instance