This package will parse \href{https://mimesniff.spec.whatwg.org/\#understanding-mime-types}{\texttt{ MIME types}} into a structured format, which can then be manipulated and serialized\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{const MIMEType = require("{}whatwg-\/mimetype"{});}
\DoxyCodeLine{}
\DoxyCodeLine{const mimeType = new MIMEType(`Text/HTML;Charset="{}utf-\/8"{}`);}
\DoxyCodeLine{}
\DoxyCodeLine{console.assert(mimeType.toString() === "{}text/html;charset=utf-\/8"{});}
\DoxyCodeLine{}
\DoxyCodeLine{console.assert(mimeType.type === "{}text"{});}
\DoxyCodeLine{console.assert(mimeType.subtype === "{}html"{});}
\DoxyCodeLine{console.assert(mimeType.essence === "{}text/html"{});}
\DoxyCodeLine{console.assert(mimeType.parameters.get("{}charset"{}) === "{}utf-\/8"{});}
\DoxyCodeLine{}
\DoxyCodeLine{mimeType.parameters.set("{}charset"{}, "{}windows-\/1252"{});}
\DoxyCodeLine{console.assert(mimeType.parameters.get("{}charset"{}) === "{}windows-\/1252"{});}
\DoxyCodeLine{console.assert(mimeType.toString() === "{}text/html;charset=windows-\/1252"{});}
\DoxyCodeLine{}
\DoxyCodeLine{console.assert(mimeType.isHTML() === true);}
\DoxyCodeLine{console.assert(mimeType.isXML() === false); }

\end{DoxyCode}


Parsing is a fairly complex process; see \href{https://mimesniff.spec.whatwg.org/\#parsing-a-mime-type}{\texttt{ the specification}} for details (and similarly \href{https://mimesniff.spec.whatwg.org/\#serializing-a-mime-type}{\texttt{ for serialization}}).

This package\textquotesingle{}s algorithms conform to those of the WHATWG \href{https://mimesniff.spec.whatwg.org/}{\texttt{ MIME Sniffing Standard}}, and is aligned up to commit \href{https://github.com/whatwg/mimesniff/commit/126286ab2dcf3e2d541349ed93539a88bf394ad5}{\texttt{ 126286a}}.\hypertarget{md_sfcode_node_modules_whatwg_mimetype_README_autotoc_md34027}{}\doxysection{$<$tt$>$\+MIMEType$<$/tt$>$ API}\label{md_sfcode_node_modules_whatwg_mimetype_README_autotoc_md34027}
This package\textquotesingle{}s main module\textquotesingle{}s default export is a class, {\ttfamily MIMEType}. Its constructor takes a string which it will attempt to parse into a MIME type; if parsing fails, an {\ttfamily Error} will be thrown.\hypertarget{md_sfcode_node_modules_whatwg_mimetype_README_autotoc_md34028}{}\doxysubsection{The $<$tt$>$parse()$<$/tt$>$ static factory method}\label{md_sfcode_node_modules_whatwg_mimetype_README_autotoc_md34028}
As an alternative to the constructor, you can use {\ttfamily MIMEType.\+parse(string)}. The only difference is that {\ttfamily parse()} will return {\ttfamily null} on failed parsing, whereas the constructor will throw. It thus makes the most sense to use the constructor in cases where unparseable MIME types would be exceptional, and use {\ttfamily parse()} when dealing with input from some unconstrained source.\hypertarget{md_sfcode_node_modules_whatwg_mimetype_README_autotoc_md34029}{}\doxysubsection{Properties}\label{md_sfcode_node_modules_whatwg_mimetype_README_autotoc_md34029}

\begin{DoxyItemize}
\item {\ttfamily type}\+: the MIME type\textquotesingle{}s \href{https://mimesniff.spec.whatwg.org/\#mime-type-type}{\texttt{ type}}, e.\+g. {\ttfamily \char`\"{}text\char`\"{}}
\item {\ttfamily subtype}\+: the MIME type\textquotesingle{}s \href{https://mimesniff.spec.whatwg.org/\#mime-type-subtype}{\texttt{ subtype}}, e.\+g. {\ttfamily \char`\"{}html\char`\"{}}
\item {\ttfamily essence}\+: the MIME type\textquotesingle{}s \href{https://mimesniff.spec.whatwg.org/\#mime-type-essence}{\texttt{ essence}}, e.\+g. {\ttfamily \char`\"{}text/html\char`\"{}}
\item {\ttfamily parameters}\+: an instance of {\ttfamily MIMEType\+Parameters}, containing this MIME type\textquotesingle{}s \href{https://mimesniff.spec.whatwg.org/\#mime-type-parameters}{\texttt{ parameters}}
\end{DoxyItemize}

{\ttfamily type} and {\ttfamily subtype} can be changed. They will be validated to be non-\/empty and only contain \href{https://mimesniff.spec.whatwg.org/\#http-token-code-point}{\texttt{ HTTP token code points}}.

{\ttfamily essence} is only a getter, and cannot be changed.

{\ttfamily parameters} is also a getter, but the contents of the {\ttfamily MIMEType\+Parameters} object are mutable, as described below.\hypertarget{md_sfcode_node_modules_whatwg_mimetype_README_autotoc_md34030}{}\doxysubsection{Methods}\label{md_sfcode_node_modules_whatwg_mimetype_README_autotoc_md34030}

\begin{DoxyItemize}
\item {\ttfamily to\+String()} serializes the MIME type to a string
\item {\ttfamily is\+HTML()}\+: returns true if this instance represents \href{https://mimesniff.spec.whatwg.org/\#html-mime-type}{\texttt{ a HTML MIME type}}
\item {\ttfamily is\+XML()}\+: returns true if this instance represents \href{https://mimesniff.spec.whatwg.org/\#xml-mime-type}{\texttt{ an XML MIME type}}
\item {\ttfamily is\+Java\+Script(\{ allow\+Parameters \})}\+: returns true if this instance represents \href{https://html.spec.whatwg.org/multipage/scripting.html\#javascript-mime-type}{\texttt{ a Java\+Script MIME type}}; {\ttfamily allow\+Parameters} can be set to true to allow arbitrary parameters, instead of their presence causing the method to return {\ttfamily false}
\end{DoxyItemize}

{\itshape Note\+: the {\ttfamily is\+HTML()}, {\ttfamily is\+XML()}, and {\ttfamily is\+Java\+Script()} methods are speculative, and may be removed or changed in future major versions. See \href{https://github.com/whatwg/mimesniff/issues/48}{\texttt{ whatwg/mimesniff\#48}} for brainstorming in this area. Currently we implement these mainly because they are useful in jsdom.}\hypertarget{md_sfcode_node_modules_whatwg_mimetype_README_autotoc_md34031}{}\doxysection{$<$tt$>$\+MIMEType\+Parameters$<$/tt$>$ API}\label{md_sfcode_node_modules_whatwg_mimetype_README_autotoc_md34031}
The {\ttfamily MIMEType\+Parameters} class, instances of which are returned by {\ttfamily mime\+Type.\+parameters}, has equivalent surface API to a \href{https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map}{\texttt{ Java\+Script {\ttfamily Map}}}.

However, {\ttfamily MIMEType\+Parameters} methods will always interpret their arguments as appropriate for MIME types, so e.\+g. parameter names will be lowercased, and attempting to set invalid characters will throw.

Some examples\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{const mimeType = new MIMEType(`x/x;a=b;c=D;E="{}F"{}`);}
\DoxyCodeLine{}
\DoxyCodeLine{// Logs:}
\DoxyCodeLine{// a b}
\DoxyCodeLine{// c D}
\DoxyCodeLine{// e F}
\DoxyCodeLine{for (const [name, value] of mimeType.parameters) \{}
\DoxyCodeLine{  console.log(name, value);}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{console.assert(mimeType.parameters.has("{}a"{}));}
\DoxyCodeLine{console.assert(mimeType.parameters.has("{}A"{}));}
\DoxyCodeLine{console.assert(mimeType.parameters.get("{}A"{}) === "{}b"{});}
\DoxyCodeLine{}
\DoxyCodeLine{mimeType.parameters.set("{}Q"{}, "{}X"{});}
\DoxyCodeLine{console.assert(mimeType.parameters.get("{}q"{}) === "{}X"{});}
\DoxyCodeLine{console.assert(mimeType.toString() === "{}x/x;a=b;c=d;e=F;q=X"{});}
\DoxyCodeLine{}
\DoxyCodeLine{// Throws:}
\DoxyCodeLine{mimeType.parameters.set("{}@"{}, "{}x"{}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_whatwg_mimetype_README_autotoc_md34032}{}\doxysection{Raw parsing/serialization APIs}\label{md_sfcode_node_modules_whatwg_mimetype_README_autotoc_md34032}
If you want primitives on which to build your own API, you can get direct access to the parsing and serialization algorithms as follows\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{const parse = require("{}whatwg-\/mimetype/parser"{});}
\DoxyCodeLine{const serialize = require("{}whatwg-\/mimetype/serialize"{}); }

\end{DoxyCode}


{\ttfamily parse(string)} returns an object containing the {\ttfamily type} and {\ttfamily subtype} strings, plus {\ttfamily parameters}, which is a {\ttfamily Map}. This is roughly our equivalent of the spec\textquotesingle{}s \href{https://mimesniff.spec.whatwg.org/\#mime-type}{\texttt{ MIME type record}}. If parsing fails, it instead returns {\ttfamily null}.

{\ttfamily serialize(record)} operates on the such an object, giving back a string according to the serialization algorithm. 