@calmdownval/media-type
v1.0.0
Published
media type type parser
Downloads
1
Maintainers
Readme
Media Type parser
This module uses ESM import/export and requires Node v8.15.0+ with the --experimental-modules
flag. Please see Node's documentation
for details.
Parses out individual parts of the media type syntax and returns a data structure fully describing the type. Validates top-level types, trees and suffixes, supports multiple parameters and comments.
See RFC 2046 for details of the syntax and its rules. This library does not yet support any extensions such as RFC 2231 and it does not check sub-types against the IANA media-type registry either.
Installation
npm i --save @calmdownval/media-type
Usage
import { parse } from '@calmdownval/media-type';
// no options needed, parses the type into a nested object structure
// returns null for invalid media types
parse('application/vnd.api+json; charset=utf-8');
parse output:
{
type: {
name: 'application',
isExtension: false
},
subType: {
name: 'vnd.api+json',
tree: 'vnd',
suffix: 'json'
},
// parameters are case-insensitive, output is always lowercased
parameters: {
charset: 'utf-8'
}
}