Skip to content

Commit

Permalink
eslint, rm types.json
Browse files Browse the repository at this point in the history
  • Loading branch information
broofa committed Sep 13, 2017
1 parent 404b573 commit ef0b204
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 46 deletions.
50 changes: 50 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"root": true,
"parserOptions": {
"ecmaVersion": 6
},
"env": {
"browser": true,
"commonjs": true,
"node": true,
"mocha": true
},
"extends": ["eslint:recommended"],
"rules": {
"array-bracket-spacing": ["warn", "never"],
"arrow-body-style": ["warn", "as-needed"],
"arrow-parens": ["warn", "as-needed"],
"arrow-spacing": "warn",
"brace-style": ["warn", "1tbs"],
"camelcase": "warn",
"comma-spacing": ["warn", {"after": true}],
"dot-notation": "warn",
"eqeqeq": ["warn", "smart"],
"indent": ["warn", 2, {
"SwitchCase": 1,
"FunctionDeclaration": {"parameters": 1},
"MemberExpression": 1,
"CallExpression": {"arguments": 1}
}],
"key-spacing": ["warn", {"beforeColon": false, "afterColon": true, "mode": "minimum"}],
"keyword-spacing": "warn",
"no-console": "off",
"no-empty": "off",
"no-multi-spaces": "warn",
"no-redeclare": "off",
"no-restricted-globals": ["warn", "Promise"],
"no-trailing-spaces": "warn",
"no-undef": "error",
"no-unused-vars": ["warn", {"args": "none"}],
"one-var": ["warn", "never"],
"padded-blocks": ["warn", "never"],
"object-curly-spacing": ["warn", "never"],
"quotes": ["warn", "single"],
"react/prop-types": "off",
"react/jsx-no-bind": "off",
"semi": ["warn", "always"],
"space-before-blocks": ["warn", "always"],
"space-before-function-paren": ["warn", "never"],
"space-in-parens": ["warn", "never"]
}
}
Empty file removed .npmignore
Empty file.
4 changes: 3 additions & 1 deletion Mime.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

/**
* @param typeMap [Object] Map of MIME type -> Array[extensions]
* @param ...
Expand Down Expand Up @@ -45,7 +47,7 @@ class Mime {
*/
getType(path) {
path = String(path);
var last = path.replace(/.*[\/\\]/, '').toLowerCase();
var last = path.replace(/.*[/\\]/, '').toLowerCase();
var ext = last.replace(/.*\./, '').toLowerCase();

var hasPath = last.length < path.length;
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ A comprehensive, compact MIME type module.

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

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

If you need the legacy version of this module for any reason, please `npm install mime@^1`. Version 1 docs may be found [here](https://github.com/broofa/node-mime/tree/v1.4.0).
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
```
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"dependencies": {},
"devDependencies": {
"chalk": "1.1.3",
"eslint": "4.6.1",
"github-release-notes": "0.9.0",
"mime-db": "1.30.0",
"mime-types": "2.1.15",
Expand Down
4 changes: 2 additions & 2 deletions src/README_js.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ A comprehensive, compact MIME type module.

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

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

If you need the legacy version of this module for any reason, please `npm install mime@^1`. Version 1 docs may be found [here](https://github.com/broofa/node-mime/tree/v1.4.0).
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
```
Expand Down
26 changes: 14 additions & 12 deletions src/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ function getScore(entry) {
}

// Use mime-db's logic for ranking by source
switch(entry.source) {
switch (entry.source) {
// Prioritize by source (same as mime-types module)
case 'iana': pri += 40; break;
case 'apache': pri += 20; break;
case 'nginx': pri += 10; break;
default: pri += 30; break;
case 'iana': pri += 40; break;
case 'apache': pri += 20; break;
case 'nginx': pri += 10; break;
default: pri += 30; break;
}

// Prefer application over other types (e.g. text/xml and application/xml, and
// text/rtf and application/rtf all appear to be respectable mime thingz. Lovely,
// right?)
switch(type) {
case 'application': pri += 1; break;
default: break;
switch (type) {
case 'application': pri += 1; break;
default: break;
}

// All other things being equal, use length
Expand All @@ -55,20 +55,22 @@ const byExtension = {};

// Clear out any conflict extensions in mime-db

for (let type in db) {
for (let type in db) {
let entry = db[type];
entry.type = type;

if (!entry.extensions) continue;

entry.extensions.forEach(ext => {
if (ext in byExtension) {
const e0 = entry, e1 = byExtension[ext];
const e0 = entry;
const e1 = byExtension[ext];
e0.pri = getScore(e0);
e1.pri = getScore(e1);

let drop = e0.pri < e1.pri ? e0 : e1, keep = e0.pri >= e1.pri ? e0 : e1;
drop.extensions = drop.extensions.filter(e => e != ext);
let drop = e0.pri < e1.pri ? e0 : e1;
let keep = e0.pri >= e1.pri ? e0 : e1;
drop.extensions = drop.extensions.filter(e => e !== ext);

console.log(`${ext}: Keeping ${chalk.green(keep.type)} (${keep.pri}), dropping ${chalk.red(drop.type)} (${drop.pri})`);
}
Expand Down
53 changes: 25 additions & 28 deletions src/test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
var mime = require('..');
var mimeTypes = require('../node_modules/mime-types');
var assert = require('assert');
var path = require('path');
var chalk = require('chalk');

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

const mime = new Mime(
Expand All @@ -26,7 +25,7 @@ describe('class Mime', function () {
});
});

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

const mime = new Mime({'text/a': ['a']}, {'text/b': ['b']});
Expand All @@ -51,7 +50,7 @@ describe('class Mime', function () {
});
});

it('getType()', function () {
it('getType()', function() {
// Upper/lower case
assert.equal(mime.getType('text.txt'), 'text/plain');
assert.equal(mime.getType('TEXT.TXT'), 'text/plain');
Expand Down Expand Up @@ -79,9 +78,9 @@ describe('class Mime', function () {
assert.equal(mime.getType('page.html'), 'text/html');
assert.equal(mime.getType('path/to/page.html'), 'text/html');
assert.equal(mime.getType('path\\to\\page.html'), 'text/html');
assert.strictEqual(mime.getType('/txt'), null);
assert.strictEqual(mime.getType('\\txt'), null);
assert.strictEqual(mime.getType('text.nope'), null);
assert.strictEqual(mime.getType('/txt'), null);
assert.strictEqual(mime.getType('\\txt'), null);
assert.strictEqual(mime.getType('text.nope'), null);
assert.strictEqual(mime.getType('/path/to/file.bogus'), null);
assert.strictEqual(mime.getType('/path/to/json'), null);
assert.strictEqual(mime.getType('/path/to/.json'), null);
Expand All @@ -99,29 +98,28 @@ describe('class Mime', function () {
assert.strictEqual(mime.getExtension(undefined), null);
assert.strictEqual(mime.getExtension(42), null);
assert.strictEqual(mime.getExtension({}), null);
})
});
});

describe('DB', function() {
var diffs = [];

after(function() {
if (diffs.length) {
console.log('\n[INFO] The following inconsistencies with MDN and/or mime-types are expected:');
console.log('\n[INFO] The following inconsistencies with MDN (https://goo.gl/lHrFU6) and/or mime-types (https://github.com/jshttp/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])}, mime[${d[1]}] = ${chalk.green(d[3])}`);
});
}
});

it('Consistency', function() {
for (var ext in this.types) {
const 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 () {
it('MDN types', function() {
// MDN types listed at https://goo.gl/lHrFU6
const MDN = {
'aac': 'audio/aac',
Expand Down Expand Up @@ -185,18 +183,17 @@ describe('DB', function() {
for (let ext in MDN) {
const expected = MDN[ext];
const actual = mime.getType(ext);
if (actual != expected) diffs.push(['MDN', ext, expected, actual]);
if (actual !== expected) diffs.push(['MDN', ext, expected, actual]);
}
const mimeTypes = require('../node_modules/mime-types');

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

it('types', function () {
it('types', function() {
// Assortment of common types
assert.equal(mime.getType('html'), 'text/html');
assert.equal(mime.getType('js'), 'application/javascript');
Expand All @@ -206,22 +203,22 @@ describe('DB', function() {
assert.equal(mime.getType('xml'), 'application/xml');
});

it('extensions', function () {
it('extensions', function() {
assert.equal(mime.getExtension('text/html;charset=UTF-8'), 'html');
assert.equal(mime.getExtension('text/HTML; charset=UTF-8'), 'html');
assert.equal(mime.getExtension('text/html; charset=UTF-8'), 'html');
assert.equal(mime.getExtension('text/html; charset=UTF-8 '), 'html');
assert.equal(mime.getExtension('text/html ; charset=UTF-8'), 'html');
assert.equal( mime.getExtension(mime._types.text), 'txt');
assert.equal( mime.getExtension(mime._types.htm), 'html');
assert.equal( mime.getExtension('application/octet-stream'), 'bin');
assert.equal( mime.getExtension('application/octet-stream '), 'bin');
assert.equal( mime.getExtension(' text/html; charset=UTF-8'), 'html');
assert.equal( mime.getExtension('text/html; charset=UTF-8 '), 'html');
assert.equal( mime.getExtension('text/html; charset=UTF-8'), 'html');
assert.equal( mime.getExtension('text/html ; charset=UTF-8'), 'html');
assert.equal( mime.getExtension('text/html;charset=UTF-8'), 'html');
assert.equal( mime.getExtension('text/Html;charset=UTF-8'), 'html');
assert.equal(mime.getExtension(mime._types.text), 'txt');
assert.equal(mime.getExtension(mime._types.htm), 'html');
assert.equal(mime.getExtension('application/octet-stream'), 'bin');
assert.equal(mime.getExtension('application/octet-stream '), 'bin');
assert.equal(mime.getExtension(' text/html; charset=UTF-8'), 'html');
assert.equal(mime.getExtension('text/html; charset=UTF-8 '), 'html');
assert.equal(mime.getExtension('text/html; charset=UTF-8'), 'html');
assert.equal(mime.getExtension('text/html ; charset=UTF-8'), 'html');
assert.equal(mime.getExtension('text/html;charset=UTF-8'), 'html');
assert.equal(mime.getExtension('text/Html;charset=UTF-8'), 'html');
assert.equal(mime.getExtension('unrecognized'), null);
});
});
1 change: 0 additions & 1 deletion types.json

This file was deleted.

0 comments on commit ef0b204

Please sign in to comment.