# codecs

> Create an binary encoder/decoder for json, utf-8 or custom types

Latest version **3.1.0** (published 2023-04-11) · MIT license · 0 weekly downloads

## Install

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

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.1.0 |
| Published | 2023-04-11 |
| First published | 2016-11-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/codecs) |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 7.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 29 |
| Author | Mathias Buus |
| Maintainers | mafintosh |

## Links

- npm: https://www.npmjs.com/package/codecs
- Repository: https://github.com/mafintosh/codecs
- Issues: https://github.com/mafintosh/codecs/issues
- npm.io page: https://npm.io/package/codecs

## Dependencies (1)

- [b4a](https://npm.io/package/b4a.md) ^1.6.3

## Recent versions

- 3.1.0 (latest) — 2023-04-11
- 3.0.0 — 2021-11-22
- 2.2.0 — 2020-11-26
- 2.1.0 — 2020-05-01
- 2.0.0 — 2019-03-15
- 1.2.1 — 2018-03-11
- 1.2.0 — 2016-11-23
- 1.1.0 — 2016-11-22
- 1.0.0 — 2016-11-22

## README

# codecs

Create an binary encoder/decoder for Node's build in types like, json, utf-8, hex.

```
npm install codecs
```

Useful to support value encodings similar to leveldb's.

## Usage

``` js
const codecs = require('codecs')
const json = codecs('json')

console.log(json.encode({ hello: 'world' })) // Buffer.from('{"hello":"world"}')
console.log(json.decode(new Buffer('{"hello":"world"}'))) // { hello: 'world' }
```

## API

#### `const codec = codecs(type, [fallback])`

Create a new codec.

Supported types are

* utf8
* json
* [ndjson](http://ndjson.org/)
* binary
* hex
* ascii
* base64
* ucs2
* ucs-2
* utf16le
* utf-16le
* binary

If an unknown type is passed-in, the `fallback` is used if given, else `binary`.
If you want to use a custom codec you can pass in an object containing a an `encode` and `decode` method and that will be returned.
If you pass a [compact encoder](https://github.com/compact-encoding/compact-encoding), then it will be converted into a codec for you and returned.

#### `const buf = codec.encode(value)`

Encode a value to a buffer.

#### `const value = codec.decode(buf)`

Decode a buffer to a value.

## License

MIT

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