TextEncoder
Encode string text into bytes, it only supports "utf-8" encoding.
See
Constructors
Constructor
new TextEncoder(): TextEncoder;
Returns
TextEncoder
Example
const encoder = new TextEncoder();
Accessors
encoding
Get Signature
get encoding(): string;
The encoding used by encoder, this always returns "utf-8".
Returns
string
Methods
encode()
encode(input): Uint8Array;
Encode string text to Uint8Array.
Parameters
| Parameter | Type | Description |
|---|---|---|
|
| Text that need encode. |
Returns
Encoded uint8 bytes array.
Example
const encodedBytes = new TextEncoder().encode("Hello, World!");
Throws
Throws TypeError if input is not a string.
encodeInto()
encodeInto(src, dest): object;
Encode string text into Uint8Array.
Parameters
| Parameter | Type | Description |
|---|---|---|
|
| Text that need encode. |
| Destination that receives the encoded uint8 bytes array. |
Returns
object
Encode result, it contains two numbers: the "read" Unicode code units from src string, and the "written" UTF-8 bytes into the dest buffer.
| Name | Type |
|---|---|
|
|
|
|
Throws
Throws TypeError if src is not a string, or dest is not a Uint8Array.