# @borewit/text-codec

> Text Decoder

Latest version **0.2.2** (published 2026-03-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install @borewit/text-codec
pnpm add @borewit/text-codec
yarn add @borewit/text-codec
bun add @borewit/text-codec
```

## Health

**Score 65/100 (B)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.2.2 |
| Published | 2026-03-11 |
| First published | 2025-08-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 0 |
| Unpacked size | 17.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Borewit |
| Maintainers | borewit |
| Keywords | TextDecoder, TextEncoder, decoder, decoding, encod, encoding, decode, text, ascii, utf-8, utf8, utf-16le, latin1, iso-8859-1, windows-1252, charset, encoding, decoding, polyfill, character-set, latin, hermes, react |

## Links

- npm: https://www.npmjs.com/package/@borewit/text-codec
- Repository: https://github.com/Borewit/text-codec
- Homepage: https://github.com/Borewit/text-codec#readme
- Issues: https://github.com/Borewit/text-codec/issues
- Funding: https://github.com/sponsors/Borewit
- npm.io page: https://npm.io/package/@borewit/text-codec

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [jwt-simple](https://npm.io/package/jwt-simple.md) — 259.5K weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads

## Recent versions

- 0.2.2 (latest) — 2026-03-11
- 0.2.1 — 2025-12-29
- 0.2.0 — 2025-08-14
- 0.1.1 — 2025-08-13
- 0.1.0 — 2025-08-13

## README

[![CI](https://github.com/Borewit/text-codec/actions/workflows/ci.yml/badge.svg)](https://github.com/Borewit/text-codec/actions/workflows/ci.yml)
[![npm version](https://img.shields.io/npm/v/%40borewit%2Ftext-codec.svg)](https://www.npmjs.com/package/@borewit/text-codec)
[![npm downloads](http://img.shields.io/npm/dm/@borewit/text-codec.svg)](https://npmcharts.com/compare/@borewit/text-codec?interval=30)
![bundlejs](https://deno.bundlejs.com/?q=@borewit/text-codec&badge)
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg?logo=open-source-initiative&logoColor=white)](LICENSE.txt)

# `@borewit/text-codec`

A **lightweight polyfill for text encoders and decoders** covering a small set of commonly used encodings.

Some JavaScript runtimes provide limited or inconsistent encoding support through `TextEncoder` and `TextDecoder`.  
Examples include environments like **Hermes (React Native)** or certain **Node.js builds with limited ICU support**.

This module provides **reliable encode/decode support for a small set of encodings that may be missing or unreliable in those environments**.

- If a native UTF-8 `TextEncoder` / `TextDecoder` is available, it is used.
- All other encodings are implemented by this library.

## Supported encodings

- `utf-8` / `utf8`
- `utf-16le`
- `ascii`
- `latin1` / `iso-8859-1`
- `windows-1252`

These encodings are commonly encountered in metadata formats and legacy text data.

## ✨ Features

- Encoding and decoding utilities
- Lightweight
- Typed API

## 📦 Installation

```sh
npm install @borewit/text-codec
```

# 📚 API Documentation

## `textDecode(bytes, encoding): string`

Decodes binary data into a JavaScript string.

**Parameters**
- `bytes` (`Uint8Array`) — The binary data to decode.
- `encoding` (`SupportedEncoding`, optional) — Encoding type. Defaults to `"utf-8"`.  

**Returns**
- `string` — The decoded text.

**Example**
```js
import { textDecode } from "@borewit/text-codec";

const bytes = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f]);
const text = textDecode(bytes, "ascii");
console.log(text); // "Hello"
```

## `textEncode(input, encoding): Uint8Array`

Encodes a JavaScript string into binary form using the specified encoding.

**Parameters**

- `input` (`string`) — The string to encode.
- `encoding` (`SupportedEncoding`, optional) — Encoding type. Defaults to `"utf-8"`.

**Returns**

`Uint8Array` — The encoded binary data.

Example:
```js
import { textEncode } from "@borewit/text-codec";

const bytes = textEncode("Hello", "utf-16le");
console.log(bytes); // Uint8Array([...])
```

## 📜 Licence

This project is licensed under the [MIT License](LICENSE.txt). Feel free to use, modify, and distribute as needed.

---
_Source: https://npm.io/package/@borewit/text-codec · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
