[](https://github.com/rubeniskov/mutant-json/actions?query=workflow%3Aunit-testing) [






A complete mutant json which uses traverse-json to enable traverse filtering.
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.
shell
npm install mutant-json --save
`
$3
`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 |
| opts | MutantOptions |
Example
$3
`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);
`$3
`
{
foo: 0,
nested: {
depth: 2,
nested: {
depth: 4,
nested: {
depth: 6,
nested: {
depth: 8,
},
},
},
},
bar: 2,
}
``functionKind: global typedef
| Param | Type | Description |
| --- | --- | --- |
| op | "remove" \| "replace" | Patch operation |
| value | any | |
function| Param | Type |
| --- | --- |
| patches | MutanPatch \| Array.<MutanPatch> |
function| Param | Type |
| --- | --- |
| mutate | MutationPatcher |
| value | any |
| path | string |
| result | any |
Array| Name | Type | Description |
| --- | --- | --- |
| 0 | string | JSONPointer |
| 1 | any | Value |
Object| 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 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 |
| [patcher] | function | | Patcher function |