UTF-8 Text Encoding Polyfill

1.0.2 · abandoned · verified Tue Apr 21

text-encoding-utf-8 is a partial polyfill for the Web's Encoding Living Standard API, specifically designed to provide UTF-8 encoding and decoding capabilities for environments lacking native `TextEncoder` and `TextDecoder` implementations, such as older versions of Safari on iOS. The current stable version is 1.0.2, last updated over seven years ago. Its key differentiator is its strict focus on UTF-8 only, resulting in a smaller footprint compared to full polyfills like `text-encoding`. While it served a critical role historically, modern browsers and Node.js environments universally support `TextEncoder` and `TextDecoder` natively, rendering this polyfill largely obsolete for new development.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic UTF-8 encoding and decoding using TextEncoder and TextDecoder, including a conceptual example of streaming decode.

import { TextEncoder, TextDecoder } from 'text-encoding-utf-8';

// Basic UTF-8 Encoding
const encoder = new TextEncoder('utf-8');
const originalString = 'Hello, world! 👋';
const encodedBytes = encoder.encode(originalString);

console.log('Original String:', originalString);
console.log('Encoded Bytes (Uint8Array):', encodedBytes);

// Basic UTF-8 Decoding
const decoder = new TextDecoder('utf-8');
const decodedString = decoder.decode(encodedBytes);

console.log('Decoded String:', decodedString);

// Streaming Decode (example concept - next_chunk needs implementation)
let streamDecoder = new TextDecoder('utf-8');
let resultString = '';

// Imagine 'chunk1', 'chunk2', etc., are Uint8Arrays from a stream
const chunk1 = encoder.encode('This is the first part. ');
const chunk2 = encoder.encode('And this is the second part.');

resultString += streamDecoder.decode(chunk1, { stream: true });
resultString += streamDecoder.decode(chunk2, { stream: true });
resultString += streamDecoder.decode(); // Finish the stream

console.log('Stream Decoded String:', resultString);

view raw JSON →