# encoders

> Transform data in a functional fashion, inspired by npm:decoders

Latest version **0.1.5** (published 2022-12-01) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.5 |
| Published | 2022-12-01 |
| First published | 2021-08-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 54.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Loïc Payol |
| Maintainers | palra |

## Links

- npm: https://www.npmjs.com/package/encoders
- Repository: https://github.com/palra/encoders
- npm.io page: https://npm.io/package/encoders

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [type-fest](https://npm.io/package/type-fest.md) ^3.3.0

## Recent versions

- 0.1.5 (latest) — 2022-12-01
- 0.1.4 — 2021-09-03
- 0.1.3 — 2021-08-26
- 0.1.2 — 2021-08-26
- 0.1.1 — 2021-08-23

## README

# Encoders ![npm](https://img.shields.io/npm/v/encoders)

Transform data in a functional fashion, inspired by [decoders](https://github.com/nvie/decoders/).

## Motivation

In a DDD environment, you're going to send domain objects to your clients, via
some API, maybe to interact with some third party. However, you might need to
transform the actual shape of the data :

```ts
type User = {
  name: string;
  address: string[];
  postalCode: string;
}

const user: User = {
  name: 'John Doe',
  address: ['Street', 'City'],
  postalCode: '75000'
}

JSON.stringify({
  full_name: user.name,
  address: user.address.join(', '),
  postal_code: user.postal_code
})
```

This module provides an elegant API to express those transformations in a
functional fashion. It provides reusability as every transform is a
composition (FP speaking) of a pure functions :

```ts
type Encoder<I, O> = (in: I) => O
```

You could rewrite the previous example that way :

```ts
import * as e from 'encoders';

type User = {
  name: string;
  address: string;
  postalCode: string;
}

const UserEncoder = e.object<User>()
  .rename('name', 'full_name')
  .rename('postalCode', 'postal_code')
  .map('address', (address) => address.join(', '))
  .encoder;

const user: User = {
  name: 'John Doe',
  address: ['Street', 'City'],
  postalCode: '75000'
}

e.toJson(UserEncoder, user);
```

> :exclamation: **Heads up!** Don't forget to enable _strict mode_ in your `tsconfig.json`.

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