Compiles a function that matches a given MIME type pattern. A faster alternative to type-is
for when the MIME type is known ahead of time and only a true
or false
result is needed.
# npm
npm install compile-mime-match
# yarn
yarn add compile-mime-match
const compileMimeMatch = require('compile-mime-match');
const mimeMatch = compileMimeMatch('application/json');
mimeMatch('application/json'); // true
mimeMatch('application/json; charset=utf-8'); // true
mimeMatch('text/plain'); // false
Matches exactly the given MIME type (see the example above).
Matches any MIME type will the given type
.
const compileMimeMatch = require('compile-mime-match');
const mimeMatch = compileMimeMatch('text/*');
mimeMatch('text/plain'); // true
mimeMatch('text/html'); // true
mimeMatch('image/png'); // false
Matches any MIME type will the given subtype
.
const compileMimeMatch = require('compile-mime-match');
const mimeMatch = compileMimeMatch('*/xml');
mimeMatch('application/xml'); // true
mimeMatch('text/xml'); // true
mimeMatch('image/svg+xml'); // false
Matches any valid MIME type.
const compileMimeMatch = require('compile-mime-match');
const mimeMatch = compileMimeMatch('*/*');
mimeMatch('image/png'); // true
mimeMatch('application/x-www-form-urlencoded'); // true
mimeMatch('invalid'); // false
mimeMatch('/'); // false
Matches any valid MIME type that ends with +suffix
.
const compileMimeMatch = require('compile-mime-match');
const mimeMatch = compileMimeMatch('+json');
mimeMatch('application/calendar+json'); // true
mimeMatch('application/geo+json'); // true
mimeMatch('application/json'); // false
mimeMatch('invalid+json'); // false
compile-mime-match
also accepts an array of strings to match multiple types at the same time:
const compileMimeMatch = require('compile-mime-match');
const mimeMatch = compileMimeMatch(['application/json', 'text/*']);
mimeMatch('application/json'); // true
mimeMatch('application/json; charset=utf-8'); // true
mimeMatch('text/plain'); // true
mimeMatch('text/html'); // true
mimeMatch('image/png'); // false