@i-xi-dev/mimetype
Version:
A JavaScript MIME type parser and serializer, implements the MIME type defined in WHATWG MIME Sniffing Standard.
102 lines (72 loc) • 2.84 kB
Markdown
A JavaScript MIME type parser and serializer, implements [the MIME type defined in WHATWG MIME Sniffing Standard](https://mimesniff.spec.whatwg.org/#understanding-mime-types).
| Chrome | Edge | Firefox | Safari | Deno | Node.js |
| :---: | :---: | :---: | :---: | :---: | :---: |
| ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
```console
$ npm i @i-xi-dev/mimetype@1.2.24
```
```javascript
import { MediaType } from "@i-xi-dev/mimetype";
```
Example for Skypack
```javascript
import { MediaType } from "https://cdn.skypack.dev/@i-xi-dev/mimetype@1.2.24";
```
Parse a MIME type string
```javascript
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
```javascript
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
```javascript
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
```javascript
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"'
```