LogoPear Docs

bare-encoding

WHATWG text encoding interfaces for JavaScript

stable

bare-encoding — WHATWG text encoding interfaces for JavaScript.

npm i bare-encoding

Usage

const { TextEncoder, TextDecoder } = require('bare-encoding')

API

TextEncoder

encode(input: string): Buffer

Source

Encode input as UTF-8 and return the result as a Buffer.

Parameters

ParameterTypeDefaultDescription
inputstringThe string to encode.

encodeInto

encodeInto(input: string, destination: ArrayBufferView | ArrayBuffer | SharedArrayBuffer): { read: number; written: number }
Source

Encode input as UTF-8 into destination. Returns the number of UTF-16 units of input read and the number of bytes written.

Parameters

ParameterTypeDefaultDescription
inputstringThe string to encode.
destinationArrayBufferView | ArrayBuffer | SharedArrayBufferThe buffer to write the UTF-8 bytes into.

TextEncoder.encoding: 'utf-8'

Source

Always 'utf-8'.

TextDecoder

new TextDecoder(label?: string)

Source

Create a TextDecoder for label. Only 'utf-8' (and its aliases) is supported; any other label throws.

Parameters

ParameterTypeDefaultDescription
label?stringThe encoding label; only 'utf-8' and its aliases are accepted (default 'utf-8').

Throws

  • INVALID_LABELlabel is not 'utf-8' or one of its aliases.

decode

decode(input: ArrayBufferView | ArrayBuffer | SharedArrayBuffer, options?: { stream: boolean }): string
Source

Decode input to a string. If options.stream is true, buffers any incomplete trailing sequence for the next call instead of including it in the result.

Parameters

ParameterTypeDefaultDescription
inputArrayBufferView | ArrayBuffer | SharedArrayBufferThe bytes to decode.
options?{ stream: boolean }Options; set stream: true when input is a chunk of a larger stream.

TextDecoder.encoding: 'utf-8'

Source

The encoding this decoder was constructed with, always 'utf-8'.

bare-encoding/global

Types

TextEncoderConstructor

type TextEncoderConstructor = typeof encoding.TextEncoder
Source

The type of the TextEncoder class, used to type the global TextEncoder constructor.

TextDecoderConstructor

type TextDecoderConstructor = typeof encoding.TextDecoder
Source

The type of the TextDecoder class, used to type the global TextDecoder constructor.

bare-encoding/errors

EncodingError

code: string

Source

The error code identifying the failure.

EncodingError.INVALID_LABEL(msg: string): EncodingError

Source

The label passed to TextDecoder is not a valid encoding.

Parameters

ParameterTypeDefaultDescription
msgstringThe error message.

Returns EncodingError — An EncodingError with code set to 'INVALID_LABEL', for the caller to throw.

See also

On this page