Skip to content

Commit

Permalink
add: promise flag option
Browse files Browse the repository at this point in the history
  • Loading branch information
rubeniskov committed Dec 4, 2020
1 parent 8129ac9 commit e1cd85a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ Patch definition acording to the [jsonpatch standard](http://jsonpatch.com/)
| --- | --- | --- |
| op | <code>&quot;remove&quot;</code> \| <code>&quot;replace&quot;</code> | Patch operation |
| value | <code>any</code> | |
| [path] | <code>String</code> | [JSONPointer](https://tools.ietf.org/html/rfc6901) |
| [from] | <code>String</code> | [JSONPointer](https://tools.ietf.org/html/rfc6901) |

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

Expand Down Expand Up @@ -174,9 +172,10 @@ Patch definition acording to the [jsonpatch standard](http://jsonpatch.com/)
| [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 |
| [opts.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 |
| [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 |

10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ const parseOptions = (opts) => typeof opts !== 'object' ? ({ test: opts }) : { .
* @callback MutanPatch
* @param {("remove"|"replace")} op Patch operation
* @param {any} value
* @param {String} [path] [JSONPointer](https://tools.ietf.org/html/rfc6901)
* @param {String} [from] [JSONPointer](https://tools.ietf.org/html/rfc6901)
*/

/**
Expand All @@ -50,9 +48,10 @@ const parseOptions = (opts) => typeof opts !== 'object' ? ({ test: opts }) : { .
* @prop {Boolean} [recursive=true] enable/disable nested arrays and objects recursion
* @prop {Boolean} [nested=false] also emit nested array or objects
* @prop {Boolean} [step=1] the step to increment, default 1
* @prop {String|Function|RegeExp} [opts.test=false] regexp, string [minimatch](https://www.npmjs.com/package/minimatch) or function to filter properties
* @prop {String|Function|RegeExp} [test=false] regexp, string [minimatch](https://www.npmjs.com/package/minimatch) or function to filter properties
* @prop {Boolean} [once=false] Stops when applies the first mutation
* @prop {Boolean} [promises=true] Processing promises taking the resolved as part of the result
* @prop {Boolean} [promise=false] Forces to return a promise even if no promises detected
* @prop {Array<MutationJsonEntry>|Iterable|Iterator} [iterator] Iterator default [traverse-json](https://github.com/rubeniskov/traverse-json)
* @prop {Function} [patcher] Patcher function
*/
Expand Down Expand Up @@ -127,6 +126,7 @@ const mutantJson = (target, process, opts) => {
const {
once = false,
promises = true,
promise = false,
iterator = traverseIterator(target, opts),
patcher = (target, patch) => jsonpatcher.apply_patch(target, [patch]),
} = parseOptions(opts);
Expand Down Expand Up @@ -220,6 +220,10 @@ const mutantJson = (target, process, opts) => {
return traverse(result);
};

if (promise) {
return Promise.resolve(traverse(target));
}

return traverse(target);
};

Expand Down
48 changes: 45 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,27 @@ test('should replace all the values entries by an string using replace', (t) =>
t.deepEqual(actual, expected);
});

test('should do nothing when no patch', (t) => {
const expected = {
a: 0,
b: 1,
c: 2,
};

const entries = Object.entries(oneDepthObject).map(([k, v]) => [`/${k}`, v]);

t.plan(4);
let idx = 0;
const actual = mutateJson(oneDepthObject, (mutate, value, path) => {
t.deepEqual([path, value], entries[idx++]);
mutate();
}, {
iterator: entries,
});

t.deepEqual(actual, expected);
});

test('should remove entries with value 1', (t) => {
const expected = {
a: 0,
Expand Down Expand Up @@ -382,13 +403,34 @@ test('should mutate using a promise', async (t) => {
};

const actual = await mutateJson(Promise.resolve(flattenObjectPromises), (mutate, value) => {
// mutate(Promise.resolve({
// value: value * 100,
// }));
mutate(Promise.resolve({
value: value * 100,
}));
});

t.deepEqual(actual, expected);
});


test('should force return a promise even when no promise detected', async (t) => {

const flattenObjectPromises = {
foo: 1,
bar: 2,
};

const expected = {
foo: 100,
bar: 200,
};

const actual = mutateJson(flattenObjectPromises, (mutate, value) => {
mutate({
value: value * 100,
});
}, { promise: true });

t.true(actual instanceof Promise);

return actual.then((value) => t.deepEqual(value, expected));
});

0 comments on commit e1cd85a

Please sign in to comment.