Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

combined update #4

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
af11599
remove index.js symlink
deoxxa Oct 8, 2013
fe8cba7
add package.json
deoxxa Oct 8, 2013
b21b009
fix author name in package.json
deoxxa Oct 8, 2013
c6922c0
add a note to the readme about forking
deoxxa Oct 8, 2013
9f469ba
reformat code to be more readable
deoxxa Oct 8, 2013
b68d419
convert tabs to spaces
figadore Apr 5, 2016
b75029e
fix more formatting
deoxxa Oct 8, 2013
7f85763
...and more
deoxxa Oct 8, 2013
371d284
make example use MediaType.whatever methods
deoxxa Oct 8, 2013
f094c21
fix mediaCmp if parameters are different
deoxxa Oct 8, 2013
08c8247
refactor .select() to be more flexible and remove forced .q
deoxxa Oct 8, 2013
86d1d4d
make example more readable
deoxxa Oct 8, 2013
ca50632
rename contenttype.js to content-type.js
deoxxa Oct 8, 2013
1393237
bump version
deoxxa Oct 8, 2013
0cb16e5
sort types by specificity before preference
deoxxa Oct 8, 2013
03a30d4
fix formatting
deoxxa Oct 8, 2013
872c259
add spaces for nicer stringifications
deoxxa Oct 8, 2013
58c172b
Add sample tests
floatdrop Sep 8, 2014
22ae1d1
formatting and style changes
figadore Apr 5, 2016
6bd1db3
fix error from merging forks
figadore Apr 5, 2016
1952c0e
tests formating/style changes
figadore Apr 5, 2016
0360b13
wrote tests for rfc7231 section 14.1
figadore Apr 6, 2016
09a8bce
refactor to pass tests
figadore Apr 7, 2016
79f106e
fixes, refactor, comments, cleanup
figadore Apr 7, 2016
40f13f5
remove example.js, update readme
figadore Apr 7, 2016
e17d1a2
update for Acubed npm package
figadore Apr 7, 2016
736ffaf
return null instead of throwing error on unacceptable
figadore Apr 7, 2016
d80b7df
fixing prev, forgot to commit changes
figadore Apr 8, 2016
af2c7f6
Add related section in readme
floatdrop Sep 8, 2014
9ce269b
simplify tests a bit
figadore Apr 8, 2016
509dc84
rename content-type.js back to contenttype.js
figadore Apr 8, 2016
cb30dde
select() returns MediaType object
figadore Apr 14, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
143 changes: 119 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,46 +1,136 @@
# Content-Type parsing

## new MediaType(type, [parameters])
## Usage

The MediaType represents a parsed Media Type. For use in HTTP, the first (but only the first) `q` parameter will be parsed as a float.
Parse
```
var MediaType = require('contenttype');

var type = MediaType.parseMedia('text/html;q=1');
console.log(type);
// MediaType { type: 'text/html', params: {}, q: 1 }

```

Express
```
var MediaType = require('contenttype');
var representations = [
'application/json',
'text/html'
];

app.use(function (req, res, next) {
var representation = MediaType.select(representations, req.headers.accept);
if (representation === null) {
res.status(406);
res.json({error: "No valid content-type found for specified Accept header"});
}
res.rep = representation;
next();
});

app.get('/', function (req, res, next) {
res.set("Content-Type", res.rep);
if (res.rep === "application/json") {
res.json({message: "hello"});
} else if (res.rep === "text/html") {
res.send("hello");
}
});
```

#### `new MediaType(type, [parameters])`

The MediaType "class" represents a parsed Media Type. For use in HTTP, the `q` parameter will be parsed as a float.
Other parameters are available through the `params` object.
The first argument is the full media type, the second argument, if provided, is strictly a list of parameters.

The `toString` method converts the object back into a Media type.
Example
```
console.log(new MediaType('text/html;l=3;q=0.7', { p: 4 }));
// MediaType { type: 'text/html', params: { l: '3', p: '4' }, q: 0.7 }
```

```javascript
var p = new MediaType('text/html;level=1;q=0.5');
p.q === 0.5;
p.params.level === "1"
Example
```
console.log(new MediaType('text/html;l=3', 'p=4;l=5;q=1'));
// MediaType { type: 'text/html', params: { l: '5', p: '4' }, q: 1 }
```

Example
```
var type = new MediaType('text/html;l=3', 'p=4;l=5');
console.log(new MediaType(type, 'p=6;'));
// MediaType { type: 'text/html', params: { l: '5', p: '6' } }
```

#### `toString()`

Convert a MediaType object to a string

Example
```
var type = new MediaType('text/html;l=3;q=0.5');
console.log(type.toString());
// "text/html; l=5; q=0.5"
```

var q = new MediaType('application/json', {profile: 'http://example.com/schema.json'});
q.type === "application/json";
q.params.profile === "http://example.com/schema.json";
#### parseMedia(type)
Parse a media type. Returns a new instance of MediaType.

q.q = 1;
q.toString() === 'application/json;q=1;profile="http://example.com/schema.json"';
Example
```
var type = MediaType.parseMedia('text/html;l=3');
console.log(type);
// MediaType { type: 'text/html', params: { l: '3' } }
```


## parseMedia(type)
Returns a new instance of MediaType.
#### splitQuotedString(str, [delimiter=';'], [quote='"'])
Splits a string by a delimiter character (default: semicolon), ignoring sections enclosed by quotes (default: double quote).

## splitQuotedString(str, delimiter, quote)
Splits a string by a delimiter character (default: semicolon), ignoring quoted sections (default: double quote).
Example
```
var items = MediaType.splitQuotedString("text/html;level=2;q=1");
console.log(items);
// [ 'text/html', 'level=2', 'q=1' ]
```

#### splitContentTypes(str)
Convenience method for `splitQuotedString(str, ',', '"');`. Splits an Accept (or similar) header into an Array of content-types strings

## splitContentTypes(str)
Splits an Accept (or similar) header into an Array of strings of content-types.
Example
```
var types = MediaType.splitContentTypes("text/html;level=2;q=1,application/json,*/*");
console.log(types);
// [ 'text/html;level=2;q=1', 'application/json', '*/*' ]
```

Example
```javascript
splitContentType('application/json, text/html').map(parseMedia)
var types = MediaType.splitContentTypes('application/json, text/html').map(MediaType.parseMedia);
console.log(types);
// [ MediaType { type: 'application/json', params: {} }, MediaType { type: 'text/html', params: {} } ]
```

## select(reps, accept)
Pick an ideal representation to send, given an Array of representations to choose from, and the client-preferred list as an Array.
#### select(representations, accept)
Pick an ideal representation to send, given an array of representations (strings or MediaTypes) to choose from, and the client-preferred Accept list (as a string, an array of strings, or an array of MediaTypes). Multiplies client type's quality factor by server type's quality factor

See example.js for an example.
Example
```javascript
var representations = [
"text/html; q=0.7",
"text/plain; q=0.5",
"image/jpeg"
];
var accept = MediaType.splitContentTypes('text/html;q=0.7, text/plain, */*;q=0.1');
var selected = (MediaType.select(representations, accept)).toString();
// text/html gets q=0.49, text/plain gets q=0.5, image/jpeg gets q=0.1
console.log(selected);
// "text/plain"
```

## mediaCmp(a, b)
#### mediaCmp(a, b)

Accepts two MediaType instances and tests them for being a subset/superset.

Expand All @@ -51,9 +141,14 @@ If they are disjoint, return null.

The q-value, if any, is ignored.

Example
```javascript
mediaCmp(parseMedia('text/html'), parseMedia('text/html')) === 0
mediaCmp(parseMedia('*/*'), parseMedia('text/html')) === 1
mediaCmp(parseMedia('text/html;level=1'), parseMedia('text/html')) === -1
mediaCmp(parseMedia('application/json;profile="v1.json"'), parseMedia('application/json;profile="v2.json"')) === null
```

# Related

* [media-typer](https://github.com/jshttp/media-typer) - if you need simple RFC 6838 media type parser and formatter.
Loading