Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose full list of extensions for a mimetype #136

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion build/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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
Expand Down
21 changes: 15 additions & 6 deletions mime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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;
}
}
};
Expand Down Expand Up @@ -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
Expand Down