Skip to content

Commit

Permalink
Refactor and add comment for dedupe operation entries
Browse files Browse the repository at this point in the history
  • Loading branch information
wwtamu committed Jan 26, 2024
1 parent b01d8fc commit 528cf96
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/app/core/json-patch/json-patch-operations.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,16 +351,22 @@ function addOperationToList(body: JsonPatchOperationObject[], actionType, target
newBody.push(makeOperationEntry({ op: JsonPatchOperationType.move, from: fromPath, path: targetPath }));
break;
}
return dedupeOperationEntry(newBody);
return dedupeOperationEntries(newBody);
}

function dedupeOperationEntry(body: JsonPatchOperationObject[]): JsonPatchOperationObject[] {
const upo = new Map<string, any>();
/**
* Dedupe operation entries by op and path. This prevents processing unnecessary patches in a single PATCH request.
*
* @param body JSON patch operation object entries
* @returns deduped JSON patch operation object entries
*/
function dedupeOperationEntries(body: JsonPatchOperationObject[]): JsonPatchOperationObject[] {
const ops = new Map<string, any>();
for (let i = body.length - 1; i >= 0; i--) {
const patch = body[i].operation;
const key = `${patch.op}-${patch.path}`;
if (!upo.has(key)) {
upo.set(key, patch);
if (!ops.has(key)) {
ops.set(key, patch);
} else {
body.splice(i, 1);
}
Expand Down

0 comments on commit 528cf96

Please sign in to comment.