Skip to content

Commit cf49c7f

Browse files
authored
fix(compass-crud): in the bulk update preview, convert array indexes from strings to numbers COMPASS-8218 (#6161)
* convert array indexes from strings to numbers * the indexes after the removed one don't need adjusting
1 parent fc7d817 commit cf49c7f

15 files changed

+1101
-42
lines changed

packages/compass-crud/src/components/change-view/unified-document.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ function itemsWithChanges({
391391
assert(delta._t === 'a', 'delta._t is not a');
392392
const toRemove = Object.keys(delta)
393393
.filter((key) => key.startsWith('_') && key !== '_t')
394-
.map((key) => key.slice(1) as unknown as number);
394+
.map((key) => parseInt(key.slice(1), 10));
395395

396396
// Removed indexes refer to the original (left) which is why we remove in a
397397
// separate pass before updating/adding
@@ -404,13 +404,6 @@ function itemsWithChanges({
404404
} else {
405405
assert(false, `item with index "${index}" does not exist`);
406406
}
407-
408-
// adjust the indexes of all items after this one
409-
for (const item of items) {
410-
if (item.index > index) {
411-
item.index = item.index - 1;
412-
}
413-
}
414407
}
415408

416409
for (const [_index, change] of Object.entries(delta)) {
@@ -421,7 +414,7 @@ function itemsWithChanges({
421414
// Non-removed indexes refer to the final (right) array which is why we
422415
// update/add in a separate pass after removing
423416

424-
const index = _index as unknown as number;
417+
const index = parseInt(_index, 10);
425418
assert(Array.isArray(change), 'unexpected non-array');
426419
assert(change.length !== 3, 'array moves are not supported');
427420
assert(change.length !== 2, 'array changes are not supported'); // always add and remove

packages/compass-crud/test/before-after-fixtures.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,37 @@ export const fixtureGroups: FixtureGroup[] = [
412412
before: { foo: [0, { bar: 'baz' }] },
413413
after: { foo: [0, { bar: 'bazz' }] },
414414
},
415+
{
416+
name: 'many items',
417+
before: {
418+
foo: [
419+
{ i: 0 },
420+
{ i: 1 },
421+
{ i: 2 },
422+
{ i: 3 },
423+
{ i: 4 },
424+
{ i: 5 },
425+
{ i: 6 },
426+
{ i: 7 },
427+
{ i: 8 },
428+
{ i: 9 },
429+
],
430+
},
431+
after: {
432+
foo: [
433+
{ i: 0, newField: 1 },
434+
{ i: 1, newField: 1 },
435+
{ i: 2, newField: 1 },
436+
{ i: 3, newField: 1 },
437+
{ i: 4, newField: 1 },
438+
{ i: 5, newField: 1 },
439+
{ i: 6, newField: 1 },
440+
{ i: 7, newField: 1 },
441+
{ i: 8, newField: 1 },
442+
{ i: 9, newField: 1 },
443+
],
444+
},
445+
},
415446
],
416447
},
417448
{

packages/compass-crud/test/fixture-results/all_types_all_types_changed.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-crud/test/fixture-results/array_changes_add_array_to_array.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-crud/test/fixture-results/array_changes_add_object_to_array.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-crud/test/fixture-results/array_changes_add_simple_value_to_array.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-crud/test/fixture-results/array_changes_remove_simple_value_from_array.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-crud/test/fixture-results/array_changes_simple_array.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-crud/test/fixture-results/nested_object_changes_nested_object_array_array_simple.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-crud/test/fixture-results/nested_object_changes_nested_object_array_simple.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)