npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@shinymayhem/contenttype

v3.0.0

Published

Javascript/ECMAScript library for parsing Content-Type and Media/MIME type strings

Downloads

4

Readme

Content-Type parsing

Usage

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.

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 }

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"

parseMedia(type)

Parse a media type. Returns a new instance of MediaType.

Example

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

splitQuotedString(str, [delimiter=';'], [quote='"'])

Splits a string by a delimiter character (default: semicolon), ignoring sections enclosed by quotes (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

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

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(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

Example

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)

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

If a is a superset of b (b is smaller than a), return 1. If b is a superset of a, return -1. If they are the exact same, return 0. If they are disjoint, return null.

The q-value, if any, is ignored.

Example

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('text/html;level=1.2.*'), parseMedia('text/html;level=1.2.3')) === 0
mediaCmp(parseMedia('application/json;profile="v1.json"'), parseMedia('application/json;profile="v2.json"')) === null

Related

  • media-typer - if you need simple RFC 6838 media type parser and formatter.