Skip to content

Borewit/text-codec

Repository files navigation

CI npm version npm downloads bundlejs License: MIT

@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

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

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:

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. Feel free to use, modify, and distribute as needed.

About

Lightweight, dependency-free TextEncoder/TextDecoder polyfill supporting UTF-8, UTF-16LE, ASCII, Latin-1, and Windows-1252 for engines like Hermes.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors