rubeniskov/mutant-json

Repository files navigation

unit-testingnpm-publishnpm-downloadscodecovpatreon-donategithub-sponsorpaypal-sponsor

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.

npm install mutant-json --save
yarn add mutant-json
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].

Mutan : function

definition acording to the json standard

Mutanter : function
MutantProcess : function
MutantJsonEntry : Array
MutantOptions : Object

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

ParamType
targetany
processMutantProcess
optsMutantOptions

Example

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);
{
  foo: 0,
  nested: {
    depth: 2,
    nested: {
      depth: 4,
      nested: {
        depth: 6,
        nested: {
          depth: 8,
        },
      },
    },
  },
  bar: 2,
}

definition acording to the json standard

Kind: global typedef

ParamTypeDescription
op"remove" | "replace"operation
valueany

Kind: global typedef

ParamType
esMutan | Array.<Mutan>

Kind: global typedef

ParamType
mutateMutationer
valueany
pathstring
resultany

Kind: global typedef
Properties

NameTypeDescription
0stringJSONPointer
1anyValue

Kind: global typedef
Properties

NameTypeDefaultDescription
[recursive]Booleantrueenable/disable nested arrays and objects recursion
[nested]Booleanfalsealso emit nested array or objects
[step]Boolean1the step to increment, default 1
[test]String | function | RegeExpfalseregexp, string minimatch or function to filter properties
[once]BooleanfalseStops when applies the first mutation
[promises]BooleantrueProcessing promises taking the resolved as part of the result
[promise]BooleanfalseForces to return a promise even if no promises detected
[iterator]Array.<MutationJsonEntry> | Iterable | IteratorIterator default traverse-json
[er]functioner function