is-valid-mime
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

isValidMime

Returns true if the string corresponds to a valid MIME type.

Build Status codecov Maintainability

Summary

About

I created this package to have a straight forward way to check if a MIME type is valid in my code.

Note that if you do not want to pull a dependency, here is how you can do it without this package:

import mimeDb from "mime-db";
 
const mimeType = "text/html";
 
if (mimeType in mimeDb) {
  console.log(`${mimeType} is a valid MIME type`);
} else {
  console.log(`${mimeType} is not a valid MIME type`);
}

This package provides its own Typescript definition.

Features

  • Returns true if the mime passed in parameter is valid, else returns false

Requirements

NPM or Yarn installed on your machine.

Installation

Using NPM

npm install is-valid-mime

Using Yarn

yarn add is-valid-mime

Examples

1. Getting started

In this example, you will see how to check if a variable is a valid mime type.

import isValidMime from "is-valid-mime";
 
const mimeType = "cats";
 
if (isValidMime(mimeType)) {
  console.log("valid");
} else {
  console.log("not valid");
}

2. Catch errors

In this example, you will see how to catch errors that are thrown by this function.

import isValidMime from "is-valid-mime";
 
const mimeType = 42;
let valid = false;
 
try {
  valid = isValidMime(mimeType);
} catch (exception) {
  if (exception instanceof TypeError) {
    console.log("expected isValidMime to be called with a string parameter");
  } else {
    console.log("Unhandled error happened");
  }
}
 
if (valid) {
  console.log("valid");
} else {
  console.log("not valid");
}

API

const isValidMime = (mime: string): boolean;

Exceptions

  • TypeError: if the first parameter is not a string.

Readme

Keywords

Package Sidebar

Install

npm i is-valid-mime

Weekly Downloads

2

Version

0.1.0

License

MIT

Unpacked Size

5.69 kB

Total Files

6

Last publish

Collaborators

  • khalyomede