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

mime-content

v0.0.9

Published

A simple mime-type based string parser

Downloads

574

Readme

mime-content

A node.js module for parsing strings based on MIME Type.

Build Status

Usage

This module supports parsing strings for a small handful of common MIME Types. Pull requests for adding additional types are welcome.

HTML

After parsing a HTML string, CSS selectors can be used to query the parsed content. The cheerio module is used to query the DOM. Selectors supported by JQuery can be used.

Use the toString() function to retrieve the un-parsed content.

var content = require('mime-content');

var html = content('<html><body><h1 class="big">Header!</h1></body></html>', 'text/html');

html('h1.big').text();
=> 'Header!'

JSON

After parsing a JSON string, a regular object is returned. Use the toString() function on that object to retrieve the un-parsed content.

var content = require('mime-content');

var json = content('{"foo":"bar"}', 'application/json');

json.foo
=> 'bar'

XML

Both the application/xml and text/xml MIME Types are supported.

After parsing a XML string, the xmldom document is available. Also, XPath support is provided by the xpath module. All DOM 3 XPath expressions are supported.

By default, the XML parser assumes your XPath expression is looking for a single DOM node so it returns the first match in the document. If you'd like all matching nodes instead, use the second optional boolean parameter with your query. Pass true to return all matching nodes.

var content = require('mime-content');

var xml = content('<people><person id="123"><name>Bob Smith</name></person><person id="456"><name>Jimmy Dean</name></person></people>', 'text/xml');

xml.xpath('/people/person/name/text()').data;
=> 'Bob Smith'

xml.xpath('/people/person/name/text()', true).map(function(text) {
  return text.data;
});
=> ['Bob Smith', 'Jimmy Dean']

xml.xpath('/people/person/@id', true).map(function(attr) {
  return attr.value;
});
=> ['123', '456']

The toObject() function uses xml2js to return an object representation of the XML. This function accepts an options object that is passed directly to the xml2js Parser. See the xml2js Readme for the supported options.

var xml = content('<people><person id="123"><name>Bob Smith</name></person><person id="456"><name>Jimmy Dean</name></person></people>', 'text/xml');
xml.toObject({explicitArray: false, explicitRoot: false, mergeAttrs: true});
=>
{
  person: [
    { id: '123', name: 'Bob Smith' },
    { id: '456', name: 'Jimmy Dean' }
  ]
}

URL Encoding

After parsing a application/x-www-form-urlencoded string, a regular object is returned.

var content = require('mime-content');

var qs = content('foo=bar&foo=baz&baz=bip', 'application/x-www-form-urlencoded');

qs.foo
=> ['bar', 'baz']

qs.baz
=> 'bip'