Skip to content

Commit 7ed9b4b

Browse files
committed
fix: formatters isLast flag
1 parent eb3a8cd commit 7ed9b4b

1 file changed

Lines changed: 22 additions & 18 deletions

File tree

  • packages/jsondiffpatch/src/formatters

packages/jsondiffpatch/src/formatters/base.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,21 @@ abstract class BaseFormatter<
305305
}, 0) + 1;
306306
let rightLength = leftLength;
307307

308+
// call fn with previous args, to catch last call and set isLast=true
309+
let previousFnArgs: Parameters<typeof fn> | undefined;
310+
const addKey: typeof fn = (...args) => {
311+
if (previousFnArgs) {
312+
fn(...previousFnArgs);
313+
}
314+
previousFnArgs = args;
315+
};
316+
const flushLastKey = () => {
317+
if (!previousFnArgs) {
318+
return;
319+
}
320+
fn(previousFnArgs[0], previousFnArgs[1], previousFnArgs[2], true);
321+
};
322+
308323
while (
309324
leftIndex < leftLength ||
310325
rightIndex < rightLength ||
@@ -322,7 +337,7 @@ abstract class BaseFormatter<
322337
// something happened to the left item at this position
323338
hasDelta = true;
324339
const itemDelta = arrayDelta[leftIndexKey];
325-
fn(
340+
addKey(
326341
leftIndexKey,
327342
movedFromIndex ?? leftIndex,
328343
movedFromIndex
@@ -331,12 +346,7 @@ abstract class BaseFormatter<
331346
value: leftArray ? leftArray[movedFromIndex] : undefined,
332347
}
333348
: undefined,
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),
349+
false,
340350
);
341351

342352
if (Array.isArray(itemDelta)) {
@@ -361,7 +371,7 @@ abstract class BaseFormatter<
361371
hasDelta = true;
362372
const itemDelta = arrayDelta[rightIndexKey];
363373
const isItemAdded = Array.isArray(itemDelta) && itemDelta.length === 1;
364-
fn(
374+
addKey(
365375
rightIndexKey,
366376
movedFromIndex ?? leftIndex,
367377
movedFromIndex
@@ -370,11 +380,7 @@ abstract class BaseFormatter<
370380
value: leftArray ? leftArray[movedFromIndex] : undefined,
371381
}
372382
: undefined,
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),
383+
false,
378384
);
379385

380386
if (isItemAdded) {
@@ -397,7 +403,7 @@ abstract class BaseFormatter<
397403
this.includeMoveDestinations !== false
398404
) {
399405
// show unchanged items only if we have the left array
400-
fn(
406+
addKey(
401407
rightIndexKey,
402408
movedFromIndex ?? leftIndex,
403409
movedFromIndex
@@ -406,10 +412,7 @@ abstract class BaseFormatter<
406412
value: leftArray ? leftArray[movedFromIndex] : undefined,
407413
}
408414
: undefined,
409-
// is this the last key in this delta?
410-
leftIndex === leftLength - 1 &&
411-
rightIndex === rightLength - 1 &&
412-
!(`${rightIndex + 1}` in arrayDelta),
415+
false,
413416
);
414417
}
415418

@@ -423,6 +426,7 @@ abstract class BaseFormatter<
423426
}
424427
}
425428
}
429+
flushLastKey();
426430
}
427431

428432
getDeltaType(delta: Delta, movedFrom?: MoveDestination | undefined) {

0 commit comments

Comments
 (0)