@i-xi-dev/mimetype
v1.2.24
Published
A JavaScript MIME type parser and serializer, implements the MIME type defined in WHATWG MIME Sniffing Standard.
Downloads
189
Maintainers
Readme
@i-xi-dev/mimetype
A JavaScript MIME type parser and serializer, implements the MIME type defined in WHATWG MIME Sniffing Standard.
Requirement
| Chrome | Edge | Firefox | Safari | Deno | Node.js | | :---: | :---: | :---: | :---: | :---: | :---: | | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
Installation
npm
$ npm i @i-xi-dev/[email protected]
import { MediaType } from "@i-xi-dev/mimetype";
CDN
Example for Skypack
import { MediaType } from "https://cdn.skypack.dev/@i-xi-dev/[email protected]";
Usage
MediaType
class
Parse a MIME type string
const mediaType = MediaType.fromString('application/soap+xml; charset=utf-8;action="https://example.com/example"');
mediaType.type;
// → "application"
mediaType.subtype;
// → "soap+xml"
mediaType.essence;
// → "application/soap+xml"
[ ...mediaType.parameterNames() ];
// → [ "charset", "action" ]
[ ...mediaType.parameters() ];
// → [ ["charset", "utf-8"], ["action", "https://example.com/example"] ]
mediaType.hasParameter("charset");
// → true
mediaType.getParameterValue("action");
// → "https://example.com/example"
Serialize a MIME type
const mediaType = MediaType.fromString('application/soap+xml; charset=utf-8;action="https://example.com/example"');
mediaType.toString();
// → 'application/soap+xml;charset=utf-8;action="https://example.com/example"'
Equivalent comparisons
const mediaType = MediaType.fromString('application/soap+xml; charset=utf-8;action="https://example.com/example"');
const mediaType2 = MediaType.fromString('application/soap+xml; charset=utf-16;action="https://example.com/example"');
mediaType.equals(mediaType2);
// → false
const mediaType3 = MediaType.fromString('APPLICATION/SOAP+XML;ACTION="https://example.com/example";CHARSET=utf-8');
mediaType.equals(mediaType3);
// → true
const mediaType4 = MediaType.fromString('application/soap+xml; charset=UTF-8;action="https://example.com/example"');
mediaType.equals(mediaType4);
// → false
mediaType.equals(mediaType4, { caseInsensitiveParameters: ["charset"] });
// → true
Instance is immutable
const mediaType = MediaType.fromString('application/soap+xml; charset=utf-8;action="https://example.com/example"');
const mediaType2 = mediaType.withParameters([ ["charset": "UTF-16"] ]);
mediaType2.toString();
// → 'application/soap+xml;charset=UTF-16'
const mediaType3 = mediaType.withoutParameters();
mediaType3.toString();
// → 'application/soap+xml'
mediaType.toString();
// → 'application/soap+xml;charset=utf-8;action="https://example.com/example"'