TextEncoder

Bun

Symbol

TextEncoder

class TextEncoder

TextEncoder takes a stream of code points as input and emits a stream of bytes. For a more scalable, non-native library, see StringView – a C-like representation of strings based on typed arrays.

MDN Reference

  • readonly encoding: string
  • encode(input?: string): Uint8Array
  • encodeInto(source: string, destination: Uint8Array): TextEncoderEncodeIntoResult

    Runs the UTF-8 encoder on source, stores the result of that operation into destination, and returns the progress made as an object wherein read is the number of converted code units of source and written is the number of bytes modified in destination.

    MDN Reference

    encodeInto(src?: string, dest?: BufferSource): EncodeIntoResult

    UTF-8 encodes the src string to the dest Uint8Array and returns an object containing the read Unicode code units and written UTF-8 bytes.

    const encoder = new TextEncoder();
    const src = 'this is some data';
    const dest = new Uint8Array(10);
    const { read, written } = encoder.encodeInto(src, dest);
    
    @param src

    The text to encode.

    @param dest

    The array to hold the encode result.