Skip to content

Commit 2249caf

Browse files
committed
if newPatch is more steps than original patch, just return original
1 parent 674c25c commit 2249caf

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/optimize.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ const jsonPatch = require('json8-patch');
1515
function optimize(doc, patches) {
1616
const copyDoc = JSON.parse(JSON.stringify(doc));
1717
const { doc: outputDoc } = jsonPatch.apply(copyDoc, patches);
18-
return jsonPatch.diff(doc, outputDoc);
18+
const newPatches = jsonPatch.diff(doc, outputDoc);
19+
if (newPatches.length >= patches.length) return patches;
20+
return newPatches;
1921
}
2022

2123
module.exports = optimize;

src/optimize.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ const cases = [
4040
],
4141
expected: [{ op: 'add', path: '/b', value: 43 }]
4242
},
43+
{
44+
name: 'replace an object',
45+
doc: { foo: { abc: { def: 34 } } },
46+
patches: [{ op: 'replace', path: '/foo/abc', value: { xyz: 64 } }],
47+
expected: [{ op: 'replace', path: '/foo/abc', value: { xyz: 64 } }]
48+
},
4349
{
4450
name: 'add element to end',
4551
doc: { foo: ['bar', 'baz'] },

0 commit comments

Comments
 (0)