Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rubeniskov/mutant-json
A complete mutate json function which traverses using traverse-json.
https://github.com/rubeniskov/mutant-json
iteratee iterator json jsonpatch jsonpatch-standard jsonschema mutation patch promises redux state traverse traverse-json
Last synced: about 2 months ago
JSON representation
A complete mutate json function which traverses using traverse-json.
- Host: GitHub
- URL: https://github.com/rubeniskov/mutant-json
- Owner: rubeniskov
- License: mit
- Created: 2020-11-24T19:13:45.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2020-12-08T15:53:45.000Z (about 4 years ago)
- Last Synced: 2024-11-16T03:27:02.641Z (2 months ago)
- Topics: iteratee, iterator, json, jsonpatch, jsonpatch-standard, jsonschema, mutation, patch, promises, redux, state, traverse, traverse-json
- Language: JavaScript
- Homepage: http://rubeniskov.com/blog/en/traverse-json-like-a-king
- Size: 140 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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
- 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].
## Typedefs
-
MutanPatch :function
-
Patch definition acording to the jsonpatch standard
-
MutantPatcher :function
-
MutantProcess :function
-
MutantJsonEntry :Array
-
MutantOptions :Object
## 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 | any
|
| process | [MutantProcess
](#MutantProcess) |
| opts | [MutantOptions
](#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,
}
```
## MutanPatch : function
Patch definition acording to the [jsonpatch standard](http://jsonpatch.com/)
**Kind**: global typedef
| Param | Type | Description |
| --- | --- | --- |
| op | "remove"
\| "replace"
| Patch operation |
| value | any
| |
## MutantPatcher : function
**Kind**: global typedef
| Param | Type |
| --- | --- |
| patches | [MutanPatch
](#MutanPatch) \| [Array.<MutanPatch>
](#MutanPatch) |
## MutantProcess : function
**Kind**: global typedef
| Param | Type |
| --- | --- |
| mutate | MutationPatcher
|
| value | any
|
| path | string
|
| result | any
|
## MutantJsonEntry : Array
**Kind**: global typedef
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| 0 | string
| [JSONPointer](https://tools.ietf.org/html/rfc6901) |
| 1 | any
| Value |
## MutantOptions : Object
**Kind**: global typedef
**Properties**
| Name | Type | Default | Description |
| --- | --- | --- | --- |
| [recursive] | Boolean
| true
| enable/disable nested arrays and objects recursion |
| [nested] | Boolean
| false
| also emit nested array or objects |
| [step] | Boolean
| 1
| the step to increment, default 1 |
| [test] | String
\| function
\| RegeExp
| false
| regexp, string [minimatch](https://www.npmjs.com/package/minimatch) or function to filter properties |
| [once] | Boolean
| false
| Stops when applies the first mutation |
| [promises] | Boolean
| true
| Processing promises taking the resolved as part of the result |
| [promise] | Boolean
| false
| Forces to return a promise even if no promises detected |
| [iterator] | Array.<MutationJsonEntry>
\| Iterable
\| Iterator
| | Iterator default [traverse-json](https://github.com/rubeniskov/traverse-json) |
| [patcher] | function
| | Patcher function |