Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 21 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,43 @@

# `@borewit/text-codec`

A **lightweight alternative implementation** of `TextEncoder` / `TextDecoder` supporting common encodings missing in some JavaScript engines and Node.js builds.
Works in environments like **Hermes** (React Native) or **Small-ICU Node.js** where only UTF-8 and UTF-16LE are available.

| Encoding | Hermes — **Encode** | Hermes — **Decode** | Small-ICU Node.js — **Encode** | Small-ICU Node.js — **Decode** |
|---------------------------|---------------------|---------------------|--------------------------------|--------------------------------|
| **utf-8** / `utf8` | ➕ | Native | Native | Native |
| **utf-16le** | ➕ | ➕ | Native | Native |
| **ascii** | ➕ | ➕ | ➕ | ➕ |
| **latin1** / `iso-8859-1` | ➕ | ➕ | ➕ | Native (sometimes) |
| **windows-1252** | ➕ | ➕ | ➕ | ➕ |

**Legend:**
- **Native** — Supported natively by the JavaScript engine.
- **➕** — Support added by this module.
- **Native (sometimes)** — Available in some builds (e.g., certain Small-ICU Node.js builds).
---

When your project needs to handle encodings like `latin1` / `iso-8859-1` or `windows-1252` in these environments,
native `TextDecoder` / `TextEncoder` may throw an error or return incorrect results.
A **lightweight polyfill for text encoders and decoders** covering a small set of commonly used encodings.

## ✨ Features
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**.

- Decoding and encoding
- Lightweight
- Typed
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

### 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 using the specified encoding.
Decodes binary data into a JavaScript string.

**Parameters**
- `bytes` (`Uint8Array`) — The binary data to decode.
Expand All @@ -63,16 +53,17 @@ Decodes binary data into a JavaScript string using the specified encoding.

**Example**
```js
import { textDecode } from "@borewit/text-encode";
import { textDecode } from "@borewit/text-codec";

const bytes = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f]);
const text = textDecode(bytes, "ascii");
console.log(text); // "Hello"
```
Encodes a JavaScript string into binary form using the specified encoding.

## `textEncode(input, encoding): Uint8Array`

Encodes a JavaScript string into binary form using the specified encoding.

**Parameters**

- `input` (`string`) — The string to encode.
Expand All @@ -84,7 +75,7 @@ Encodes a JavaScript string into binary form using the specified encoding.

Example:
```js
import { textEncode } from "@borewit/text-encode";
import { textEncode } from "@borewit/text-codec";

const bytes = textEncode("Hello", "utf-16le");
console.log(bytes); // Uint8Array([...])
Expand Down
Loading
Loading