# @mojule/mapper

> Framework for creating object mappers

Latest version **0.1.1** (published 2017-10-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install @mojule/mapper
pnpm add @mojule/mapper
yarn add @mojule/mapper
bun add @mojule/mapper
```

## 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.1 |
| Published | 2017-10-29 |
| First published | 2017-10-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Nik Coughlin |
| Maintainers | nrkn, andybell |

## Links

- npm: https://www.npmjs.com/package/@mojule/mapper
- Repository: https://github.com/mojule/mapper
- Homepage: https://github.com/mojule/mapper#readme
- Issues: https://github.com/mojule/mapper/issues
- npm.io page: https://npm.io/package/@mojule/mapper

## Dependencies (1)

- [@mojule/is](https://npm.io/package/@mojule/is.md) ^0.1.11

## Recent versions

- 0.1.1 (latest) — 2017-10-29
- 0.1.0 — 2017-10-04

## README

# mapper

A simple module for mapping things from one value to another.

`npm install @mojule/mapper`

With no options passed, it defaults to returning a function that expects JSON
compatible values and clones them:

```javascript
const Mapper = require( '@mojule/mapper' )

const mapper = Mapper()

const data = { foo: 'bar' }
const clone = mapper( data )
```

The real value of it comes when you pass custom predicates and mappings.

Clone the value, and while cloning it convert any instances of a legacy person
object to a new person object:

```javascript
const Mapper = require( '@mojule/mapper' )
const is = require( '@mojule/is' )

const map = {
  legacyPerson: value => {
    const { name, age } = value
    const [ firstName, lastName ] = name.split( ' ' )

    return { firstName, lastName, age }
  }
}

const predicates = {
  legacyPerson: value =>
    is.object( value ) && is.string( value.name ) && is.number( value.age )
}

const mapper = Mapper( { map, predicates } )

const data = {
  department: 'Mad Science',
  people: [
    { name: 'Xjkzlor Wijklmon', age: 107 },
    { name: 'Vrnujghx Brjhamix', age: 9 }
  ]
}

/*
  {
    department: 'Mad Science',
    people: [
      { firstName: 'Xjkzlor', lastName: 'Wijklmon', age: 107 },
      { firstName: 'Vrnujghx', lastName: 'Brjhamix', age: 9 }
    ]
  }
*/
const result = mapper( data )
```

The easiest way to understand is probably to look at the test fixtures where you
can see examples of:

- Mapping objects to and from arrays of name value pairs
- Mapping non-JSON compatible values to and from JSON-compatible values
- Mapping objects to and from JSON schema

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