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.
Symbol
TextEncoder
class TextEncoder
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.
UTF-8 encodes the
src
string to thedest
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 srcThe text to encode.
@param destThe array to hold the encode result.