json-diff-ts 5.0.0-alpha.8 → 5.0.0-alpha.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1357,7 +1357,8 @@ function walkChanges(changes, basePath, oldCtx, newCtx, ops, options) {
1357
1357
  const childOld = change.key === "$root" ? oldCtx : oldCtx?.[change.key];
1358
1358
  const childNew = change.key === "$root" ? newCtx : newCtx?.[change.key];
1359
1359
  if (change.embeddedKey) {
1360
- for (const childChange of change.changes) {
1360
+ const orderedChildChanges = orderArrayChildChanges(change.changes, change.embeddedKey);
1361
+ for (const childChange of orderedChildChanges) {
1361
1362
  const filterPath = buildCanonicalFilterPath(
1362
1363
  childPath,
1363
1364
  change.embeddedKey,
@@ -1384,6 +1385,46 @@ function walkChanges(changes, basePath, oldCtx, newCtx, ops, options) {
1384
1385
  }
1385
1386
  }
1386
1387
  }
1388
+ function orderArrayChildChanges(changes, embeddedKey) {
1389
+ if (embeddedKey !== "$index") {
1390
+ return changes;
1391
+ }
1392
+ const groups = [];
1393
+ const pureRemoves = [];
1394
+ for (let i = 0; i < changes.length; i++) {
1395
+ const current = changes[i];
1396
+ const next = changes[i + 1];
1397
+ if (current.type === "REMOVE" /* REMOVE */ && next && next.type === "ADD" /* ADD */ && String(current.key) === String(next.key)) {
1398
+ groups.push({ kind: "preserved", changes: [current, next] });
1399
+ i++;
1400
+ continue;
1401
+ }
1402
+ if (current.type === "REMOVE" /* REMOVE */) {
1403
+ pureRemoves.push(current);
1404
+ groups.push({ kind: "pure-remove" });
1405
+ continue;
1406
+ }
1407
+ groups.push({ kind: "preserved", changes: [current] });
1408
+ }
1409
+ if (pureRemoves.length < 2) {
1410
+ return changes;
1411
+ }
1412
+ const removeIndices = pureRemoves.map((change) => Number(change.key));
1413
+ if (removeIndices.some((idx) => !Number.isInteger(idx))) {
1414
+ return changes;
1415
+ }
1416
+ pureRemoves.sort((a, b) => Number(b.key) - Number(a.key));
1417
+ const ordered = [];
1418
+ let removeIndex = 0;
1419
+ for (const group of groups) {
1420
+ if (group.kind === "pure-remove") {
1421
+ ordered.push(pureRemoves[removeIndex++]);
1422
+ } else {
1423
+ ordered.push(...group.changes);
1424
+ }
1425
+ }
1426
+ return ordered;
1427
+ }
1387
1428
  function emitLeafOp(change, path, ops, options) {
1388
1429
  switch (change.type) {
1389
1430
  case "ADD" /* ADD */: {