Skip to content

Commit 949ebfe

Browse files
authored
Merge pull request #11 from marcolink/fix/max-depth
fix: maxDepth was not respected fully
2 parents 9bf5234 + 196c157 commit 949ebfe

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

src/index.spec.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -677,19 +677,34 @@ describe('a generate json patch function', () => {
677677
},
678678
};
679679

680-
const patch = generateJSONPatch(before, after, { maxDepth: 3 });
681-
expect(patch).to.eql([
682-
{
683-
op: 'replace',
684-
path: '/firstLevel/secondLevel',
685-
value: {
686-
thirdLevel: {
680+
it('detects changes as a given depth of 3', () => {
681+
const patch = generateJSONPatch(before, after, { maxDepth: 3 });
682+
expect(patch).to.eql([
683+
{
684+
op: 'replace',
685+
path: '/firstLevel/secondLevel',
686+
value: {
687+
thirdLevel: {
688+
fourthLevel: 'hello-brave-new-world',
689+
},
690+
thirdLevelTwo: 'hello',
691+
},
692+
},
693+
]);
694+
});
695+
696+
it('detects changes as a given depth of 4', () => {
697+
const patch = generateJSONPatch(before, after, { maxDepth: 4 });
698+
expect(patch).to.eql([
699+
{
700+
op: 'replace',
701+
path: '/firstLevel/secondLevel/thirdLevel',
702+
value: {
687703
fourthLevel: 'hello-brave-new-world',
688704
},
689-
thirdLevelTwo: 'hello',
690705
},
691-
},
692-
]);
706+
]);
707+
});
693708
});
694709
});
695710

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ export function generateJSONPatch(
186186
compareArrays(leftValue, rightValue, newPath);
187187
} else if (isJsonObject(rightValue)) {
188188
if (isJsonObject(leftValue)) {
189-
if (maxDepth <= path.split('/').length) {
190-
patch.push({ op: 'replace', path: path, value: rightJsonValue });
189+
if (maxDepth <= newPath.split('/').length) {
190+
patch.push({ op: 'replace', path: newPath, value: rightValue });
191191
} else {
192192
compareObjects(newPath, leftValue, rightValue);
193193
}

0 commit comments

Comments
 (0)