Skip to content

Replacements for utf8

Modern Node and browsers provide native UTF-8 APIs, so this dependency is rarely needed.

TextEncoder/TextDecoder (built-in)

The built-in TextEncoder and TextDecoder APIs provide a native way to handle UTF-8 encoding and decoding.

ts
const text = '€'
const encoder = new TextEncoder()
const utf8Bytes = encoder.encode(text) // Uint8Array of UTF-8 bytes

// and to decode:

const decoder = new TextDecoder('utf-8', { fatal: true })
const decodedText = decoder.decode(utf8Bytes) // "€"

Buffer (Node.js)

Node's built-in Buffer provides both Buffer.from(str, 'utf8') and buf.toString('utf8') methods for UTF-8 encoding and decoding.

ts
const text = '€'
const utf8Buffer = Buffer.from(text, 'utf8') // Buffer of UTF-8 bytes

Released under the MIT License. (9bdffbbe)