@nberlette/utf8
Version:
Blazing fast universal ponyfills for TextEncoder and TextDecoder.
72 lines (71 loc) • 2.25 kB
TypeScript
/**
* A BufferSource is a source of bytes, such as an ArrayBuffer, TypedArray,
* SharedArrayBuffer, or DataView.
*
* @category Types
*/
export type BufferSource = ArrayBufferLike | ArrayBufferView;
/**
* Options for the {@linkcode TextDecoder} constructor.
*
* @category Types
*/
export interface TextDecoderOptions {
/**
* If true, invalid bytes will throw a TypeError. Otherwise, they will be
* replaced with the Unicode replacement character.
* @default {false}
*/
fatal?: boolean;
/**
* If true, the BOM (Byte Order Mark) will be ignored.
* @default {false}
*/
ignoreBOM?: boolean;
}
/**
* Options for the {@linkcode TextDecoder.decode} method.
*
* @category Types
*/
export interface TextDecodeOptions {
/**
* If true, indicates that the data being decoded is part of a larger stream.
* This allows the decoder to handle incomplete byte sequences appropriately.
* @default {false}
*/
stream?: boolean;
}
/**
* Decodes an encoded sequence of bytes into a string, using the specified
* encoding standard. Currently, only UTF-8 encoding is supported.
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder
* @category Encoding
* @tags utf-8, decoder
*/
export declare class TextDecoder {
#private;
/**
* Creates a new TextDecoder instance.
* @param label The encoding to use. Currently, only "utf-8" is supported.
* @param options Configuration options.
*/
constructor(label?: string, options?: TextDecoderOptions);
/** The encoding standard to use. */
get encoding(): string;
/** If true, invalid bytes will throw a TypeError. */
get fatal(): boolean;
/** If true, the BOM (Byte Order Mark) will be ignored. */
get ignoreBOM(): boolean;
/**
* Decodes a BufferSource into a string using UTF-8 decoding.
*
* @param input The bytes to decode. Defaults to an empty Uint8Array.
* @param [options] Decoding options.
* @returns The decoded string.
* @throws if the input is not a BufferSource.
* @throws if fatal is true and an invalid byte sequence is encountered.
*/
decode(input?: BufferSource, options?: TextDecodeOptions): string;
}