Skip to content

Commit

Permalink
Merge branch 'master' of github.com:broofa/node-mime
Browse files Browse the repository at this point in the history
  • Loading branch information
broofa committed Sep 13, 2017
2 parents 5522c79 + a1b2279 commit c0c86b7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 52 deletions.
72 changes: 35 additions & 37 deletions Mime.js
Expand Up @@ -2,65 +2,63 @@
* @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) {
/**
* 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 (let i = 0; i < extensions.length; i++) {
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}".`);
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;
this._types[ext] = type;
}

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

/**
* Lookup a mime type based on extension
*/
getType(path) {
/**
* 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) {
/**
* 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;
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -40,5 +40,5 @@
"url": "https://github.com/broofa/node-mime",
"type": "git"
},
"version": "2.0.0"
"version": "2.0.1"
}
31 changes: 17 additions & 14 deletions src/test.js
Expand Up @@ -6,9 +6,9 @@ var chalk = require('chalk');

describe('class Mime', function () {
it('new constructor()', function () {
const Mime = require('../Mime');
var Mime = require('../Mime');

const mime = new Mime(
var mime = new Mime(
{'text/a': ['a', 'a1']},
{'text/b': ['b', 'b1']}
);
Expand All @@ -27,9 +27,9 @@ describe('class Mime', function () {
});

it('define()', function () {
const Mime = require('../Mime');
var Mime = require('../Mime');

const mime = new Mime({'text/a': ['a']}, {'text/b': ['b']});
var mime = new Mime({'text/a': ['a']}, {'text/b': ['b']});

assert.throws(function() {
mime.define({'text/c': ['b']});
Expand Down Expand Up @@ -109,21 +109,24 @@ describe('DB', function() {
if (diffs.length) {
console.log('\n[INFO] The following inconsistencies with MDN and/or mime-types are expected:');
diffs.forEach(function(d) {
console.warn(` ${d[0]}[${chalk.blue(d[1])}] = ${chalk.red(d[2])}, node-uuid[${d[1]}] = ${chalk.green(d[3])}`);
console.warn(
' ' + d[0] +
'[' + chalk.blue(d[1]) + '] = ' +
chalk.red(d[2]) + ', node-uuid[' + d[1] + '] = ' + chalk.green(d[3]));
});
}
});

it('Consistency', function() {
for (var ext in this.types) {
const ext2 = this.extensions[this.types[ext]];
var ext2 = this.extensions[this.types[ext]];
assert.equal(ext, this.extensions[this.types[ext]], '${ext} does not have consistent ext->type->ext mapping');
}
});

it('MDN types', function () {
// MDN types listed at https://goo.gl/lHrFU6
const MDN = {
var MDN = {
'aac': 'audio/aac',
'abw': 'application/x-abiword',
'arc': 'application/octet-stream',
Expand Down Expand Up @@ -182,16 +185,16 @@ describe('DB', function() {
'7z': 'application/x-7z-compressed',
};

for (let ext in MDN) {
const expected = MDN[ext];
const actual = mime.getType(ext);
for (var ext in MDN) {
var expected = MDN[ext];
var actual = mime.getType(ext);
if (actual != expected) diffs.push(['MDN', ext, expected, actual]);
}
const mimeTypes = require('../node_modules/mime-types');
var mimeTypes = require('../node_modules/mime-types');

for (let ext in mimeTypes.types) {
const expected = mimeTypes.types[ext];
const actual = mime.getType(ext);
for (var ext in mimeTypes.types) {
var expected = mimeTypes.types[ext];
var actual = mime.getType(ext);
if (actual != expected) diffs.push(['mime-types', ext, expected, actual]);
}
});
Expand Down

0 comments on commit c0c86b7

Please sign in to comment.