media-typer
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -0,1 +1,6 @@ | ||
0.2.0 / 2014-06-18 | ||
================== | ||
* Add `typer.format()` to format media types | ||
0.1.0 / 2014-06-17 | ||
@@ -2,0 +7,0 @@ ================== |
92
index.js
@@ -30,3 +30,5 @@ /*! | ||
*/ | ||
var paramRegExp = /; *([!#$%&'\*\+\-\.0-9A-Z\^_`a-z\|~]+) *= *("(?:[ !\u0023-\u005b\u005d-\u00ff]|\\[\u0020-\u007e])*"|[!#$%&'\*\+\-\.0-9A-Z\^_`a-z\|~]+) */g; | ||
var paramRegExp = /; *([!#$%&'\*\+\-\.0-9A-Z\^_`a-z\|~]+) *= *("(?:[ !\u0023-\u005b\u005d-\u007e\u0080-\u00ff]|\\[\u0020-\u007e])*"|[!#$%&'\*\+\-\.0-9A-Z\^_`a-z\|~]+) */g; | ||
var textRegExp = /^[\u0020-\u007e\u0080-\u00ff]+$/ | ||
var tokenRegExp = /^[!#$%&'\*\+\-\.0-9A-Z\^_`a-z\|~]+$/ | ||
@@ -42,2 +44,7 @@ /** | ||
/** | ||
* RegExp to match chars that must be quoted-pair in RFC 2616 | ||
*/ | ||
var quoteRegExp = /([\\"])/g; | ||
/** | ||
* RegExp to match type in RFC 6838 | ||
@@ -58,2 +65,4 @@ * | ||
*/ | ||
var subtypeNameRegExp = /^[A-Za-z0-9][A-Za-z0-9!#$&^_.-]{0,126}$/ | ||
var typeNameRegExp = /^[A-Za-z0-9][A-Za-z0-9!#$&^_-]{0,126}$/ | ||
var typeRegExp = /^ *([A-Za-z0-9][A-Za-z0-9!#$&^_-]{0,126})\/([A-Za-z0-9][A-Za-z0-9!#$&^_.+-]{0,126}) *$/; | ||
@@ -65,5 +74,63 @@ | ||
exports.format = format | ||
exports.parse = parse | ||
/** | ||
* Format object to media type. | ||
* | ||
* @param {object} obj | ||
* @return {string} | ||
* @api public | ||
*/ | ||
function format(obj) { | ||
if (!obj || typeof obj !== 'object') { | ||
throw new TypeError('argument obj is required') | ||
} | ||
var parameters = obj.parameters | ||
var subtype = obj.subtype | ||
var suffix = obj.suffix | ||
var type = obj.type | ||
if (!type || !typeNameRegExp.test(type)) { | ||
throw new TypeError('invalid type') | ||
} | ||
if (!subtype || !subtypeNameRegExp.test(subtype)) { | ||
throw new TypeError('invalid subtype') | ||
} | ||
// format as type/subtype | ||
var string = type + '/' + subtype | ||
// append +suffix | ||
if (suffix) { | ||
if (!typeNameRegExp.test(suffix)) { | ||
throw new TypeError('invalid suffix') | ||
} | ||
string += '+' + suffix | ||
} | ||
// append parameters | ||
if (parameters && typeof parameters === 'object') { | ||
var param | ||
var params = Object.keys(parameters).sort() | ||
for (var i = 0; i < params.length; i++) { | ||
param = params[i] | ||
if (!tokenRegExp.test(param)) { | ||
throw new TypeError('invalid parameter name') | ||
} | ||
string += '; ' + param + '=' + qstring(parameters[param]) | ||
} | ||
} | ||
return string | ||
} | ||
/** | ||
* Parse media type to object. | ||
@@ -143,2 +210,25 @@ * | ||
/** | ||
* Quote a string if necessary. | ||
* | ||
* @param {string} val | ||
* @return {string} | ||
* @api private | ||
*/ | ||
function qstring(val) { | ||
var str = String(val) | ||
// no need to quote tokens | ||
if (tokenRegExp.test(str)) { | ||
return str | ||
} | ||
if (str.length > 0 && !textRegExp.test(str)) { | ||
throw new TypeError('invalid parameter value') | ||
} | ||
return '"' + str.replace(quoteRegExp, '\\$1') + '"' | ||
} | ||
/** | ||
* Simply "type/subtype+siffx" into parts. | ||
@@ -145,0 +235,0 @@ * |
{ | ||
"name": "media-typer", | ||
"description": "Simple RFC 6838 media type parser and formatter", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"author": "Douglas Christopher Wilson <doug@somethingdoug.com>", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -23,2 +23,6 @@ # media-typer | ||
```js | ||
var obj = typer.parse('image/svg+xml; charset=utf-8') | ||
``` | ||
Parse a media type string. This will return an object with the following | ||
@@ -37,2 +41,6 @@ properties (examples are shown for the string `'image/svg+xml; charset=utf-8'`): | ||
```js | ||
var obj = typer.parse(req) | ||
``` | ||
Parse the `content-type` header from the given `req`. Short-cut for | ||
@@ -43,5 +51,19 @@ `typer.parse(req.headers['content-type'])`. | ||
```js | ||
var obj = typer.parse(req) | ||
``` | ||
Parse the `content-type` header set on the given `res`. Short-cut for | ||
`typer.parse(res.getHeader('content-type'))`. | ||
### typer.format(obj) | ||
```js | ||
var obj = typer.format({type: 'image', subtype: 'svg', suffix: 'xml'}) | ||
``` | ||
Format an object into a media type string. This will return a string of the | ||
mime type for the given object. For the properties of the object, see the | ||
documentation for `typer.parse(string)`. | ||
## License | ||
@@ -48,0 +70,0 @@ |
10061
215
89