MIMEType class implementation
npm install @xstd/mime-type
!npm
!NPM
!coverage
!npm type definitions

This library provides a MIMEType class to manage MIME types, somehow like the URL class.
Example:
``ts
import { MIMEType } from '@xstd/mime-type';
function isUtf8EncodedText(
file: Blob,
): boolean {
const mimeType = new MIMEType(file.type);
return mimeType.type === 'text' && mimeType.parameters.get('encoding') === 'utf-8';
}
console.log(isUtf8EncodedText(new Blob(['abc'], { type: 'text/plain; encoding=utf-8' }))); // logs true`
`shell`
yarn add @xstd/mime-typeor
npm install @xstd/mime-type --save
`tstrue
/**
* Represents a MIME type.
*/
declare class MIMEType {
/**
* Returns if input can be parsed into a valid MIME type.MIMEType
*/
static canParse(input: string): boolean;
/**
* Returns a if input can be parsed a into valid MIME type, else it returns null.input
*/
static parse(input: string): MIMEType | null;
/**
* Constructs a new MIMEType from an input string.
*
* Throws if the is not a valid MIME type.
*
* @example
*
* const mimeType = new MIMEType('text/plain; encoding=utf-8');
*/
constructor(input: string);
/ IMMUTABLE /
/**
* Throws if this object is immutable.
*/
throwIfImmutable(): void;
/**
* Returns true if this object is immutable.
*/
get immutable(): boolean;
/**
* Makes this object immutable.
*/
makeImmutable(): this;
/ PROPERTIES /
/**
* Returns the type of this MIMEType.
*/
get type(): string;
/**
* Sets the type of this MIMEType.
*
* Throws if the input is not a valid type.input
*/
set type(input: string);
/**
* Returns the subtype of this MIMEType.
*/
get subtype(): string;
/**
* Sets the subtype of this MIMEType.
*
* Throws if the is not a valid subtype.
*/
set subtype(input: string);
/**
* Returns the type and subtype of this MIMEType.
*/
get typeAndSubtype(): string;
/**
* Sets the type and subtype of this MIMEType.
*
* Throws if the input is not a valid MIMEType.`
*/
set typeAndSubtype(input: string);
/**
* Returns the parameters of this MIMEType.
*/
get parameters(): MIMETypeParameters;
/**
* Returns a MIME type string.
*/
toString(): string;
}
`ts
/ TYPES /
type MIMETypeParameterTuple = readonly [key: string, value: string];
type MIMETypeParametersInit = Iterable
interface MIMETypeParametersFromOptions {
readonly mode?: 'replace' | 'append';
}
interface MIMETypeParametersToStringOptions {
readonly includeLeadingSeparator?: boolean;
}
/ CLASS /
/**
* Represents a list of parameters of a MIME type.
*/
declare class MIMETypeParameters {
/**
* Returns true if input can be parsed into valid MIME type parameters.MIMETypeParameters
*/
static canParse(input: string): boolean;
/**
* Returns a if input can be parsed into valid parameters, else it returns null.input
*/
static parse(input: string): MIMETypeParameters | null;
/**
* Constructs a new MIMETypeParameters from an input string, an Iterable of key/value, or an object of key/value.
*
* Throws if the is invalid.input
*
* > If the is a string, the leading separator ; bay be omitted.
*
* @example
*
* const parameters = new MIMETypeParameters('; encoding=utf-8');
*/
constructor(init?: MIMETypeParametersInit);
/ IMMUTABLE /
/**
* Throws if this object is immutable.
*/
throwIfImmutable(): void;
/**
* Returns true if this object is immutable.
*/
get immutable(): boolean;
/**
* Makes this object immutable.
*/
makeImmutable(): this;
/ MAP LIKE /
/**
* Returns the number of parameters.
*/
get size(): number;
/**
* Appends a specified key/value pair as a new parameter.
*/
append(key: string, value: string): this;
/**
* Deletes specified parameters and their associated value(s) from the list of all parameters.
*
* @returns The number of deleted parameters.
*/
delete(key: string, value?: string): number;
/**
* Returns the first value associated with the given parameter.
*
* Throws if the parameter doesn't exist.
*/
get(key: string): string;
/**
* Returns all the values associated with a given parameter as an array.
*/
getAll(key: string): string[];
/**
* Returns the first value associated with the given parameter, or undefined if there is none.Iterator
*/
getOptional(key: string): string | undefined;
/**
* Returns a boolean value that indicates whether the specified parameter is in the parameters.
*/
has(key: string, value?: string): boolean;
/**
* Sets the value associated with a given parameter to the given value.
* If there were several matching values, this method deletes the others.
* If the parameter doesn't exist, this method creates it.
*/
set(key: string, value: string): this;
/**
* Removes all the parameters.
*/
clear(): void;
/**
* Sorts all key/value pairs contained in this object in place.
* The sort order is according to unicode code points of the keys.
* This method uses a stable sorting algorithm (i.e. the relative order between key/value pairs with equal keys will be preserved).
*/
sort(): this;
/**
* Returns an allowing iteration through all keys contained in this object.Iterator
* The keys are strings.
*/
keys(): Generator
/**
* Returns an allowing iteration through all values contained in this object.Iterator
*/
values(): Generator
/**
* Returns an allowing iteration through all key/value pairs contained in this object..entries()
* The iterator returns key/value pairs in the same order as they appear in the parameters string.
* The key and value of each pair are strings.
*/
entries(): Generator
/**
* Alias of .``
*
* @see MIMETypeParameters.entries
*/
[Symbol.iterator](): IterableIterator
/**
* Allows iteration through all values contained in this object via a callback function.
*/
forEach(callback: (value: string, key: string, parameters: MIMETypeParameters) => void): void;
/**
* Returns a MIME type parameters string suitable for use in a MIME type.
*/
toString({ includeLeadingSeparator }?: MIMETypeParametersToStringOptions): string;
}