npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

text-encoder-2

v1.0.6

Published

TextEncoder implementation with stream support for proper surrogate pair handling

Downloads

29

Readme

text-encoder-2

This is a replacement for the standard TextEncoder with fixed surrogate pairs (emojis) support.

The standard TextEncoder can't handle Unicode surrogate pairs (emojis) that are split across chunks when streaming data. This library fixes that by adding a stream option that properly buffers incomplete surrogate pairs, just like that in standard TextDecoder class.

✅ 92% test coverage with 28 comprehensive test cases covering all edge cases

Installation

npm install text-encoder-2

Usage

Basic Usage (same as standard TextEncoder)

import { TextEncoder } from 'text-encoder-2'

const encoder = new TextEncoder()
const bytes = encoder.encode('Hello 🚀')

Stream Mode (the fix)

const encoder = new TextEncoder()

// Split surrogate pair for 🚀 character
const part1 = '\uD83D'  // High surrogate
const part2 = '\uDE80'  // Low surrogate

// Without stream mode - broken
bytes1 = encoder.encode(part1) // ❌ [239, 191, 189] (replacement character)
bytes2 = encoder.encode(part2) // ❌ [239, 191, 189] (replacement character)

// With stream mode - fixed
bytes1 = encoder.encode(part1, { stream: true }) // ✅ [] (buffered)
bytes2 = encoder.encode(part2, { stream: true }) // ✅ [240, 159, 154, 128] (correct 🚀)

// End stream to flush any pending data
rest = encoder.encode('', { stream: false })
// or just
rest = encoder.encode()

API

Same as standard TextEncoder plus optional stream parameter:

class TextEncoder {
  encode(input?: string, options?: { stream?: boolean }): Uint8Array
  encodeInto(input: string, destination: Uint8Array, options?: { stream?: boolean }): { read: number, written: number }
  get encoding(): 'utf-8'
}

Why This Matters

When processing Unicode text in chunks (like reading files or network streams), surrogate pairs can be split:

Chunk 1: "Hello \uD83D"     // High surrogate at end
Chunk 2: "\uDE80 World"     // Low surrogate at start

Standard TextEncoder result:

encode(chunk1) → "Hello �"      // Broken! High surrogate becomes �
encode(chunk2) → "� World"      // Broken! Low surrogate becomes �
Final result:    "Hello � � World"

This library result:

encode(chunk1, {stream: true}) → "Hello "     // High surrogate buffered
encode(chunk2, {stream: true}) → "🚀 World"   // Combined into correct emoji
Final result:                    "Hello 🚀 World"

License

MIT