# mutant-json

> [![unit-testing](https://github.com/rubeniskov/mutant-json/workflows/unit-testing/badge.svg)](https://github.com/rubeniskov/mutant-json/actions?query=workflow%3Aunit-testing) [![npm-publish](https://github.com/rubeniskov/mutant-json/workflows/npm-publish/

Latest version **0.6.3** (published 2020-12-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install mutant-json
pnpm add mutant-json
yarn add mutant-json
bun add mutant-json
```

## 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.6.3 |
| Published | 2020-12-07 |
| First published | 2020-11-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 16.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | rubeniskov |
| Maintainers | rubeniskov |

## Links

- npm: https://www.npmjs.com/package/mutant-json
- Repository: https://github.com/rubeniskov/mutant-json
- Homepage: https://github.com/rubeniskov/mutant-json#readme
- Issues: https://github.com/rubeniskov/mutant-json/issues
- npm.io page: https://npm.io/package/mutant-json

## Dependencies (3)

- [jsonpatch](https://npm.io/package/jsonpatch.md) ^3.0.1
- [is-promise](https://npm.io/package/is-promise.md) ^4.0.0
- [traverse-json](https://npm.io/package/traverse-json.md) ^0.5.1

## Recent versions

- 0.6.3 (latest) — 2020-12-07
- 0.6.2 — 2020-12-07
- 0.6.1 — 2020-12-06
- 0.6.0 — 2020-12-04
- 0.5.2 — 2020-12-01
- 0.5.1 — 2020-11-27
- 0.5.0 — 2020-11-27
- 0.4.0 — 2020-11-27
- 0.3.0 — 2020-11-26
- 0.2.0 — 2020-11-25
- 0.1.1 — 2020-11-25
- 0.1.0 — 2020-11-24
- 0.0.3 — 2020-11-24
- 0.0.2 — 2020-11-24
- 0.0.1 — 2020-11-24

## README

# mutant-json

[![unit-testing](https://github.com/rubeniskov/mutant-json/workflows/unit-testing/badge.svg)](https://github.com/rubeniskov/mutant-json/actions?query=workflow%3Aunit-testing)
[![npm-publish](https://github.com/rubeniskov/mutant-json/workflows/npm-publish/badge.svg)](https://github.com/rubeniskov/mutant-json/actions?query=workflow%3Anpm-publish)
[![npm-downloads](https://img.shields.io/npm/dw/mutant-json)](https://www.npmjs.com/package/mutant-json)
[![codecov](https://codecov.io/gh/rubeniskov/mutant-json/branch/master/graph/badge.svg)](https://codecov.io/gh/rubeniskov/mutant-json)
[![patreon-donate](https://img.shields.io/badge/patreon-donate-yellow.svg)](https://patreon.com/rubeniskov)
[![github-sponsor](https://img.shields.io/badge/github-donate-yellow.svg)](https://github.com/sponsors/rubeniskov)
[![paypal-sponsor](https://img.shields.io/badge/paypal-donate-yellow.svg)](https://paypal.me/rubeniskov)

A complete mutant json which uses [traverse-json](https://github.com/rubeniskov/traverse-json) to enable traverse filtering.

## Motivation

Many time I've encontered with the difficult task of mutate a object with nested properties by filtering properties using a single function, so a `mutant-json` solves this using `traverse-json` with multiple options for traversing.


## Installation

### Npm:
```shell
npm install mutant-json --save
```
### Yarn:
```shell
yarn add mutant-json
```
## Functions

<dl>
<dt><a href="#mutantJson">mutantJson(target, process, opts)</a></dt>
<dd><p>Iterates through the given iterator and applies mutation
whereas the iterator entry returns. Also works with promises.
The iteratee must return an entry of [path, value].</p>
</dd>
</dl>

## Typedefs

<dl>
<dt><a href="#MutanPatch">MutanPatch</a> : <code>function</code></dt>
<dd><p>Patch definition acording to the <a href="http://jsonpatch.com/">jsonpatch standard</a></p>
</dd>
<dt><a href="#MutantPatcher">MutantPatcher</a> : <code>function</code></dt>
<dd></dd>
<dt><a href="#MutantProcess">MutantProcess</a> : <code>function</code></dt>
<dd></dd>
<dt><a href="#MutantJsonEntry">MutantJsonEntry</a> : <code>Array</code></dt>
<dd></dd>
<dt><a href="#MutantOptions">MutantOptions</a> : <code>Object</code></dt>
<dd></dd>
</dl>

<a name="mutantJson"></a>

## mutantJson(target, process, opts)
Iterates through the given iterator and applies mutation
whereas the iterator entry returns. Also works with promises.
The iteratee must return an entry of [path, value].

**Kind**: global function  

| Param | Type |
| --- | --- |
| target | <code>any</code> | 
| process | [<code>MutantProcess</code>](#MutantProcess) | 
| opts | [<code>MutantOptions</code>](#MutantOptions) | 

**Example**  
### Working with promises

```javascript
const mutateJson = require('mutant-json');

const recursiveObjectPromises = {
  foo: 0,
  nested: Promise.resolve({
    depth: 1,
    nested: Promise.resolve({
      depth: 2,
      nested: Promise.resolve({
        depth: 3,
        nested: Promise.resolve({
          depth: 4,
        }),
      }),
    }),
  }),
  bar: 1,
};

const actual = await mutateJson(recursiveObjectPromises, (mutate, value) => {
  mutate({
    value: value * 2,
  });
});

console.log(actual);
```

### Output
```
{
  foo: 0,
  nested: {
    depth: 2,
    nested: {
      depth: 4,
      nested: {
        depth: 6,
        nested: {
          depth: 8,
        },
      },
    },
  },
  bar: 2,
}
```
<a name="MutanPatch"></a>

## MutanPatch : <code>function</code>
Patch definition acording to the [jsonpatch standard](http://jsonpatch.com/)

**Kind**: global typedef  

| Param | Type | Description |
| --- | --- | --- |
| op | <code>&quot;remove&quot;</code> \| <code>&quot;replace&quot;</code> | Patch operation |
| value | <code>any</code> |  |

<a name="MutantPatcher"></a>

## MutantPatcher : <code>function</code>
**Kind**: global typedef  

| Param | Type |
| --- | --- |
| patches | [<code>MutanPatch</code>](#MutanPatch) \| [<code>Array.&lt;MutanPatch&gt;</code>](#MutanPatch) | 

<a name="MutantProcess"></a>

## MutantProcess : <code>function</code>
**Kind**: global typedef  

| Param | Type |
| --- | --- |
| mutate | <code>MutationPatcher</code> | 
| value | <code>any</code> | 
| path | <code>string</code> | 
| result | <code>any</code> | 

<a name="MutantJsonEntry"></a>

## MutantJsonEntry : <code>Array</code>
**Kind**: global typedef  
**Properties**

| Name | Type | Description |
| --- | --- | --- |
| 0 | <code>string</code> | [JSONPointer](https://tools.ietf.org/html/rfc6901) |
| 1 | <code>any</code> | Value |

<a name="MutantOptions"></a>

## MutantOptions : <code>Object</code>
**Kind**: global typedef  
**Properties**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| [recursive] | <code>Boolean</code> | <code>true</code> | enable/disable nested arrays and objects recursion |
| [nested] | <code>Boolean</code> | <code>false</code> | also emit nested array or objects |
| [step] | <code>Boolean</code> | <code>1</code> | the step to increment, default 1 |
| [test] | <code>String</code> \| <code>function</code> \| <code>RegeExp</code> | <code>false</code> | regexp, string [minimatch](https://www.npmjs.com/package/minimatch) or function to filter properties |
| [once] | <code>Boolean</code> | <code>false</code> | Stops when applies the first mutation |
| [promises] | <code>Boolean</code> | <code>true</code> | Processing promises taking the resolved as part of the result |
| [promise] | <code>Boolean</code> | <code>false</code> | Forces to return a promise even if no promises detected |
| [iterator] | <code>Array.&lt;MutationJsonEntry&gt;</code> \| <code>Iterable</code> \| <code>Iterator</code> |  | Iterator default [traverse-json](https://github.com/rubeniskov/traverse-json) |
| [patcher] | <code>function</code> |  | Patcher function |

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