Fix immutableJSONPatch not handling root path

This commit is contained in:
Jos de Jong 2020-08-23 16:27:29 +02:00
parent 058cf6e104
commit 41d5ab5786
2 changed files with 14 additions and 2 deletions

View File

@ -22,8 +22,8 @@ export function immutableJSONPatch (json, operations) {
for (let i = 0; i < operations.length; i++) { for (let i = 0; i < operations.length; i++) {
const operation = operations[i] const operation = operations[i]
const path = operation.path ? parseJSONPointer(operation.path) : null const path = operation.path != undefined ? parseJSONPointer(operation.path) : null
const from = operation.from ? parseJSONPointer(operation.from) : null const from = operation.from != undefined ? parseJSONPointer(operation.from) : null
switch (operation.op) { switch (operation.op) {
case 'add': { case 'add': {

View File

@ -145,6 +145,18 @@ describe('immutableJSONPatch', () => {
assert.strictEqual(result.json.unchanged, json.unchanged) 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', () => { it('jsonpatch copy', () => {
const json = { const json = {
arr: [1,2,3], arr: [1,2,3],