@calmdownval/media-type
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

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'
  }
}

Package Sidebar

Install

npm i @calmdownval/media-type

Weekly Downloads

4

Version

1.0.0

License

GPL-3.0-or-later

Unpacked Size

41.8 kB

Total Files

9

Last publish

Collaborators

  • calmdownval