# o-mapper

> Object mapper

Latest version **1.0.0** (published 2018-08-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install o-mapper
pnpm add o-mapper
yarn add o-mapper
bun add o-mapper
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2018-08-17 |
| First published | 2015-06-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6.4.0 |
| Dependencies | 0 |
| Unpacked size | 5.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Pedro Miranda |
| Maintainers | opedromiranda |
| Keywords | object, mapper, schema |

## Links

- npm: https://www.npmjs.com/package/o-mapper
- Repository: https://github.com/opedromiranda/o-mapper
- Issues: https://github.com/opedromiranda/o-mapper/issues
- npm.io page: https://npm.io/package/o-mapper

## Alternatives

- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads
- [@oridune/validator](https://npm.io/package/@oridune/validator.md) — 16 weekly downloads

## Recent versions

- 1.0.0 (latest) — 2018-08-17
- 0.1.4 — 2018-07-17
- 0.1.3 — 2016-02-21
- 0.1.2 — 2015-06-07
- 0.1.1 — 2015-06-05
- 0.1.0 — 2015-06-04

## README

# O-Mapper
[![Build Status](https://travis-ci.org/opedromiranda/o-mapper.svg)](https://travis-ci.org/opedromiranda/o-mapper)


## What
Validation and conversion of a given object to another.

Like this:
```json
{
    "first_name": "Toby",
    "last_name": "Flenderson",
    "birth_date": "1971-04-01",
    "job_name": "HR",
    "extra_data": {
        "orders": []
    }
}
```
to this:
```javascript
{
    "full_name": "Toby Flenderson",
    "job": "HR",
    "birth_date": new Date("1971-04-01"),
    "orders": []
}
```


## How
```javascript
const omapper = require('o-mapper');

const schema = { ... };
const input = { ... };

const result = omapper(input, schema);
```

## Schema
Schemas are objects that will dictate what the final object will contain and where to get the values from the source object.
Each key from the schema represents a key of the final object.

```javascript
const schema = {

    // key property specifies what key to look for in the source object
    job: {
        key: 'job_name',
    },

    // when multiple keys are selected, an handler is required to process the multiple values
    full_name: {
        key: ['fist_name', 'last_name'],
        handler: (first, last) => `${first} ${last}`,
    },

    // handlers can also be used for single values
    // the key property can be omitted if it matches the final object
    // a property can be set as required
    birth_date: {
        handler: bd => new Date(bd),
        required: true,
    },

    // dot notation can also be used
    // default values can be set in case property doesn't exist
    orders: {
        key: 'data.orders',
        default: [],
    },
};
```

License
----

MIT

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