0.6.3 • Published 3 years ago

mutant-json v0.6.3

Weekly downloads
1
License
MIT
Repository
github
Last release
3 years ago

mutant-json

unit-testing npm-publish npm-downloads codecov patreon-donate github-sponsor paypal-sponsor

A complete mutant json which uses 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:

npm install mutant-json --save

Yarn:

yarn add mutant-json

Functions

Typedefs

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

ParamType
targetany
processMutantProcess
optsMutantOptions

Example

Working with promises

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

Kind: global typedef

ParamTypeDescription
op"remove" | "replace"Patch operation
valueany

MutantPatcher : function

Kind: global typedef

ParamType
patchesMutanPatch | Array.<MutanPatch>

MutantProcess : function

Kind: global typedef

ParamType
mutateMutationPatcher
valueany
pathstring
resultany

MutantJsonEntry : Array

Kind: global typedef
Properties

NameTypeDescription
0stringJSONPointer
1anyValue

MutantOptions : Object

Kind: global typedef
Properties

NameTypeDefaultDescription
recursiveBooleantrueenable/disable nested arrays and objects recursion
nestedBooleanfalsealso emit nested array or objects
stepBoolean1the step to increment, default 1
testString | function | RegeExpfalseregexp, string minimatch or function to filter properties
onceBooleanfalseStops when applies the first mutation
promisesBooleantrueProcessing promises taking the resolved as part of the result
promiseBooleanfalseForces to return a promise even if no promises detected
iteratorArray.<MutationJsonEntry> | Iterable | IteratorIterator default traverse-json
patcherfunctionPatcher function
0.6.3

3 years ago

0.6.2

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.5.2

3 years ago

0.5.1

3 years ago

0.5.0

3 years ago

0.4.0

3 years ago

0.3.0

3 years ago

0.1.0

3 years ago

0.2.0

3 years ago

0.1.1

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago