README.md
1 # media-typer 2 3 [![NPM Version][npm-version-image]][npm-url] 4 [![NPM Downloads][npm-downloads-image]][npm-url] 5 [![Node.js Version][node-version-image]][node-version-url] 6 [![Build Status][travis-image]][travis-url] 7 [![Test Coverage][coveralls-image]][coveralls-url] 8 9 Simple RFC 6838 media type parser. 10 11 This module will parse a given media type into it's component parts, like type, 12 subtype, and suffix. A formatter is also provided to put them back together and 13 the two can be combined to normalize media types into a canonical form. 14 15 If you are looking to parse the string that represents a media type and it's 16 parameters in HTTP (for example, the `Content-Type` header), use the 17 [content-type module](https://www.npmjs.com/package/content-type). 18 19 ## Installation 20 21 This is a [Node.js](https://nodejs.org/en/) module available through the 22 [npm registry](https://www.npmjs.com/). Installation is done using the 23 [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally): 24 25 ```sh 26 $ npm install media-typer 27 ``` 28 29 ## API 30 31 <!-- eslint-disable no-unused-vars --> 32 33 ```js 34 var typer = require('media-typer') 35 ``` 36 37 ### typer.parse(string) 38 39 <!-- eslint-disable no-undef, no-unused-vars --> 40 41 ```js 42 var obj = typer.parse('image/svg+xml') 43 ``` 44 45 Parse a media type string. This will return an object with the following 46 properties (examples are shown for the string `'image/svg+xml; charset=utf-8'`): 47 48 - `type`: The type of the media type (always lower case). Example: `'image'` 49 50 - `subtype`: The subtype of the media type (always lower case). Example: `'svg'` 51 52 - `suffix`: The suffix of the media type (always lower case). Example: `'xml'` 53 54 If the given type string is invalid, then a `TypeError` is thrown. 55 56 ### typer.format(obj) 57 58 <!-- eslint-disable no-undef, no-unused-vars --> 59 60 ```js 61 var obj = typer.format({ type: 'image', subtype: 'svg', suffix: 'xml' }) 62 ``` 63 64 Format an object into a media type string. This will return a string of the 65 mime type for the given object. For the properties of the object, see the 66 documentation for `typer.parse(string)`. 67 68 If any of the given object values are invalid, then a `TypeError` is thrown. 69 70 ### typer.test(string) 71 72 <!-- eslint-disable no-undef, no-unused-vars --> 73 74 ```js 75 var valid = typer.test('image/svg+xml') 76 ``` 77 78 Validate a media type string. This will return `true` is the string is a well- 79 formatted media type, or `false` otherwise. 80 81 ## License 82 83 [MIT](LICENSE) 84 85 [coveralls-image]: https://badgen.net/coveralls/c/github/jshttp/media-typer/master 86 [coveralls-url]: https://coveralls.io/r/jshttp/media-typer?branch=master 87 [node-version-image]: https://badgen.net/npm/node/media-typer 88 [node-version-url]: https://nodejs.org/en/download 89 [npm-downloads-image]: https://badgen.net/npm/dm/media-typer 90 [npm-url]: https://npmjs.org/package/media-typer 91 [npm-version-image]: https://badgen.net/npm/v/media-typer 92 [travis-image]: https://badgen.net/travis/jshttp/media-typer/master 93 [travis-url]: https://travis-ci.org/jshttp/media-typer