[](https://github.com/Borewit/text-codec/actions/workflows/ci.yml)
[](https://www.npmjs.com/package/@borewit/text-codec)
[](https://npmcharts.com/compare/@borewit/text-codec?interval=30)

[](LICENSE.txt)
# `@borewit/text-codec`
A **lightweight text encoding/decoding polyfill** for JavaScript engines (like [Hermes](https://hermesengine.dev/)) which lack support for certain `TextEncoder` / `TextDecoder` encoding types.
This module implements manual encoding and decoding for the most commonly used encodings, without pulling in large dependencies or experimental features.
## β¨ Features
### Supported encodings:
- `utf-8` / `utf8`
- `utf-16le`
- `ascii`
- `latin1` / `iso-8859-1`
- `windows-1252`
---
## π¦ Installation
```sh
npm install @borewit/text-codec
```
# π API Documentation
## `textDecode(bytes, encoding): string`
Decodes binary data into a JavaScript string using the specified encoding.
**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-encode";
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`
**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-encode";
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.