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

Added a new method to get all extensions. #26

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ Get the default extension for a content-type.
mime.extension('application/octet-stream') // 'bin'
```


### mime.allExtensions(type)

Get all the extensions for a content-type.

```js
mime.allExtensions('audio/mpeg') // ["mpga", "mp2", "mp2a", "mp3", "m2a", "m3a"]
```

### mime.charset(type)

Lookup the implied default charset of a content-type.
Expand Down
16 changes: 15 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ exports.charset = charset
exports.charsets = { lookup: charset }
exports.contentType = contentType
exports.extension = extension
exports.allExtensions = allExtensions
exports.extensions = Object.create(null)
exports.lookup = lookup
exports.types = Object.create(null)
Expand Down Expand Up @@ -105,6 +106,19 @@ function contentType (str) {
*/

function extension (type) {
var extensions = allExtensions(type)

return extensions ? extensions[0] : extensions
}

/**
* Get all the extensions for a MIME type.
*
* @param {string} type
* @return {boolean|Array}
*/

function allExtensions (type) {
if (!type || typeof type !== 'string') {
return false
}
Expand All @@ -119,7 +133,7 @@ function extension (type) {
return false
}

return exts[0]
return exts
}

/**
Expand Down
31 changes: 31 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,37 @@ describe('mimeTypes', function () {
})
})

describe('.allExtensions(type)', function () {
it('should return a list of extensions for mime type', function () {
assert.deepEqual(mimeTypes.allExtensions('text/html'), ['html', 'htm', 'shtml'])
assert.deepEqual(mimeTypes.allExtensions(' text/html'), ['html', 'htm', 'shtml'])
assert.deepEqual(mimeTypes.allExtensions('text/html '), ['html', 'htm', 'shtml'])
})

it('should return false for unknown type', function () {
assert.strictEqual(mimeTypes.allExtensions('application/x-bogus'), false)
})

it('should return false for non-type string', function () {
assert.strictEqual(mimeTypes.allExtensions('bogus'), false)
})

it('should return false for non-strings', function () {
assert.strictEqual(mimeTypes.allExtensions(null), false)
assert.strictEqual(mimeTypes.allExtensions(undefined), false)
assert.strictEqual(mimeTypes.allExtensions(42), false)
assert.strictEqual(mimeTypes.allExtensions({}), false)
})

it('should return extension for mime type with parameters', function () {
assert.deepEqual(mimeTypes.allExtensions('text/html;charset=UTF-8'), ['html', 'htm', 'shtml'])
assert.deepEqual(mimeTypes.allExtensions('text/HTML; charset=UTF-8'), ['html', 'htm', 'shtml'])
assert.deepEqual(mimeTypes.allExtensions('text/html; charset=UTF-8'), ['html', 'htm', 'shtml'])
assert.deepEqual(mimeTypes.allExtensions('text/html; charset=UTF-8 '), ['html', 'htm', 'shtml'])
assert.deepEqual(mimeTypes.allExtensions('text/html ; charset=UTF-8'), ['html', 'htm', 'shtml'])
})
})

describe('.lookup(extension)', function () {
it('should return mime type for ".html"', function () {
assert.equal(mimeTypes.lookup('.html'), 'text/html')
Expand Down