# mergeri

> A simple object merger using matcher.

Latest version **0.1.3** (published 2019-06-17) · MIT license · 0 weekly downloads

## Install

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

## 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.3 |
| Published | 2019-06-17 |
| First published | 2018-09-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | skt-t1-byungi |
| Maintainers | skt-t1-byungi |
| Keywords | assign, copy, merge, extend, match, matcher |

## Links

- npm: https://www.npmjs.com/package/mergeri
- Repository: https://github.com/skt-t1-byungi/mergeri
- npm.io page: https://npm.io/package/mergeri

## Recent versions

- 0.1.3 (latest) — 2019-06-17
- 0.1.2 — 2019-06-14
- 0.1.1 — 2019-06-13
- 0.1.0 — 2019-06-13
- 0.0.3 — 2018-09-25
- 0.0.2 — 2018-09-25
- 0.0.1 — 2018-09-24

## README

# mergeri
> A simple object merger using matcher.

[![npm](https://img.shields.io/npm/v/mergeri.svg?style=flat-square)](https://www.npmjs.com/package/mergeri)

![merge objects](pain.gif)

## Install
```sh
npm i mergeri
```

## Example
### Basic
```js
const obj1 = {
    a: {
        b: [
            {
                id: 1000,
                data: { a: 1 }
            }
        ]
    }
}

const obj2 = {
    a: {
        b: [
            {
                id: 999,
                data: { a: 2 }
            },
            {
                id: 1000,
                data: { b: 3 }
            }
        ]
    }
}

const result = mergeri({ 'a.b.*': 'id' }, obj1, obj2)
```

`result`
```js
{
    a: {
        b: [
            {
                id: 999,
                data: { a: 2 }
            },
            {
                id: 1000,
                data: { a: 1, b: 3 }
            }
        ]
    }
}
```

## API
### mergeri(matcher, target, src1, [src2, src3...])

#### Matcher

##### Dot notation
```js
const obj = {
    a: {
        b: [
            {c: {d: 1 }},
            {c: {d: 2 }}
        ]
    }
}

const matcher = { "a.b.*": "c.d" }
```

##### Multiple matcher
```js
const matchers = {
    "a.*": "b.c.d" ,
    "e.f.g.*": "h.i",
    "x.y.*": "z",
}
```

##### Middle wildcard
```js
const matcher = {
    "a.*.c.*": "other_id"
}
```

##### Complex matcher
```js
const matcher = {
    "a.b.*": ["id", "other_id"]
}
```

##### Custom matcher
```js
const matcher = {
    "a.b.*": function(targetKey, srcKey, targetValue, srcValue, targetObj, srcObj) {
        return targetValue.c.id === srcValue.c.id
    }
    // same as {"a.b.*": "c.id"}
}
```

##### Array merging
The default behavior is `concat`. but, `Custom key matcher` change the behavior to index merging.

```js
mergeri(null, {a: [1]}, {a: [2, 3]}) // => {a: [1, 2, 3]}

mergeri({'a': (targetKey, srcKey) => targetKey === srcKey }, {a: [1]}, {a: [2, 3]}) // => {a: [2, 3]}
```

##### Last wildcard is optional.
```js
const matcher = { 'a.b': 'id' } // => same as {'a.b.*': 'id'}
```

## License
MIT

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