Skip to content

Commit

Permalink
code cleanup to pass jslint
Browse files Browse the repository at this point in the history
  • Loading branch information
rsdoiel committed Dec 25, 2014
1 parent 9443163 commit b0eeb8c
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 87 deletions.
43 changes: 23 additions & 20 deletions mimetype.js
Expand Up @@ -7,9 +7,11 @@
// Released under New the BSD License.
// See: http://opensource.org/licenses/bsd-license.php
//

/*jslint indent: 4 */
/*global require, exports */
(function (self) {
var path;
"use strict";
var path, MimeType;

// If we're NodeJS I can use the path module.
// If I'm MongoDB shell, not available.
Expand Down Expand Up @@ -54,21 +56,21 @@

// Handle the special cases where their is no extension
// e..g README, manifest, LICENSE, TODO
if (ext == "") {
if (ext === "") {
ext = fname;
}

if (this.catalog[ext] !== undefined) {
if (include_charset === true &&
this.catalog[ext].indexOf('text/') === 0 &&
this.catalog[ext].indexOf('charset') < 0) {
if (include_charset === true &&
this.catalog[ext].indexOf('text/') === 0 &&
this.catalog[ext].indexOf('charset') < 0) {
return this.catalog[ext] + '; charset=' + charset;
} else {
return this.catalog[ext];
}
} else if (default_mime_type !== undefined) {
if (include_charset === true &&
default_mime_type.indexOf('text/') === 0) {
if (include_charset === true &&
default_mime_type.indexOf('text/') === 0) {
return default_mime_type + '; charset=' + charset;
}
return default_mime_type;
Expand All @@ -77,7 +79,8 @@
},
set: function (exts, mime_type_string) {
var result = true, self = this;
if (exts.indexOf(',')) {
//console.log("DEBUG exts.indexOf(',')", typeof exts.indexOf(','), exts.indexOf(','));
if (exts.indexOf(',') > -1) {
exts.split(',').forEach(function (ext) {
ext = ext.trim();
self.catalog[ext] = mime_type_string;
Expand All @@ -86,7 +89,7 @@
}
});
} else {
result = (self.catalog[exts] === mime_type_string);
self.catalog[exts] = mime_type_string;
}
return result;
},
Expand Down Expand Up @@ -707,7 +710,7 @@
MimeType.set(".jpm,.jpgm", "video/jpm");
MimeType.set(".mj2,.mjp2", "video/mj2");
MimeType.set(".mp4,.mp4v,.mpg4,.m4v", "video/mp4");
MimeType.set(".webm", "video/webm")
MimeType.set(".webm", "video/webm");
MimeType.set(".mpeg,.mpg,.mpe,.m1v,.m2v", "video/mpeg");
MimeType.set(".ogv", "video/ogg");
MimeType.set(".qt,.mov", "video/quicktime");
Expand All @@ -734,7 +737,7 @@
MimeType.set(".mobi", "application/x-mobipocket-ebook");

// Here's some common special cases without filename extensions
MimeType.set("README,LICENSE,COPYING,TODO,ABOUT,AUTHORS,CONTRIBUTORS",
MimeType.set("README,LICENSE,COPYING,TODO,ABOUT,AUTHORS,CONTRIBUTORS",
"text/plain");
MimeType.set("manifest,.manifest,.mf,.appcache", "text/cache-manifest");
if (exports !== undefined) {
Expand All @@ -745,13 +748,13 @@
exports.del = MimeType.del;
exports.forEach = MimeType.forEach;
}
// Note: Chrome now defines window.MimeType, only define for legacy usage.
if (self.MimeType === undefined) {
self.MimeType = MimeType;
}
// Note: Per Hypercuded switch to camel case to avoid Chrome issues.
if (self.mimeType === undefined) {
self.mimeType = MimeType;
}
// Note: Chrome now defines window.MimeType, only define for legacy usage.
if (self.MimeType === undefined) {
self.MimeType = MimeType;
}
// Note: Per Hypercuded switch to camel case to avoid Chrome issues.
if (self.mimeType === undefined) {
self.mimeType = MimeType;
}
return self;
}(this));
153 changes: 86 additions & 67 deletions tests/mimetype_test.js
Expand Up @@ -7,77 +7,96 @@
// Released under the Simplified BSD License.
// See: http://opensource.org/licenses/bsd-license.php
//
/*hslint node: true, indent 4 */
/*global require */
(function () {
"use strict";
var Y = require("yui/test"),
assert = Y.Assert,
path = require('path'),
v0_0_3 = require('../lib/v0.0.3').data,
mimetype = require('../mimetype'),
basicTests;

// Tests for version 0.0.2
basicTests = new Y.Test.Case({
name: "Basic Tests",
"Should pass tests for version 0.0.2": function () {
var ky_cnt, i;

assert.areEqual(mimetype.lookup("myfile.txt"), 'text/plain', "lookup should return text/plain");
assert.areEqual(mimetype.set('.exotic', 'x-application/experimental'), true, "set should return true for .exotic.");
assert.areEqual(mimetype.lookup("myfile.exotic"), "x-application/experimental", "lookup should return x-application/experimental");
assert.areEqual(mimetype.del('.exotic'), true, "del() should return true");
assert.areEqual(mimetype.lookup("myfile.exotic"), false, "lookup(myfile.exotic) should return false now");
ky_cnt = Object.keys(mimetype.catalog).length;
i = 0;
mimetype.forEach(function (ext, mime_type_string) {
Y.assert(ext, "Should have an ext");
Y.assert(mime_type_string, "Should have a mime_type string");
assert.areSame(mimetype.catalog[ext], mime_type_string);
i += 1;
});
assert.areEqual(ky_cnt, i, "i should equal ky_cnt");

var Y = require("yui/test"),
assert = Y.Assert,
path = require('path'),
v0_0_3 = require('../lib/v0.0.3').data,
mimetype = require('../mimetype');
// Test multi-extension set()
assert.areEqual(mimetype.lookup("test.txt1"), false, "Should not have the .txt1 defined yet.");
assert.areEqual(mimetype.lookup("test.txt2"), false, "Should not have the .txt2 defined yet.");
assert.areEqual(mimetype.lookup("test.txt3"), false, "Should not have the .txt3 defined yet.");
mimetype.set(".txt1,.txt2,.txt3", "text/plain");
assert.areEqual(mimetype.lookup("test.txt1"), "text/plain", "Should have the .txt1 now.");
assert.areEqual(mimetype.lookup("test.txt2"), "text/plain", "Should have the .txt2 now.");
assert.areEqual(mimetype.lookup("test.txt3"), "text/plain", "Should have the .txt3 now.");
assert.areEqual(mimetype.lookup("this.isNotDefined"), false, "Should not have a mime type defined for this.isNotDefined");
assert.areEqual(mimetype.lookup("this.isNotDefined", false, "text/plain"), "text/plain", "Should not have a mime type defined for this.isNotDefined");
assert.areEqual(mimetype.lookup("this.isNotDefined", true, "text/plain"), "text/plain; charset=UTF-8", "Should have a mime type with charset defined for this.isNotDefined: " + mimetype.lookup("this.isNotDefined", true, "text/plain"));
assert.areEqual(mimetype.lookup("this.isNotDefined", "UTF-8", "text/plain"), "text/plain; charset=UTF-8", "this.isNotDefined should be text/plain;charset=UTF-8: " + mimetype.lookup("this.isNotDefined", "UTF-8", "text/plain"));

// Tests for version 0.0.2
var basicTests = new Y.Test.Case({
name: "Basic Tests",
"Should pass tests for version 0.0.2": function () {
assert.areEqual(mimetype.lookup("myfile.txt"), 'text/plain', "lookup should return text/plain");
assert.areEqual(mimetype.set('.exotic', 'x-application/experimental'), true, "set should return true.");
assert.areEqual(mimetype.lookup("myfile.exotic"), "x-application/experimental", "lookup should return x-application/experimental");
assert.areEqual(mimetype.del('.exotic'), true, "del() should return true");
assert.areEqual(mimetype.lookup("myfile.exotic"), false, "lookup(myfile.exotic) should return false now");
ky_cnt = Object.keys(mimetype.catalog).length;
i = 0;
mimetype.forEach(function (ext, mime_type_string) {
Y.assert(ext, "Should have an ext");
Y.assert(mime_type_string, "Should have a mime_type string");
assert.areSame(mimetype.catalog[ext], mime_type_string);
i += 1;
});
assert.areEqual(ky_cnt, i, "i should equal ky_cnt");

// Test multi-extension set()
assert.areEqual(mimetype.lookup("test.txt1"), false, "Should not have the .txt1 defined yet.");
assert.areEqual(mimetype.lookup("test.txt2"), false, "Should not have the .txt2 defined yet.");
assert.areEqual(mimetype.lookup("test.txt3"), false, "Should not have the .txt3 defined yet.");
mimetype.set(".txt1,.txt2,.txt3", "text/plain");
assert.areEqual(mimetype.lookup("test.txt1"), "text/plain", "Should have the .txt1 now.");
assert.areEqual(mimetype.lookup("test.txt2"), "text/plain", "Should have the .txt2 now.");
assert.areEqual(mimetype.lookup("test.txt3"), "text/plain", "Should have the .txt3 now.");
assert.areEqual(mimetype.lookup("this.isNotDefined"), false, "Should not have a mime type defined for this.isNotDefined");
assert.areEqual(mimetype.lookup("this.isNotDefined", false, "text/plain"), "text/plain", "Should not have a mime type defined for this.isNotDefined");
assert.areEqual(mimetype.lookup("this.isNotDefined", true, "text/plain"), "text/plain; charset=UTF-8", "Should have a mime type with charset defined for this.isNotDefined: " + mimetype.lookup("this.isNotDefined", true, "text/plain"));
assert.areEqual(mimetype.lookup("this.isNotDefined", "UTF-8", "text/plain"), "text/plain; charset=UTF-8", "this.isNotDefined should be text/plain;charset=UTF-8: " + mimetype.lookup("this.isNotDefined", "UTF-8", "text/plain"));

assert.areEqual(mimetype.lookup("README"), "text/plain", "README should return text/plain mime-type.");
assert.areEqual(mimetype.lookup("manifest"), "text/cache-manifest", "manifest should return text/plain mime-type.");
},
assert.areEqual(mimetype.lookup("README"), "text/plain", "README should return text/plain mime-type.");
assert.areEqual(mimetype.lookup("manifest"), "text/cache-manifest", "manifest should return text/plain mime-type.");
},

// tests for version 0.0.3
"Should pass tests for version 0.0.3": function () {
Object.keys(v0_0_3).forEach(function (i) {
var vals, j, testname;

Y.assert(v0_0_3[i].mime_type, "Missing v0_0_3 index:" + i);
if (v0_0_3[i].ext !== undefined) {
if (v0_0_3[i].ext.indexOf(" ") > 0) {
vals = v0_0_3[i].ext.split(" ");
for (j = 0; j < vals.length; j += 1) {
testname = ["testname", vals[j]].join(".");
// tests for version 0.0.3
"Should pass tests for version 0.0.3": function () {
Object.keys(v0_0_3).forEach(function (i) {
var vals, j, testname;

Y.assert(v0_0_3[i].mime_type, "Missing v0_0_3 index:" + i);
if (v0_0_3[i].ext !== undefined) {
if (v0_0_3[i].ext.indexOf(" ") > 0) {
vals = v0_0_3[i].ext.split(" ");
for (j = 0; j < vals.length; j += 1) {
testname = ["testname", vals[j]].join(".");
assert.areEqual(
mimetype.lookup(testname),
v0_0_3[i].mime_type,
[
testname,
mimetype.lookup(testname),
'->',
v0_0_3[i].mime_type,
vals[j],
"failed"
].join(" ")
);
}
} else {
testname = ["testname", v0_0_3[i].ext].join(".");
assert.areEqual(
mimetype.lookup(testname),
v0_0_3[i].mime_type,
[testname, mimetype.lookup(testname), '->', v0_0_3[i].mime_type, vals[j], "failed"].join(" "));
mimetype.lookup(testname),
v0_0_3[i].mime_type,
[
testname,
v0_0_3[i].mime_type,
"failed"
].join(" ")
);
}
} else {
testname = ["testname", v0_0_3[i].ext].join(".");
assert.areEqual(
mimetype.lookup(testname),
v0_0_3[i].mime_type,
[testname, v0_0_3[i].mime_type, "failed"].join(" "));
}
}
});
}
});

Y.Test.Runner.add(basicTests);
Y.Test.Runner.run();
});
}
});

Y.Test.Runner.add(basicTests);
Y.Test.Runner.run();
}());

0 comments on commit b0eeb8c

Please sign in to comment.