# utf-8

> Encode/decode UTF8.

Latest version **3.0.0** (published 2024-08-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install utf-8
pnpm add utf-8
yarn add utf-8
bun add utf-8
```

## 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 | 3.0.0 |
| Published | 2024-08-11 |
| First published | 2013-09-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=20.11.1 |
| Dependencies | 1 |
| Unpacked size | 65.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 19 |
| Author | Nicolas Froidure |
| Maintainers | nfroidure, cipherboy |
| Keywords | utf-8, encode, decode, encoder, decoder, strict |

## Links

- npm: https://www.npmjs.com/package/utf-8
- Repository: https://github.com/nfroidure/utf-8
- Homepage: https://github.com/nfroidure/utf-8#readme
- Issues: https://github.com/nfroidure/utf-8/issues
- npm.io page: https://npm.io/package/utf-8

## Dependencies (1)

- [yerror](https://npm.io/package/yerror.md) ^8.0.0

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M 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
- [datasets-fi-postalcodes](https://npm.io/package/datasets-fi-postalcodes.md) — 1.1K weekly downloads

## Recent versions

- 3.0.0 (latest) — 2024-08-11
- 2.0.0 — 2018-04-29
- 1.0.0 — 2015-06-25
- 0.1.2 — 2014-09-25
- 0.1.1 — 2014-05-09
- 0.1.0 — 2013-09-30

## README

[//]: # ( )
[//]: # (This file is automatically generated by a `metapak`)
[//]: # (module. Do not change it  except between the)
[//]: # (`content:start/end` flags, your changes would)
[//]: # (be overridden.)
[//]: # ( )
# utf-8
> Encode/decode UTF8.

[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/nfroidure/utf-8/blob/main/LICENSE)


[//]: # (::contents:start)

As you probably noticed, there is already standards ways to
 encode/decode utf-8 strings into buffers in the NodeJS
 standard library.

This library can be useful if you need to write at given buffer
 indexes or to validate utf-8 encoded buffers.

Otherwise, use NodeJS standard library.

## Usage

```sh
npm install utf-8
```

### Encoding

A char:

```js
import * as UTF8 from 'utf-8';

UTF8.setBytesFromCharCode('é'.charCodeAt(0));
// [0xC3, 0xA9]
```

A string:

```js
UTF8.setBytesFromString('1.3$ ~= 1€');
// [49, 46, 51, 36, 32, 126, 61, 32, 49, 226, 130, 172]
```

### Decoding

A char:

```js
String.fromCharCode(UTF8.getCharCode([0xc3, 0xa9]));
// 'é'
```

A string:

```js
UTF8.getStringFromBytes([49, 46, 51, 36, 32, 126, 61, 32, 49, 226, 130, 172]);
// '1.3$ ~= 1€'
```

### TypedArrays are welcome

As inputs:

```js
const bytes = new Uint8Array([
  0xc3, 0xa9, 49, 46, 51, 36, 32, 126, 61, 32, 49, 226, 130, 172,
]);

// The first char
String.fromCharCode(UTF8.getCharCode(bytes));
// é

// The following string at the offset 2
UTF8.getStringFromBytes(bytes, 2);
// '1.3$ ~= 1€'
```

As well as outputs :

```js
const bytes = new Uint8Array(14);

// First encoding a char
UTF8.setBytesFromCharCode('é'.charCodeAt(0));

// Then encoding a string
UTF8.setBytesFromString('1.3$ ~= 1€', 2);
```

## UTF8 encoding detection

```js
UTF8.isNotUTF8(bytes);
// true | false
```

This function can prove the text contained by the given bytes is not UTF-8 (or
badly encoded UTF-8 string). It's not reciprocally true, especially for short
strings with which false positives are frequent.

## Strict mode

If you try to encode an UTF8 string in an ArrayBuffer too short to contain the
complete string, it will silently fail. To avoid this behavior, use the strict
mode :

```js
UTF8.setBytesFromString('1.3$ ~= 1€', 2, null, true);
```

# Thanks

- The Debian project for it's free (as freedom) russian/japanese man pages used
  for real world files tests!

[//]: # (::contents:end)

# Authors
- [Nicolas Froidure](https://insertafter.com/en/index.html)

# License
[MIT](https://github.com/nfroidure/utf-8/blob/main/LICENSE)

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