# utf8-codec

> utf8 to/from bytes codec (esm/cjs)

Latest version **1.0.0** (published 2022-05-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install utf8-codec
pnpm add utf8-codec
yarn add utf8-codec
bun add utf8-codec
```

## Health

**Score 30/100 (F)** — status: abandoned.

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-05-30 |
| First published | 2022-05-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 14.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Martin Heidegger |
| Maintainers | leichtgewicht |
| Keywords | utf8, codec, bytes, encoding, buffer, uint8array |

## Links

- npm: https://www.npmjs.com/package/utf8-codec
- Repository: https://github.com/martinheidegger/utf8-codec
- Homepage: https://github.com/martinheidegger/utf8-codec#readme
- Issues: https://github.com/martinheidegger/utf8-codec/issues
- npm.io page: https://npm.io/package/utf8-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

- 1.0.0 (latest) — 2022-05-30

## README

# utf8-codec

A javascript-only (esm/cjs) utf8 codec that is [abstract-encoding][] compatible,
well-tested and pretty efficient. Bonus: It doesn't use Nodejs' Buffer object
and comes with typescript types.

[abstract-encoding]: https://github.com/mafintosh/abstract-encoding

## Usage

```js
import { encode, encodingLength, decode } from 'utf8-codec' // require works too!

const str = 'Hello World / こんにちは世界'
const bytes = encode(
  str,
  new Uint8Array(endcodingLength(str)), // own buffer supplied, optional
  0 // offset, at which to write the str, optional
)
str === decode(bytes, 0, bytes.length)
```

## Why?

The [TextEncoder][] and [TextDecoder][] classes _exist_ to encode utf8 strings but
they are not optimized for bigger byte processing and don't offer APIs to figure
out how preemptively how many bytes are supposed to be written/read. Surprisingly
this algorithm is even faster.

The other implementations found at the time do either not implement the edge cases
properly and/or both directions of the codec.

[TextEncoder]: https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder
[TextDecoder]: https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder

## License

[MIT](./LICENSE)

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