Content-Type
Header Strings
Parse This package will parse the Content-Type
header field into an introspectable data structure, whose parameters can be manipulated:
const contentTypeParser = ; const contentType = ; console; console;console;console; contentType;console;console; console;console;console;
Note how parsing will lowercase the type, subtype, and parameter name tokens (but not parameter values).
If the passed string cannot be parsed as a content-type, contentTypeParser
will return null
.
ContentType
instance API
This package's main module's default export will return an instance of the ContentType
class, which has the following public APIs:
Properties
type
: the top-level media type, e.g."text"
subtype
: the subtype, e.g."html"
parameterList
: an array of{ separator, key, value }
pairs representing the parameters. Theseparator
field contains any whitespace, not just the;
character.
Parameter manipulation
In general you should not directly manipulate parameterList
. Instead, use the following APIs:
get("key")
: returns the value of the parameter with the given key, orundefined
if no such parameter is presentset("key", "value")
: adds the given key/value pair to the parameter list, or overwrites the existing value if an entry already existed
Both of these will lowercase the keys.
MIME type tests
isHTML()
: returns true if this instance's MIME type is the HTML MIME type,"text/html"
isXML()
: returns true if this instance's MIME type is an XML MIME typeisText()
: returns true if this instance's top-level media type is"text"
Serialization
toString()
will return a canonicalized representation of the content-type, re-built from the parsed components
Credits
This package was originally based on the excellent work of @nicolashenry, in jsdom. It has since been pulled out into this separate package.