Skip to content

Commit 4b76581

Browse files
committed
fix: last item in delta detection
1 parent 7c0a3d4 commit 4b76581

File tree

1 file changed

+18
-9
lines changed
  • packages/jsondiffpatch/src/formatters

1 file changed

+18
-9
lines changed

packages/jsondiffpatch/src/formatters/base.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ abstract class BaseFormatter<
244244
for (let index = 0; index < keys.length; index++) {
245245
const key = keys[index];
246246
if (key === undefined) continue;
247-
const isLast = index === length - 1;
247+
const isLast = index === keys.length - 1;
248248
fn(
249249
// for object diff, the delta key and left key are the same
250250
key,
@@ -310,8 +310,6 @@ abstract class BaseFormatter<
310310
rightIndex < rightLength ||
311311
`${rightIndex}` in arrayDelta
312312
) {
313-
const isLast =
314-
leftIndex === leftLength - 1 || rightIndex === rightLength - 1;
315313
let hasDelta = false;
316314

317315
const leftIndexKey = `_${leftIndex}` as const;
@@ -333,7 +331,12 @@ abstract class BaseFormatter<
333331
value: leftArray ? leftArray[movedFromIndex] : undefined,
334332
}
335333
: undefined,
336-
isLast && !(rightIndexKey in arrayDelta),
334+
335+
// is this the last key in this delta?
336+
leftIndex === leftLength - 1 &&
337+
rightIndex === rightLength - 1 &&
338+
!(`${rightIndex + 1}` in arrayDelta) &&
339+
!(rightIndexKey in arrayDelta),
337340
);
338341

339342
if (Array.isArray(itemDelta)) {
@@ -357,6 +360,7 @@ abstract class BaseFormatter<
357360
// something happened to the right item at this position
358361
hasDelta = true;
359362
const itemDelta = arrayDelta[rightIndexKey];
363+
const isItemAdded = Array.isArray(itemDelta) && itemDelta.length === 1;
360364
fn(
361365
rightIndexKey,
362366
movedFromIndex ?? leftIndex,
@@ -366,10 +370,14 @@ abstract class BaseFormatter<
366370
value: leftArray ? leftArray[movedFromIndex] : undefined,
367371
}
368372
: undefined,
369-
isLast,
373+
// is this the last key in this delta?
374+
leftIndex === leftLength - 1 &&
375+
rightIndex === rightLength - 1 + (isItemAdded ? 1 : 0) &&
376+
!(`_${leftIndex + 1}` in arrayDelta) &&
377+
!(`${rightIndex + 1}` in arrayDelta),
370378
);
371379

372-
if (Array.isArray(itemDelta) && itemDelta.length === 1) {
380+
if (isItemAdded) {
373381
// added
374382
rightLength++;
375383
rightIndex++;
@@ -398,7 +406,10 @@ abstract class BaseFormatter<
398406
value: leftArray ? leftArray[movedFromIndex] : undefined,
399407
}
400408
: undefined,
401-
isLast,
409+
// is this the last key in this delta?
410+
leftIndex === leftLength - 1 &&
411+
rightIndex === rightLength - 1 &&
412+
!(`${rightIndex + 1}` in arrayDelta),
402413
);
403414
}
404415

@@ -446,9 +457,7 @@ abstract class BaseFormatter<
446457
parseTextDiff(value: string) {
447458
const output = [];
448459
const lines = value.split("\n@@ ");
449-
// for (let i = 0, l = lines.length; i < l; i++) {
450460
for (const line of lines) {
451-
//const line = lines[i];
452461
const lineOutput: {
453462
pieces: LineOutputPiece[];
454463
location?: LineOutputLocation;

0 commit comments

Comments
 (0)