# varintes

> Unsigned Varint encoding and decoding, exposed as ESModule

Latest version **2.0.5** (published 2023-08-18) · (MIT OR Apache-2.0) license · 0 weekly downloads

## Install

```sh
npm install varintes
pnpm add varintes
yarn add varintes
bun add varintes
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.5 |
| Published | 2023-08-18 |
| First published | 2021-12-04 |
| Weekly downloads | 0 |
| License | (MIT OR Apache-2.0) |
| TypeScript types | none |
| Module format | ESM |
| Dependencies | 0 |
| Unpacked size | 9.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Maintainers | ukstv |
| Keywords | unsigned-varint, varint, fast |

## Links

- npm: https://www.npmjs.com/package/varintes
- Repository: https://github.com/ukstv/varintes
- Homepage: https://github.com/ukstv/varintes#readme
- Issues: https://github.com/ukstv/varintes/issues
- npm.io page: https://npm.io/package/varintes

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 2.0.5 (latest) — 2023-08-18
- 2.0.0-a.3 (next) — 2022-10-04
- 2.0.4 — 2022-10-30
- 2.0.3 — 2022-10-29
- 2.0.2 — 2022-10-29
- 2.0.1 — 2022-10-04
- 2.0.0 — 2022-10-04
- 2.0.0-a.2 — 2022-10-04
- 2.0.0-a.1 — 2022-10-04
- 2.0.0-a.0 — 2022-10-04
- 1.0.2 — 2022-09-13
- 1.0.1 — 2022-09-12
- 1.0.0 — 2021-12-04
- 0.0.2 — 2021-12-04
- 0.0.1 — 2021-12-04

## README

# Varintes

Encode and decode numbers per [unsigned-varint](https://github.com/multiformats/unsigned-varint) specification,
exposed as [pure ES Module](https://nodejs.org/api/esm.html).

It is **fast**: 1.5x - 10x times faster than original `varint` module.

## History

We started this package as an ES Module version of [`varint`](https://www.npmjs.com/package/varint) package.
Then we added few helpers used by existing varint package consumers.

## Installation

```shell
npm install varintes
```

## Usage

To encode a number as varint use `encode` function:

```typescript
import * as varintes from "varintes";
const bytes = varintes.encode(4242); // Uint8Array(2) [ 146, 33 ]
```

To decode varint from bytes, use `decode` function. It returns decoded number, and amount of bytes read to decode.
The bytes read number may be used to continue decoding of a long bytes sequence.

```typescript
import * as varintes from "varintes";
const [number, bytesRead] = varintes.decode(new Uint8Array([146, 33])); // number = 4242, bytesRead = 2
```

`encodingLength` gives you length in bytes of a number when encoded as varint:

```typescript
import * as varintes from "varintes";
const length = varintes.encodingLength(4242); // 2
```

Let's call tight packing of varints an encoding where varints' bytes follow each other.
There are two functions for tight packing and unpacking provided - `encodePach` and `decodePack`:

```typescript
import * as varintes from "varintes";
const packed = varintes.encodePack([1, 17, 4242]); // Uint8Array(4) [ 1, 17, 146, 33 ]
const unpacked = varintes.decodePack(packed); // [1, 17, 4242]
```

# To Do

- [x] Faster decode by loop unrolling

## License

MIT or APACHE-2.0

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