Skip to content

Commit

Permalink
Merge pull request #11 from marcolink/fix/max-depth
Browse files Browse the repository at this point in the history
fix: maxDepth was not respected fully
  • Loading branch information
marcolink authored Sep 3, 2024
2 parents 9bf5234 + 196c157 commit 949ebfe
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
35 changes: 25 additions & 10 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -677,19 +677,34 @@ describe('a generate json patch function', () => {
},
};

const patch = generateJSONPatch(before, after, { maxDepth: 3 });
expect(patch).to.eql([
{
op: 'replace',
path: '/firstLevel/secondLevel',
value: {
thirdLevel: {
it('detects changes as a given depth of 3', () => {
const patch = generateJSONPatch(before, after, { maxDepth: 3 });
expect(patch).to.eql([
{
op: 'replace',
path: '/firstLevel/secondLevel',
value: {
thirdLevel: {
fourthLevel: 'hello-brave-new-world',
},
thirdLevelTwo: 'hello',
},
},
]);
});

it('detects changes as a given depth of 4', () => {
const patch = generateJSONPatch(before, after, { maxDepth: 4 });
expect(patch).to.eql([
{
op: 'replace',
path: '/firstLevel/secondLevel/thirdLevel',
value: {
fourthLevel: 'hello-brave-new-world',
},
thirdLevelTwo: 'hello',
},
},
]);
]);
});
});
});

Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ export function generateJSONPatch(
compareArrays(leftValue, rightValue, newPath);
} else if (isJsonObject(rightValue)) {
if (isJsonObject(leftValue)) {
if (maxDepth <= path.split('/').length) {
patch.push({ op: 'replace', path: path, value: rightJsonValue });
if (maxDepth <= newPath.split('/').length) {
patch.push({ op: 'replace', path: newPath, value: rightValue });
} else {
compareObjects(newPath, leftValue, rightValue);
}
Expand Down

0 comments on commit 949ebfe

Please sign in to comment.