# data-adapter

> Library for data adaptation using decorators. You can declaratively state how your data should look when being send to the server.

Latest version **0.2.3** (published 2016-03-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install data-adapter
pnpm add data-adapter
yarn add data-adapter
bun add data-adapter
```

## Health

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

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

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.3 |
| Published | 2016-03-23 |
| First published | 2016-03-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 44 |
| Author | Minko Gechev |
| Maintainers | mgechev |
| Keywords | json, data adapter, adapter, translator, normalizer, json adapter, restful adapter |

## Links

- npm: https://www.npmjs.com/package/data-adapter
- Repository: https://github.com/mgechev/data-adapter
- Homepage: https://github.com/mgechev/data-adapter#readme
- Issues: https://github.com/mgechev/data-adapter/issues
- npm.io page: https://npm.io/package/data-adapter

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.2.3 (latest) — 2016-03-23
- 0.2.2 — 2016-03-22
- 0.2.1 — 2016-03-22
- 0.2.0 — 2016-03-21
- 0.1.5 — 2016-03-21
- 0.1.4 — 2016-03-20
- 0.1.3 — 2016-03-20
- 0.1.2 — 2016-03-19
- 0.1.1 — 2016-03-18
- 0.1.0 — 2016-03-18
- 0.0.1 — 2016-03-18

## README

# Introduction

Library for data adaptation using decorators. You can declaratively state how your data should look when being send to the server.

## How to use?

```
npm install data-adapter
```

## Example

### Using class adapter

```ts
import {AdaptClass, denormalize, normalize} from 'data-adapter';

@AdaptClass({
  name: snakeCase
})
class Human {
  firstName: string = 'foo';
  lastName: string = 'bar';
  age: number = 42;
}

// { first_name: 'foo', last_name: 'bar', age: 42 }
denormalize(new Human());

// { firstName: 'foo', lastName: 'bar', age: 42 }
normalize({ first_name: 'foo', last_name: 'bar', age: 42 }, Human);

```

### Using property adapters

```ts
import {Adapt, denormalize, normalize} from 'data-adapter';

const denormalize = (obj, field) => {
  return obj[field] === 'male' ? 0 : 1;
};

const normalize = (obj, field) => {
  return obj[field] === 0 ? 'male' : 'female';
};

class Kid {
  @Adapt({ normalize, denormalize })
  gender;
}

class Parent {
  @Adapt({ name: 'first_name' }) firstName;
  @Adapt({ name: 'last_name' }) lastName;
  @Adapt({ type: Kid })
  kids;
}

let parent = new Parent();
parent.firstName = 'John';
parent.lastName = 'Doe';

let kid1 = new Kid();
kid1.gender = 'male';
let kid2 = new Kid();
kid2.gender = 'female';

// { first_name: 'John', last_name: 'Doe', kids: [{ gender: 0 }, { gender: 1 }] }
const denormalized = denormalize(new Person());

// { firstName: 'John', lastName: 'Doe', kids: [{ gender: 'male' }, { gender: 'female' }] }
const normalized = normalize(denormalized);
```

## License

MIT

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