Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

media-typer

jshttp94.8mMIT1.1.0TypeScript support: definitely-typed

Simple RFC 6838 media type parser and formatter

readme

media-typer

NPM Version NPM Downloads Node.js Version Build Status Test Coverage

Simple RFC 6838 media type parser.

This module will parse a given media type into it's component parts, like type, subtype, and suffix. A formatter is also provided to put them back together and the two can be combined to normalize media types into a canonical form.

If you are looking to parse the string that represents a media type and it's parameters in HTTP (for example, the Content-Type header), use the content-type module.

Installation

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install media-typer

API

var typer = require('media-typer')

typer.parse(string)

var obj = typer.parse('image/svg+xml')

Parse a media type string. This will return an object with the following properties (examples are shown for the string 'image/svg+xml; charset=utf-8'):

  • type: The type of the media type (always lower case). Example: 'image'

  • subtype: The subtype of the media type (always lower case). Example: 'svg'

  • suffix: The suffix of the media type (always lower case). Example: 'xml'

If the given type string is invalid, then a TypeError is thrown.

typer.format(obj)

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).

If any of the given object values are invalid, then a TypeError is thrown.

typer.test(string)

var valid = typer.test('image/svg+xml')

Validate a media type string. This will return true is the string is a well- formatted media type, or false otherwise.

License

MIT

changelog

1.1.0 / 2019-04-24

  • Add test(string) function

1.0.2 / 2019-04-19

  • Fix JSDoc comment for parse function

1.0.1 / 2018-10-20

  • Remove left over parameters property from class

1.0.0 / 2018-10-20

This major release brings the module back to it's RFC 6838 roots. If you want a module to parse the Content-Type or similar HTTP headers, use the content-type module instead.

  • Drop support for Node.js below 0.8
  • Remove parameter handling, which is outside RFC 6838 scope
  • Remove parse(req) and parse(res) signatures
  • perf: enable strict mode
  • perf: use a class for object creation

0.3.0 / 2014-09-07

  • Support Node.js 0.6
  • Throw error when parameter format invalid on parse

0.2.0 / 2014-06-18

  • Add typer.format() to format media types

0.1.0 / 2014-06-17

  • Accept req as argument to parse
  • Accept res as argument to parse
  • Parse media type with extra LWS between type and first parameter

0.0.0 / 2014-06-13

  • Initial implementation