Skip to content

Commit

Permalink
fix: ES5 support (back to node v0.4)
Browse files Browse the repository at this point in the history
  • Loading branch information
broofa committed Dec 22, 2017
1 parent ab5238a commit f14ccb6
Show file tree
Hide file tree
Showing 9 changed files with 1,263 additions and 141 deletions.
101 changes: 52 additions & 49 deletions Mime.js
Expand Up @@ -4,65 +4,68 @@
* @param typeMap [Object] Map of MIME type -> Array[extensions]
* @param ...
*/
class Mime {
constructor() {
this._types = Object.create(null);
this._extensions = Object.create(null);
function Mime() {
this._types = Object.create(null);
this._extensions = Object.create(null);

for (var i = 0; i < arguments.length; i++) {
this.define(arguments[i]);
}
for (var i = 0; i < arguments.length; i++) {
this.define(arguments[i]);
}
}

/**
* Define mimetype -> xtension mappings. Each key is a mime-type that maps
* to an array of extensions associated with the type. The first extension is
* used as the default extension for the type.
*
* e.g. mime.define({'audio/ogg', ['oga', 'ogg', 'spx']});
*
* @param map (Object) type definitions
*/
define(typeMap, force) {
for (let type in typeMap) {
var extensions = typeMap[type];
for (let i = 0; i < extensions.length; i++) {
var ext = extensions[i];
if (!force && (ext in this._types)) {
throw new Error(`Attempt to change mapping for "${ext}" extension from "${this._types[ext]}" to "${type}". Pass \`force=true\` to allow this, otherwise remove "${ext}" from the list of extensions for "${type}".`);
}

this._types[ext] = type;
/**
* Define mimetype -> xtension mappings. Each key is a mime-type that maps
* to an array of extensions associated with the type. The first extension is
* used as the default extension for the type.
*
* e.g. mime.define({'audio/ogg', ['oga', 'ogg', 'spx']});
*
* @param map (Object) type definitions
*/
Mime.prototype.define = function(typeMap, force) {
for (var type in typeMap) {
var extensions = typeMap[type];
for (var i = 0; i < extensions.length; i++) {
var ext = extensions[i];
if (!force && (ext in this._types)) {
throw new Error(
'Attempt to change mapping for "' + ext +
'" extension from "' + this._types[ext] + '" to "' + type +
'". Pass `force=true` to allow this, otherwise remove "' + ext +
'" from the list of extensions for "' + type + '".'
);
}

// Use first extension as default
if (force || !this._extensions[type]) {
this._extensions[type] = extensions[0];
}
this._types[ext] = type;
}

// Use first extension as default
if (force || !this._extensions[type]) {
this._extensions[type] = extensions[0];
}
}
};

/**
* Lookup a mime type based on extension
*/
getType(path) {
path = String(path);
var last = path.replace(/^.*[/\\]/, '').toLowerCase();
var ext = last.replace(/^.*\./, '').toLowerCase();
/**
* Lookup a mime type based on extension
*/
Mime.prototype.getType = function(path) {
path = String(path);
var last = path.replace(/^.*[/\\]/, '').toLowerCase();
var ext = last.replace(/^.*\./, '').toLowerCase();

var hasPath = last.length < path.length;
var hasDot = ext.length < last.length - 1;
var hasPath = last.length < path.length;
var hasDot = ext.length < last.length - 1;

return (hasDot || !hasPath) && this._types[ext] || null;
}
return (hasDot || !hasPath) && this._types[ext] || null;
};

/**
* Return file extension associated with a mime type
*/
getExtension(type) {
type = /^\s*([^;\s]*)/.test(type) && RegExp.$1;
return type && this._extensions[type.toLowerCase()] || null;
}
}
/**
* Return file extension associated with a mime type
*/
Mime.prototype.getExtension = function(type) {
type = /^\s*([^;\s]*)/.test(type) && RegExp.$1;
return type && this._extensions[type.toLowerCase()] || null;
};

module.exports = Mime;
52 changes: 28 additions & 24 deletions README.md
@@ -1,23 +1,46 @@
<!--
-- This file is auto-generated from src/README_js.md. Changes should be made there.
-->
# Mime

A comprehensive, compact MIME type module.

## Version 2 Notes

Version 2 is a breaking change from 1.x, as the semver implies. Specifically:
Version 2 is a breaking change from 1.x as the semver implies. Specifically:

* **ES6 support required (node@>=6)**
* `lookup()` renamed to `getType()`
* `extension()` renamed to `getExtension()`
* `charset()` and `load()` methods have been removed

If you prefer the legacy version of this module please `npm install mime@^1`. Version 1 docs may be found [here](https://github.com/broofa/node-mime/tree/v1.x).
If you prefer the legacy version of this module please `npm install mime@^1`. Version 1 docs may be found [here](https://github.com/broofa/node-mime/tree/v1.4.0).

## Install - NPM
## Install

### NPM
```
npm install mime
```

### Browser

It is recommended that you use a bundler such as
[webpack](https://webpack.github.io/) or [browserify](http://browserify.org/) to
package your code. However, browser-ready versions are available via wzrd.in.
E.g. For the full version:

<script src="https://wzrd.in/standalone/mime@latest"></script>
<script>
mime.getType(...); // etc.
<script>

Or, for the `mime/lite` version:

<script src="https://wzrd.in/standalone/mime%2flite@latest"></script>
<script>
mimelite.getType(...); // (Note `mimelite` here)
<script>

## Quick Start

For the full version (800+ MIME types, 1,000+ extensions):
Expand All @@ -42,25 +65,6 @@ to 8KB for the full version. To load the lite version:
const mime = require('mime/lite');
```

## Browser-ready Versions

To use this module in the browser, you would typlically use
[webpack](https://webpack.github.io/) or [browserify](http://browserify.org/) to
package your code. However, browser-ready versions are available via wzrd.in.
E.g. For the full version:

<script src="https://wzrd.in/standalone/mime@latest"></script>
<script>
mime.getType(...); // etc.
<script>

Or, for the `mime/lite` version:

<script src="https://wzrd.in/standalone/mime%2flite@latest"></script>
<script>
mimelite.getType(...); // (Note `mimelite` here)
<script>

## Mime .vs. mime-types .vs. mime-db modules

For those of you wondering about the difference between these [popular] NPM modules,
Expand Down Expand Up @@ -182,4 +186,4 @@ E.g.
application/javascript

----
Markdown generated from [src/README_js.md](src/README_js.md) by [![RunMD Logo](http://i.imgur.com/h0FVyzU.png)](https://github.com/broofa/runmd)
Markdown generated from [src/README_js.md](src/README_js.md) by [![RunMD Logo](http://i.imgur.com/h0FVyzU.png)](https://github.com/broofa/runmd)
2 changes: 1 addition & 1 deletion index.js
@@ -1,4 +1,4 @@
'use strict';

const Mime = require('./Mime');
var Mime = require('./Mime');
module.exports = new Mime(require('./types/standard'), require('./types/other'));
2 changes: 1 addition & 1 deletion lite.js
@@ -1,4 +1,4 @@
'use strict';

const Mime = require('./Mime');
var Mime = require('./Mime');
module.exports = new Mime(require('./types/standard'));

0 comments on commit f14ccb6

Please sign in to comment.