# sub-encoder

> Generate sub encodings for key/value stores

Latest version **2.1.3** (published 2023-10-10) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install sub-encoder
pnpm add sub-encoder
yarn add sub-encoder
bun add sub-encoder
```

## Health

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

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.1.3 |
| Published | 2023-10-10 |
| First published | 2022-10-12 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | separate (@types/sub-encoder) |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 24.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Andrew Osheroff |
| Maintainers | mafintosh, andrewosh |
| Keywords | kv-store, encoding, hyperbee |

## Links

- npm: https://www.npmjs.com/package/sub-encoder
- Repository: https://github.com/holepunchto/sub-encoder
- Homepage: https://github.com/holepunchto/sub-encoder#readme
- Issues: https://github.com/holepunchto/sub-encoder/issues
- npm.io page: https://npm.io/package/sub-encoder

## Dependencies (2)

- [b4a](https://npm.io/package/b4a.md) ^1.6.0
- [codecs](https://npm.io/package/codecs.md) ^3.1.0

## 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

- 2.1.3 (latest) — 2023-10-10
- 2.1.2 — 2023-10-10
- 2.1.1 — 2023-05-25
- 2.1.0 — 2023-05-24
- 2.0.0 — 2023-05-24
- 1.0.6 — 2023-05-09
- 1.0.5 — 2023-04-11
- 1.0.4 — 2023-01-20
- 1.0.3 — 2022-10-18
- 1.0.2 — 2022-10-18
- 1.0.1 — 2022-10-12
- 1.0.0 — 2022-10-12

## README

# sub-encoder
Generate sub encodings for key/value stores

### Usage
```js
const enc = new SubEncoder()
const subA = enc.sub('sub-a')
const subB = enc.sub('sub-b', 'binary') // subs support custom key encodings

await bee.put('k1', 'b')
await bee.put('a1', 'a1', { keyEncoding: subA })
await bee.put('b1', 'b1', { keyEncoding: subB })

// k1 as normal
await bee.get('k1')
// a1 from the sub
await bee.get('a1', { keyEncoding: subA })

// also supports read streams
for await (const node of bee.createReadStream({ keyEncoding: subA })) {
  // Iterates everything in the A sub
}

// The range options will be encoded properly too
for await (const node of bee.createReadStream({ lt: 'b2' }, { keyEncoding: subB })) {
}
```

### API

#### `const enc = new SubEncoder([prefix, encoding])`

Create a new SubEncoder. Optionally set the initial prefix and encoding.

`prefix` can be string or Buffer. If set, acts as an initial sub instead of starting at the Hyperbee's base level.

#### `subEnc = enc.sub(prefix, [encoding])`

Make a new sub. Returns a SubEncoder, so subs can easily be nested.

`prefix` can be string or Buffer.

#### `buf = enc.encode(key)`

Encode a key.

#### `key = enc.decode(buf)`

Decode a key.

#### `encodedRange = enc.encodeRange(range)`

Encode a range.

### License
Apache-2.0

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