From d3182d23da76c2b664f9f9682979ab6d6d076553 Mon Sep 17 00:00:00 2001 From: Aiden McClelland Date: Thu, 16 Jan 2025 16:41:48 -0700 Subject: [PATCH] fix patching of arrays --- client/lib/json-patch-lib.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/client/lib/json-patch-lib.ts b/client/lib/json-patch-lib.ts index 361a5af..4c0f3db 100644 --- a/client/lib/json-patch-lib.ts +++ b/client/lib/json-patch-lib.ts @@ -115,14 +115,17 @@ function recursiveApplyArray( ): T { const index = parseInt(path[0]) - const updated = recursiveApply(data[index], path.slice(1), op, value) const result = [...data] as T - if (op === PatchOp.ADD) { - result.splice(index, 0, updated) - } else if (op === PatchOp.REPLACE) { - result.splice(index, 1, updated) + // add/remove is only handled differently if this is the last segment in the path + if (path.length === 1) { + if (op === PatchOp.ADD) result.splice(index, 0, value) + else if (op === PatchOp.REMOVE) result.splice(index, 1) } else { - result.splice(index, 1) + result.splice( + index, + 1, + recursiveApply(data[index], path.slice(1), op, value), + ) } return result