{ } qjs-modules

textcode

Source: quickjs-textcode.c — module exports TextDecoder and TextEncoder

WHATWG-style text encoding/decoding, extended with endianness control and incremental (buffered) operation.

TextDecoder

WHATWG TextDecoder, the same as the global one in browsers, node and bun, plus utf-32 and two extra getters.

new TextDecoder(label = 'utf-8', { fatal = false, ignoreBOM = false })   // length 1
dec.decode(input, { stream = false })
MemberArgsKindDescription
decode(input, options)1methodDecodes an ArrayBuffer, SharedArrayBuffer, typed array or DataView (none for undefined). Each maximal invalid subpart becomes U+FFFD. With stream: true an incomplete sequence at the end is held for the next call.
encoding—getterThe WHATWG name: 'utf-8', 'utf-16le', 'windows-1252', ...
fatal—getterWhether invalid input throws instead of giving U+FFFD.
ignoreBOM—getterWhether a leading byte order mark is kept.
endian—getterExtension: true for a big-endian encoding.
buffered—getterExtension: bytes held back by stream: true.

Encodings and labels follow the WHATWG list, labels match case-insensitively with surrounding whitespace ignored:

EncodingExample labels
utf-8utf8, unicode-1-1-utf-8
utf-16le, utf-16beutf-16, ucs-2, unicode / utf-16be
windows-1252latin1, iso-8859-1, ascii, us-ascii
windows-1250 ... windows-1258, windows-874cp1250, iso-8859-9 (windows-1254), tis-620 (windows-874)
iso-8859-2 ... iso-8859-16, iso-8859-8-ilatin2, cyrillic, greek, hebrew, l9
utf-32le, utf-32beextension: utf-32, utf-32le, utf-32be

Not supported: ibm866, koi8-r, koi8-u, macintosh, x-mac-cyrillic, the CJK encodings, replacement and x-user-defined.

Errors carry Node's code:

ErrorcodeWhen
RangeErrorERR_ENCODING_NOT_SUPPORTEDunknown label
TypeErrorERR_ENCODING_INVALID_ENCODED_DATAinvalid input with fatal: true
TypeErrorERR_INVALID_ARG_TYPEinput is not bytes, options is not an object

TextEncoder

new TextEncoder([encoding])   // length 1
MemberArgsKindDescription
encode(input)1methodEncodes a string into a Uint8Array.
encodeInto(source, dest)2methodEncodes source into the existing dest typed array; returns { read, written }, read in UTF-16 units as WHATWG.
encoding—getter'utf-8', or 'utf-16le'/'utf-16be'/'utf-32le'/'utf-32be' for the extension encodings (enumerable).
endian—getterByte order used for multi-byte encodings.
buffered—getterBytes pending across incremental encode calls.

Internal helpers

Module exports that lib/ builds Node APIs on; not standard APIs themselves.

FunctionArgsDescription
isAscii(view) / isUtf8(view)1Whether the bytes are 7-bit ASCII / valid UTF-8 (no overlong form, no surrogate); buffer.isAscii()/isUtf8().
utf8Offsets(bytes, indices)2Converts a Uint32Array of UTF-16 indices into the text bytes (UTF-8) encodes to byte offsets, in place, one pass when ascending.
encode(string, encoding)2'utf-16le', 'latin1' or 'ascii' bytes of a string as an ArrayBuffer, lone surrogates kept (Node's Buffer.from).
decode(view, encoding)2'latin1' (byte = code point) or 'ascii' (byte & 0x7f) bytes as a string (Node's toString).

Export list

Generated by utilities/doc-api.js from the textcode dump of utilities/api-surface.sh: 22 members. Edit the text above this block, not the block.

Functions: decode(), encode(), isAscii(), isUtf8(), utf8Offsets()

TextDecoder

class, constructible

  • methods: decode()
  • properties: buffered (get), encoding (get), endian (get), fatal (get), ignoreBOM (get), [Symbol.toStringTag]

TextEncoder

class, constructible

  • methods: encode(), encodeInto()
  • properties: buffered (get), encoding (get), endian (get), [Symbol.toStringTag]