Skip to content

Commit

Permalink
Cli tests (#252)
Browse files Browse the repository at this point in the history
* chore: downgrade to mocha@7 for node 8 testing

* chore: CLI test

* chore: eslint

* chore: switch to github actions for CI
  • Loading branch information
broofa committed Jan 16, 2021
1 parent 4a548b7 commit 082342e
Show file tree
Hide file tree
Showing 12 changed files with 484 additions and 253 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"node": true,
"mocha": true
},
"ignorePatterns": ["types/"],
"extends": ["eslint:recommended"],
"rules": {
"array-bracket-spacing": ["warn", "never"],
Expand Down Expand Up @@ -37,6 +38,7 @@
"no-undef": "error",
"no-unused-vars": ["warn", {"args": "none"}],
"one-var": ["warn", "never"],
"no-var": "warn",
"padded-blocks": ["warn", "never"],
"object-curly-spacing": ["warn", "never"],
"quotes": ["warn", "single"],
Expand Down
7 changes: 2 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
name: Node.js CI
name: Mime CI

on:
push:
branches: [ $default-branch ]
pull_request:
branches: [ $default-branch ]
[push]

jobs:
build:
Expand Down
26 changes: 14 additions & 12 deletions Mime.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function Mime() {
this._types = Object.create(null);
this._extensions = Object.create(null);

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

Expand Down Expand Up @@ -37,16 +37,18 @@ function Mime() {
* @param force (Boolean) if true, force overriding of existing definitions
*/
Mime.prototype.define = function(typeMap, force) {
for (var type in typeMap) {
var extensions = typeMap[type].map(function(t) {return t.toLowerCase()});
for (let type in typeMap) {
let extensions = typeMap[type].map(function(t) {
return t.toLowerCase();
});
type = type.toLowerCase();

for (var i = 0; i < extensions.length; i++) {
var ext = extensions[i];
for (let i = 0; i < extensions.length; i++) {
const ext = extensions[i];

// '*' prefix = not the preferred type for this extension. So fixup the
// extension, and skip it.
if (ext[0] == '*') {
if (ext[0] === '*') {
continue;
}

Expand All @@ -64,8 +66,8 @@ Mime.prototype.define = function(typeMap, force) {

// Use first extension as default
if (force || !this._extensions[type]) {
var ext = extensions[0];
this._extensions[type] = (ext[0] != '*') ? ext : ext.substr(1)
const ext = extensions[0];
this._extensions[type] = (ext[0] !== '*') ? ext : ext.substr(1);
}
}
};
Expand All @@ -75,11 +77,11 @@ Mime.prototype.define = function(typeMap, force) {
*/
Mime.prototype.getType = function(path) {
path = String(path);
var last = path.replace(/^.*[/\\]/, '').toLowerCase();
var ext = last.replace(/^.*\./, '').toLowerCase();
let last = path.replace(/^.*[/\\]/, '').toLowerCase();
let ext = last.replace(/^.*\./, '').toLowerCase();

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

return (hasDot || !hasPath) && this._types[ext] || null;
};
Expand Down
16 changes: 7 additions & 9 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
'use strict';

process.title = 'mime';
var mime = require('.');
var pkg = require('./package.json');
var args = process.argv.splice(2);
let mime = require('.');
let pkg = require('./package.json');
let args = process.argv.splice(2);

if (args.includes('--version') || args.includes('-v') || args.includes('--v')) {
console.log(pkg.version);
process.exit(0);
}
else if (args.includes('--name') || args.includes('-n') || args.includes('--n')) {
} else if (args.includes('--name') || args.includes('-n') || args.includes('--n')) {
console.log(pkg.name);
process.exit(0);
}
else if (args.includes('--help') || args.includes('-h') || args.includes('--h')) {
} else if (args.includes('--help') || args.includes('-h') || args.includes('--h')) {
console.log(pkg.name + ' - ' + pkg.description + '\n');
console.log(`Usage:
Expand All @@ -41,8 +39,8 @@ else if (args.includes('--help') || args.includes('-h') || args.includes('--h'))
process.exit(0);
}

var file = args[0];
var type = mime.getType(file);
let file = args[0];
let type = mime.getType(file);

process.stdout.write(type + '\n');

2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';

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

var Mime = require('./Mime');
let Mime = require('./Mime');
module.exports = new Mime(require('./types/standard'));
7 changes: 7 additions & 0 deletions mime.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"folders": [
{
"path": "."
}
]
}
Loading

0 comments on commit 082342e

Please sign in to comment.