refactor: move .is to type-is

master
Jonathan Ong 2013-12-27 18:53:19 -08:00
parent 7fe4133b4a
commit 20615b808d
3 changed files with 12 additions and 63 deletions

View File

@ -5,8 +5,8 @@
var debug = require('debug')('koa:context');
var qs = require('querystring');
var typeis = require('type-is');
var fresh = require('fresh');
var mime = require('mime');
var url = require('url');
var parse = url.parse;
var stringify = url.format;
@ -486,38 +486,9 @@ module.exports = {
*/
is: function(types){
// no request body
if (!(this.length || 'transfer-encoding' in this.header)) return;
var ct = this.type;
// no content-type
if (!ct) return false;
// paramless
ct = ct.split(';')[0];
if (!types) return ct;
// ensure array
if (!types) return typeis(this.req);
if (!Array.isArray(types)) types = [].slice.call(arguments);
var mt;
for (var i = 0; i < types.length; i++) {
var type = types[i];
switch (type) {
case 'urlencoded':
mt = 'application/x-www-form-urlencoded';
break;
default:
mt = ~type.indexOf('/') ? type : mime.lookup(type);
}
if (mimeMatch(mt, ct)) return ct;
}
// no matches
return false;
return typeis(this.req, types);
},
/**
@ -592,27 +563,4 @@ module.exports = {
header: this.header
}
}
};
/**
* Check if `exected` mime type
* matches `actual` mime type with
* wildcard support.
*
* @param {String} expected
* @param {String} actual
* @return {Boolean}
* @api private
*/
function mimeMatch(expected, actual) {
if (expected == actual) return true;
if (!~expected.indexOf('*')) return false;
actual = actual.split('/');
expected = expected.split('/');
if ('*' == expected[0] && expected[1] == actual[1]) return true;
if ('*' == expected[1] && expected[0] == actual[0]) return true;
}
};

View File

@ -22,6 +22,7 @@
"license": "MIT",
"dependencies": {
"accepts": "~1.0.0",
"type-is": "~1.0.0",
"on-socket-error": "~1.0.1",
"co": "~3.0.1",
"debug": "*",

View File

@ -49,8 +49,8 @@ describe('ctx.is(type)', function(){
ctx.header['content-type'] = 'image/png';
ctx.header['transfer-encoding'] = 'chunked';
ctx.is('png').should.equal('image/png');
ctx.is('.png').should.equal('image/png');
ctx.is('png').should.equal('png');
ctx.is('.png').should.equal('.png');
ctx.is('image/png').should.equal('image/png');
ctx.is('image/*').should.equal('image/png');
ctx.is('*/png').should.equal('image/png');
@ -69,8 +69,8 @@ describe('ctx.is(type)', function(){
ctx.header['content-type'] = 'image/png';
ctx.header['transfer-encoding'] = 'chunked';
ctx.is('png').should.equal('image/png');
ctx.is('.png').should.equal('image/png');
ctx.is('png').should.equal('png');
ctx.is('.png').should.equal('.png');
ctx.is('text/*', 'image/*').should.equal('image/png');
ctx.is('image/*', 'text/*').should.equal('image/png');
ctx.is('image/*', 'image/png').should.equal('image/png');
@ -89,9 +89,9 @@ describe('ctx.is(type)', function(){
ctx.header['content-type'] = 'application/x-www-form-urlencoded';
ctx.header['transfer-encoding'] = 'chunked';
ctx.is('urlencoded').should.equal('application/x-www-form-urlencoded');
ctx.is('json', 'urlencoded').should.equal('application/x-www-form-urlencoded');
ctx.is('urlencoded', 'json').should.equal('application/x-www-form-urlencoded');
ctx.is('urlencoded').should.equal('urlencoded');
ctx.is('json', 'urlencoded').should.equal('urlencoded');
ctx.is('urlencoded', 'json').should.equal('urlencoded');
})
})
})