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

v1.0.1

Published

`mime-entries` is a utility for handling and converting between MIME types and file extensions.

Downloads

3

Readme

Mime Entries

mime-entries is a utility for handling and converting between MIME types and file extensions.

It provides a simple interface to fetch MIME types based on file extensions and vice versa. This can be particularly useful in scenarios where you need to determine the MIME type of a file using the name or path, based on their extension.

It uses mime-db as references for MIME types and file extensions.

Installation

npm install mime-entries

(You can also use yarn or pnpm instead of npm)

Usage

import { getMimeType, getExtension } from 'mime-entries';

// Create a new instance of MimeEntries
const mimeEntries = new MimeEntries();

// Get MIME types by file extension
const types = mimeEntries.getTypesByExtension('.txt');
console.log(types);

// Get all extensions for a given MIME type
const extensions = mimeEntries.getAllExtensions('text/plain');
console.log(extensions);

// Get MIME entry by type
const entryByType = mimeEntries.getMimeEntry('text/plain');
console.log(entryByType);

// Get MIME entries by extension
const entriesByExt = mimeEntries.getMimeEntriesByExt('.txt');
console.log(entriesByExt);

It is also possible to add custom types and extensions to the instance.

import { MimeEntries } from 'mime-entries';

// Create a new instance of MimeEntries
const database = new MimeEntries(
	{"text/x-python" : {"extensions": ["py"],"compressible": true}}
);
const entriesByExt = database.getMimeEntriesByExt('.py');
console.log(entriesByExt); // => { 'text/x-python': { extensions: [ 'py' ], compressible: true } }

API

getTypesByExtension(extension: string): Set<string> | undefined

Returns a set of MIME types for a given file extension.

database.getTypesByExtension("txt") // => Set { 'text/plain' }
database.getTypesByExtension(".json") // => Set { 'application/json' }
database.getTypesByExtension("js") // => Set { 'application/javascript', 'text/javascript' }
database.getTypesByExtension("dir/text.txt") // => Set { 'text/plain' }
database.getTypesByExtension("dir\\text.txt") // => Set { 'text/plain' }
database.getTypesByExtension(".txt.txt") // => Set { 'text/plain' }
database.getTypesByExtension(".txt") // => Set { 'text/plain' }

undefined is returned if the extension is not found.

database.getTypesByExtension("unknown") // => undefined

And an error is thrown if the extension is empty or invalid.

database.getTypesByExtension(" ") // => Error: Invalid path

getAllExtensions(type: string): Set<string> | undefined

Returns a set of file extensions for a given MIME type.

database.getAllExtensions("text/plain") // => { 'txt', 'text', 'conf', 'def', 'list', 'log', 'in', 'ini' }
database.getAllExtensions("application/json") // => { 'json', 'map' }

getMimeEntry(type: string): MimeEntry | undefined

Just an alias of this.database[type].

database.getMimeEntry("text/plain") // => { source: 'iana', charset: 'UTF-8', compressible: true, extensions: [ 'txt', 'text', 'conf', 'def', 'list', 'log', 'in', 'ini' ] }

getMimeEntriesByExt(extension: string): {[type: string]: MimeEntry} | undefined

Returns a map of MIME entries for a given file extension.

database.getMimeEntriesByExt("txt") // => { 'text/plain': { source: 'iana', charset: 'UTF-8', compressible: true, extensions: [ 'txt', 'text', 'conf', 'def', 'list', 'log', 'in', 'ini' ] } }
database.getMimeEntriesByExt("js") /*
 => 'application/javascript': {
    source: 'iana',
    charset: 'UTF-8',
    compressible: true,
    extensions: [ 'js', 'mjs' ]
  },
  {
	'text/javascript': { source: 'iana', compressible: true }
  }
*/

getMainTypeByExt(extension: string): Set<string> | undefined

Returns a set of main MIME types for a given file extension. Main is the first part of the MIME type, e.g. text in text/plain.

database.getMainTypeByExt("txt") // => Set { 'text' }
database.getMainTypeByExt("js") // => Set { 'application', 'text' }

Credit & Thanks

I was heavily inspired by mime but wanted to add some function. I ended up to write my own version, hopefully more understandable and with more features than mime.

Contributing

The package use commit-and-tag-version for release, so please follow the Conventional Commits specification.

Don't forget to update the tests before submitting a PR!