Comparing version 2.9.0 to 3.0.0
@@ -9,3 +9,3 @@ // Generated by CoffeeScript 1.9.3 | ||
(function() { | ||
var MimeTypes, defineProperty, extname, extractTypeRegExp, isArray, isString, mediaTyper, minimatch, textTypeRegExp, | ||
var MimeTypes, defineProperty, extname, extractTypeRegExp, isArray, isFunction, isString, mediaTyper, minimatch, textTypeRegExp, | ||
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; | ||
@@ -21,2 +21,4 @@ | ||
isFunction = require('util-ex/lib/is/type/function'); | ||
defineProperty = require('util-ex/lib/defineProperty'); | ||
@@ -42,6 +44,5 @@ | ||
if (!(this instanceof MimeTypes)) { | ||
return new MimeTypes(db); | ||
return new MimeTypes(db, duplicationProcessWay); | ||
} | ||
defineProperty(this, 'types', {}); | ||
defineProperty(this, 'mimes', {}); | ||
defineProperty(this, 'dupDefault', 0); | ||
@@ -54,7 +55,8 @@ defineProperty(this, 'dupSkip', 1); | ||
get: function() { | ||
var k, mime, ref, result; | ||
var j, k, len, mime, ref, result; | ||
result = {}; | ||
ref = this.mimes; | ||
for (k in ref) { | ||
mime = ref[k]; | ||
ref = Object.keys(this); | ||
for (j = 0, len = ref.length; j < len; j++) { | ||
k = ref[j]; | ||
mime = this[k]; | ||
result[k] = mime.extensions; | ||
@@ -69,3 +71,3 @@ } | ||
if (db) { | ||
this._load(db); | ||
this.load(db); | ||
} | ||
@@ -91,3 +93,3 @@ } | ||
type = mediaTyper.format(obj); | ||
result = this.mimes[type] && this.mimes[type].charset; | ||
result = this[type] && this[type].charset; | ||
} | ||
@@ -139,3 +141,3 @@ if (!result && obj.type === 'text') { | ||
result = extractTypeRegExp.exec(type); | ||
result = result && this.mimes[result[1].toLowerCase()]; | ||
result = result && this[result[1].toLowerCase()]; | ||
if (result) { | ||
@@ -180,3 +182,3 @@ result = result.defaultExtension || result.extensions[0]; | ||
} | ||
result = Object.keys(this.mimes).filter(function(name) { | ||
result = Object.keys(this).filter(function(name) { | ||
return minimatch(name, pattern); | ||
@@ -194,5 +196,3 @@ }); | ||
MimeTypes.prototype.exist = function(type) { | ||
return this.mimes.hasOwnProperty(type); | ||
}; | ||
MimeTypes.prototype.exist = Object.prototype.hasOwnProperty; | ||
@@ -210,7 +210,8 @@ refSources = ['nginx', 'apache', void 0, 'iana']; | ||
* * "extensions": ["js"] | ||
* @return {array}: the added extensions | ||
*/ | ||
MimeTypes.prototype.define = function(type, mime, dup) { | ||
var extension, exts, from, i, len, ref, t, to; | ||
if (!(type && mime && mime.extensions && !this.mimes.hasOwnProperty(type))) { | ||
var extension, exts, from, j, len, ref, t, to; | ||
if (!(type && mime && mime.extensions && !this.hasOwnProperty(type))) { | ||
return; | ||
@@ -230,4 +231,4 @@ } | ||
ref = mime.extensions; | ||
for (i = 0, len = ref.length; i < len; i++) { | ||
extension = ref[i]; | ||
for (j = 0, len = ref.length; j < len; j++) { | ||
extension = ref[j]; | ||
t = this.types[extension]; | ||
@@ -253,3 +254,3 @@ if (t) { | ||
} | ||
from = refSources.indexOf(this.mimes[t].source); | ||
from = refSources.indexOf(this[t].source); | ||
to = refSources.indexOf(mime.source); | ||
@@ -273,3 +274,3 @@ if (t !== 'application/octet-stream' && from > to || from === to && t.substr(0, 12) === 'application/') { | ||
mime.extensions = exts; | ||
this.mimes[type] = mime; | ||
this[type] = mime; | ||
} | ||
@@ -281,14 +282,76 @@ return exts; | ||
/* | ||
* Populate the extensions and types maps from db. | ||
* @private | ||
* load mime-types from db. | ||
*/ | ||
MimeTypes.prototype._load = function(db) { | ||
return Object.keys(db).forEach((function(_this) { | ||
MimeTypes.prototype.load = function(mimes, duplicationProcessWay) { | ||
var result; | ||
result = 0; | ||
Object.keys(mimes).forEach((function(_this) { | ||
return function(type) { | ||
return _this.define(type, db[type]); | ||
var t; | ||
t = _this.define(type, mimes[type], duplicationProcessWay); | ||
if (t && t.length) { | ||
return result++; | ||
} | ||
}; | ||
})(this)); | ||
return result; | ||
}; | ||
/* | ||
* remove the specified mime-type. | ||
*/ | ||
MimeTypes.prototype["delete"] = function(type) { | ||
var i, k, ref, result, v; | ||
result = this.exist(type); | ||
if (result) { | ||
ref = this.types; | ||
for (k in ref) { | ||
v = ref[k]; | ||
if (isArray(v)) { | ||
i = v.indexOf(type); | ||
if (i !== -1) { | ||
v.splice(i, 1); | ||
} | ||
} else if (type === v) { | ||
delete this.types[k]; | ||
} | ||
} | ||
delete this[type]; | ||
} | ||
return result; | ||
}; | ||
/* | ||
* clear the mime-types. | ||
*/ | ||
MimeTypes.prototype.clear = function(filter) { | ||
var k, result, v; | ||
result = 0; | ||
for (k in this) { | ||
v = this[k]; | ||
if (this.hasOwnProperty(k)) { | ||
if (isFunction(filter)) { | ||
if (filter(k, v)) { | ||
this["delete"](k); | ||
result++; | ||
} | ||
} else if (isString(filter)) { | ||
if (minimatch(k, filter)) { | ||
this["delete"](k); | ||
result++; | ||
} | ||
} else { | ||
this["delete"](k); | ||
result++; | ||
} | ||
} | ||
} | ||
return result; | ||
}; | ||
return MimeTypes; | ||
@@ -295,0 +358,0 @@ |
{ | ||
"name": "mime-type", | ||
"description": "the custom content-type utility can work with meme-db.", | ||
"version": "2.9.0", | ||
"description": "the custom content-type utility can work with mime-db.", | ||
"version": "3.0.0", | ||
"contributors": [ | ||
@@ -14,2 +14,3 @@ "Riceball LEE https://github.com/snowyu", | ||
"mime", | ||
"mime-db", | ||
"types" | ||
@@ -30,3 +31,3 @@ ], | ||
}, | ||
"pre-commit": "npm test", | ||
"pre-commit": ["test"], | ||
"files": [ | ||
@@ -33,0 +34,0 @@ "HISTORY.md", |
@@ -17,4 +17,7 @@ # mime-type | ||
- `mime = new Mime()` business, so you could do `lookup = mime.lookup.bind(mime)`. | ||
- you can add the mime-type via `.define()` functionality | ||
- you can search the mime-type via `.glob()` functionality | ||
- you can add the mime-type via `.define(type, mime)` functionality | ||
- you can add many mime-type via `.load(mimes)` functionality | ||
- you can search the mime-type via `.glob(pattern)` functionality | ||
- you can remove a mime-type via `.delete(type)` functionality | ||
- you can clear mime-types via `.clear(filter)` functionality | ||
- `.exist(type)` functionality to check whether a mime-type is exist. | ||
@@ -61,12 +64,81 @@ - `.extensions` will be deprecated, use `mime[type].extensions` instead. | ||
```js | ||
mime.glob('*/*') // 'application/json' | ||
mime.lookup('.md') // 'text/x-markdown' | ||
mime.lookup('file.html') // 'text/html' | ||
mime.lookup('folder/file.js') // 'application/javascript' | ||
mime.lookup('folder/.htaccess') // false | ||
mime.glob('*/*') // ['application/octet-stream'] | ||
mime.glob('*/*markdown') // ['text/x-markdown'] | ||
mime.glob('text/j*') // ['text/jade', 'text/jsx'] | ||
mime.glob('unknown/x') // [] | ||
``` | ||
mime.lookup('cats') // false | ||
### mime.exist(type) | ||
test whether a mime-type is exist. | ||
It is an alias for `mime.hasOwnProperty` | ||
```js | ||
mime.exist('text/x-markdown') // true | ||
mime.exist('unknown/xxx') // false | ||
``` | ||
### mime.define(type, object, duplicationWay) | ||
define a new mime-type. the duplicationWay is the process way of duplication extensions: | ||
* mime.dupDefault: the default process way. | ||
* mime.dupOverwrite: the news overwrite the olds | ||
* mime.dupSkip: just skip it. | ||
* mime.dupAppend: append the news to the exist extensions. | ||
return the added extensions list if successful or `undefined`. | ||
```js | ||
mime.define('script/coffee', { | ||
extensions: ['coffee', 'litcoffee', 'coffee.md'] | ||
}, mime.dupAppend) | ||
mime.lookup ('coffee') //[ 'text/coffeescript', 'script/coffee' ] | ||
``` | ||
### mime.delete(type) | ||
remove a specified mime-type | ||
```js | ||
mime.delete('script/coffee') //true | ||
``` | ||
### mime.clear(filter) | ||
clear all or specified mime-types | ||
the filter could be a string pattern or a function | ||
return the count of deleted mime-types. | ||
```js | ||
mime.clear() //clear all mime-types | ||
mime.clear('text/*') //clear the specified mime-types | ||
mime.clear(function(type, mime){ | ||
return type.substr(0,5) === 'text/' | ||
}) | ||
``` | ||
### mime.load(mimes) | ||
load a lot of mime-types. return the count of loaded mime-types. | ||
```js | ||
mime.clear() //clear all mime-types | ||
mime.load({ | ||
'script/coffee': { | ||
extensions: ['coffee', 'coffee.md', 'litcoffee'], | ||
compressible: true, | ||
charset: 'utf-8', | ||
defaultExtension: 'coffee.md' | ||
}, | ||
'script/python': { | ||
extensions: ['py', 'py.md', 'litpy'], | ||
compressible: true, | ||
charset: 'utf-8' | ||
} | ||
}) | ||
``` | ||
### mime.contentType(type) | ||
@@ -73,0 +145,0 @@ |
Sorry, the diff of this file is not supported yet
25821
307
192