bare-encoding
WHATWG text encoding interfaces for JavaScript
bare-encoding — WHATWG text encoding interfaces for JavaScript.
npm i bare-encodingUsage
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
| Parameter | Type | Default | Description |
|---|---|---|---|
input | string | — | The string to encode. |
encodeInto
encodeInto(input: string, destination: ArrayBufferView | ArrayBuffer | SharedArrayBuffer): { read: number; written: number }Encode input as UTF-8 into destination. Returns the number of UTF-16 units of input read and the number of bytes written.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
input | string | — | The string to encode. |
destination | ArrayBufferView | ArrayBuffer | SharedArrayBuffer | — | The 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
| Parameter | Type | Default | Description |
|---|---|---|---|
label? | string | — | The encoding label; only 'utf-8' and its aliases are accepted (default 'utf-8'). |
Throws
INVALID_LABEL—labelis not'utf-8'or one of its aliases.
decode
decode(input: ArrayBufferView | ArrayBuffer | SharedArrayBuffer, options?: { stream: boolean }): stringDecode 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
| Parameter | Type | Default | Description |
|---|---|---|---|
input | ArrayBufferView | ArrayBuffer | SharedArrayBuffer | — | The 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.TextEncoderThe type of the TextEncoder class, used to type the global TextEncoder constructor.
TextDecoderConstructor
type TextDecoderConstructor = typeof encoding.TextDecoderThe 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
| Parameter | Type | Default | Description |
|---|---|---|---|
msg | string | — | The error message. |
Returns EncodingError — An EncodingError with code set to 'INVALID_LABEL', for the caller to throw.
See also
- Bare modules — the full
bare-*catalog. - Bare runtime API — the runtime these modules extend.