From 41d5ab57868edecd72c560ff5d38e1358f9931d5 Mon Sep 17 00:00:00 2001 From: Jos de Jong Date: Sun, 23 Aug 2020 16:27:29 +0200 Subject: [PATCH] Fix immutableJSONPatch not handling root path --- src/utils/immutableJSONPatch.js | 4 ++-- src/utils/immutableJSONPatch.test.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/utils/immutableJSONPatch.js b/src/utils/immutableJSONPatch.js index 4b14eae..ab9d359 100644 --- a/src/utils/immutableJSONPatch.js +++ b/src/utils/immutableJSONPatch.js @@ -22,8 +22,8 @@ export function immutableJSONPatch (json, operations) { for (let i = 0; i < operations.length; i++) { const operation = operations[i] - const path = operation.path ? parseJSONPointer(operation.path) : null - const from = operation.from ? parseJSONPointer(operation.from) : null + const path = operation.path != undefined ? parseJSONPointer(operation.path) : null + const from = operation.from != undefined ? parseJSONPointer(operation.from) : null switch (operation.op) { case 'add': { diff --git a/src/utils/immutableJSONPatch.test.js b/src/utils/immutableJSONPatch.test.js index 7f13f63..548314a 100644 --- a/src/utils/immutableJSONPatch.test.js +++ b/src/utils/immutableJSONPatch.test.js @@ -145,6 +145,18 @@ describe('immutableJSONPatch', () => { assert.strictEqual(result.json.unchanged, json.unchanged) }) + it('jsonpatch replace root document', () => { + const json = { + a: 2 + } + const patch = [ + {op: 'replace', path: '', value: {b: 3}} + ] + const result = immutableJSONPatch(json, patch) + + assert.deepStrictEqual(result.json, {b: 3}) + }) + it('jsonpatch copy', () => { const json = { arr: [1,2,3],