UTF-8
byte streams as outputs. For more information, see TextEncoder. // The TextEncoder() constructor does not have any parameters.const encoder = new TextEncoder();
// encoder.encodingreadonly encoding: string;
UTF-8
.encoder.encode(input?: string | undefined): Uint8Array
UTF-8
byte streams as outputs.encoder.encode
parametersParameter | Type | Required | Description |
input | string | undefined | No | The text to be encoded. |
encoder.encodeInto(input: string, destination: Uint8Array): EncodeIntoResult;
UTF-8
byte streams as outputs, and writes the outputs to a destination
byte array.Parameter | Type | Required | Description |
input | string | Yes | The text to be encoded. |
destination | Yes | The object in which the encoded text is stored. |
EncodeIntoResult
Parameter | Type | Description |
read | number | The number of UTF-16 units that have been converted to UTF-8. |
written | number |
const decoder = new TextDecoder(label?: string | undefined, options?: DecoderOptions | undefined): TextEncoder;
Parameter | Type | Required | Description |
label | string | undefined | No | The name of the decoding algorithm that is used by the decoder. Default value: UTF-8. For the valid values of label, see Encoding API Encodings. |
options | No | The configuration items of the decoder. |
Parameter | Type | Default value | Description |
fatal | boolean | false | Specifies whether to throw an exception when decoding fails. |
ignoreBOM | boolean | false | Specifies whether to ignore byte-order marker. |
// decoder.encodingreadonly encoding: string;
// decoder.fatalreadonly fatal: boolean;
// decoder.ignoreBOMreadonly ignoreBOM: boolean;
const result = decoder.decode(buffer?: ArrayBuffer | ArrayBufferView | undefined, options?: DecodeOptions | undefined): string;
Parameter | Type | Required | Description |
buffer | No | The byte stream to be decoded. The maximum length of buffer is 100 MB. An exception will be thrown if the maximum length is exceeded. | |
options | No | The configuration items for decoding. |
Parameter | Type | Default value | Description |
stream | boolean | false | Specifies whether to perform decoding in streaming mode. Default value: false. Valid values: true Perform decoding in streaming mode. Use this value if data is processed in chunks and additional chunks are expected. false Do not perform decoding in streaming mode. Use this value if the current chunk is the last one or data is not chunked. |
function handleEvent(event) {// Encoderconst encoder = new TextEncoder();const encodeText = encoder.encode('hello world');// Decoderconst decoder = new TextDecoder();const decodeText = decoder.decode(encodeText);// Responseconst response = new Response(JSON.stringify({encodeText: encodeText.toString(),decodeText,}));return response;}addEventListener('fetch', (event) => {event.respondWith(handleEvent(event));});
Was this page helpful?