content-type
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -0,1 +1,6 @@ | ||
1.0.3 / 2017-09-10 | ||
================== | ||
* perf: remove argument reassignment | ||
1.0.2 / 2016-05-09 | ||
@@ -2,0 +7,0 @@ ================== |
78
index.js
@@ -23,5 +23,5 @@ /*! | ||
*/ | ||
var paramRegExp = /; *([!#$%&'\*\+\-\.\^_`\|~0-9A-Za-z]+) *= *("(?:[\u000b\u0020\u0021\u0023-\u005b\u005d-\u007e\u0080-\u00ff]|\\[\u000b\u0020-\u00ff])*"|[!#$%&'\*\+\-\.\^_`\|~0-9A-Za-z]+) */g | ||
var textRegExp = /^[\u000b\u0020-\u007e\u0080-\u00ff]+$/ | ||
var tokenRegExp = /^[!#$%&'\*\+\-\.\^_`\|~0-9A-Za-z]+$/ | ||
var PARAM_REGEXP = /; *([!#$%&'*+.^_`|~0-9A-Za-z-]+) *= *("(?:[\u000b\u0020\u0021\u0023-\u005b\u005d-\u007e\u0080-\u00ff]|\\[\u000b\u0020-\u00ff])*"|[!#$%&'*+.^_`|~0-9A-Za-z-]+) */g | ||
var TEXT_REGEXP = /^[\u000b\u0020-\u007e\u0080-\u00ff]+$/ | ||
var TOKEN_REGEXP = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/ | ||
@@ -34,3 +34,3 @@ /** | ||
*/ | ||
var qescRegExp = /\\([\u000b\u0020-\u00ff])/g | ||
var QESC_REGEXP = /\\([\u000b\u0020-\u00ff])/g | ||
@@ -40,6 +40,6 @@ /** | ||
*/ | ||
var quoteRegExp = /([\\"])/g | ||
var QUOTE_REGEXP = /([\\"])/g | ||
/** | ||
* RegExp to match type in RFC 6838 | ||
* RegExp to match type in RFC 7231 sec 3.1.1.1 | ||
* | ||
@@ -50,3 +50,3 @@ * media-type = type "/" subtype | ||
*/ | ||
var typeRegExp = /^[!#$%&'\*\+\-\.\^_`\|~0-9A-Za-z]+\/[!#$%&'\*\+\-\.\^_`\|~0-9A-Za-z]+$/ | ||
var TYPE_REGEXP = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+\/[!#$%&'*+.^_`|~0-9A-Za-z-]+$/ | ||
@@ -69,3 +69,3 @@ /** | ||
function format(obj) { | ||
function format (obj) { | ||
if (!obj || typeof obj !== 'object') { | ||
@@ -78,3 +78,3 @@ throw new TypeError('argument obj is required') | ||
if (!type || !typeRegExp.test(type)) { | ||
if (!type || !TYPE_REGEXP.test(type)) { | ||
throw new TypeError('invalid type') | ||
@@ -93,3 +93,3 @@ } | ||
if (!tokenRegExp.test(param)) { | ||
if (!TOKEN_REGEXP.test(param)) { | ||
throw new TypeError('invalid parameter name') | ||
@@ -113,3 +113,3 @@ } | ||
function parse(string) { | ||
function parse (string) { | ||
if (!string) { | ||
@@ -119,21 +119,17 @@ throw new TypeError('argument string is required') | ||
if (typeof string === 'object') { | ||
// support req/res-like objects as argument | ||
string = getcontenttype(string) | ||
// support req/res-like objects as argument | ||
var header = typeof string === 'object' | ||
? getcontenttype(string) | ||
: string | ||
if (typeof string !== 'string') { | ||
throw new TypeError('content-type header is missing from object'); | ||
} | ||
} | ||
if (typeof string !== 'string') { | ||
if (typeof header !== 'string') { | ||
throw new TypeError('argument string is required to be a string') | ||
} | ||
var index = string.indexOf(';') | ||
var index = header.indexOf(';') | ||
var type = index !== -1 | ||
? string.substr(0, index).trim() | ||
: string.trim() | ||
? header.substr(0, index).trim() | ||
: header.trim() | ||
if (!typeRegExp.test(type)) { | ||
if (!TYPE_REGEXP.test(type)) { | ||
throw new TypeError('invalid media type') | ||
@@ -147,5 +143,5 @@ } | ||
paramRegExp.lastIndex = index | ||
PARAM_REGEXP.lastIndex = index | ||
while (match = paramRegExp.exec(string)) { | ||
while ((match = PARAM_REGEXP.exec(header))) { | ||
if (match.index !== index) { | ||
@@ -163,3 +159,3 @@ throw new TypeError('invalid parameter format') | ||
.substr(1, value.length - 2) | ||
.replace(qescRegExp, '$1') | ||
.replace(QESC_REGEXP, '$1') | ||
} | ||
@@ -170,3 +166,3 @@ | ||
if (index !== -1 && index !== string.length) { | ||
if (index !== -1 && index !== header.length) { | ||
throw new TypeError('invalid parameter format') | ||
@@ -186,12 +182,18 @@ } | ||
function getcontenttype(obj) { | ||
function getcontenttype (obj) { | ||
var header | ||
if (typeof obj.getHeader === 'function') { | ||
// res-like | ||
return obj.getHeader('content-type') | ||
header = obj.getHeader('content-type') | ||
} else if (typeof obj.headers === 'object') { | ||
// req-like | ||
header = obj.headers && obj.headers['content-type'] | ||
} | ||
if (typeof obj.headers === 'object') { | ||
// req-like | ||
return obj.headers && obj.headers['content-type'] | ||
if (typeof header !== 'string') { | ||
throw new TypeError('content-type header is missing from object') | ||
} | ||
return header | ||
} | ||
@@ -207,15 +209,15 @@ | ||
function qstring(val) { | ||
function qstring (val) { | ||
var str = String(val) | ||
// no need to quote tokens | ||
if (tokenRegExp.test(str)) { | ||
if (TOKEN_REGEXP.test(str)) { | ||
return str | ||
} | ||
if (str.length > 0 && !textRegExp.test(str)) { | ||
if (str.length > 0 && !TEXT_REGEXP.test(str)) { | ||
throw new TypeError('invalid parameter value') | ||
} | ||
return '"' + str.replace(quoteRegExp, '\\$1') + '"' | ||
return '"' + str.replace(QUOTE_REGEXP, '\\$1') + '"' | ||
} | ||
@@ -227,5 +229,5 @@ | ||
*/ | ||
function ContentType(type) { | ||
function ContentType (type) { | ||
this.parameters = Object.create(null) | ||
this.type = type | ||
} |
{ | ||
"name": "content-type", | ||
"description": "Create and parse HTTP Content-Type header", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"author": "Douglas Christopher Wilson <doug@somethingdoug.com>", | ||
@@ -16,3 +16,9 @@ "license": "MIT", | ||
"devDependencies": { | ||
"istanbul": "0.4.3", | ||
"eslint": "3.19.0", | ||
"eslint-config-standard": "10.2.1", | ||
"eslint-plugin-import": "2.7.0", | ||
"eslint-plugin-node": "5.1.1", | ||
"eslint-plugin-promise": "3.5.0", | ||
"eslint-plugin-standard": "3.0.1", | ||
"istanbul": "0.4.5", | ||
"mocha": "~1.21.5" | ||
@@ -30,2 +36,3 @@ }, | ||
"scripts": { | ||
"lint": "eslint .", | ||
"test": "mocha --reporter spec --check-leaks --bail test/", | ||
@@ -32,0 +39,0 @@ "test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/", |
10031
176
8