diff --git a/README.md b/README.md index 6a7c54e..86bfefa 100644 --- a/README.md +++ b/README.md @@ -128,8 +128,6 @@ Patch definition acording to the [jsonpatch standard](http://jsonpatch.com/) | --- | --- | --- | | op | "remove" \| "replace" | Patch operation | | value | any | | -| [path] | String | [JSONPointer](https://tools.ietf.org/html/rfc6901) | -| [from] | String | [JSONPointer](https://tools.ietf.org/html/rfc6901) | @@ -174,9 +172,10 @@ Patch definition acording to the [jsonpatch standard](http://jsonpatch.com/) | [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 | -| [opts.test] | String \| function \| RegeExp | false | regexp, string [minimatch](https://www.npmjs.com/package/minimatch) or function to filter properties | +| [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 | diff --git a/index.js b/index.js index 429f5e1..a62a35f 100644 --- a/index.js +++ b/index.js @@ -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) */ /** @@ -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|Iterable|Iterator} [iterator] Iterator default [traverse-json](https://github.com/rubeniskov/traverse-json) * @prop {Function} [patcher] Patcher function */ @@ -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); @@ -220,6 +220,10 @@ const mutantJson = (target, process, opts) => { return traverse(result); }; + if (promise) { + return Promise.resolve(traverse(target)); + } + return traverse(target); }; diff --git a/test.js b/test.js index 7197a4a..55e6046 100644 --- a/test.js +++ b/test.js @@ -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, @@ -382,9 +403,6 @@ 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, })); @@ -392,3 +410,27 @@ test('should mutate using a promise', async (t) => { 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)); +});