Skip to content

Commit

Permalink
fix(compass-crud): in the bulk update preview, convert array indexes …
Browse files Browse the repository at this point in the history
…from strings to numbers COMPASS-8218 (#6161)

* convert array indexes from strings to numbers

* the indexes after the removed one don't need adjusting
  • Loading branch information
lerouxb authored Aug 28, 2024
1 parent fc7d817 commit cf49c7f
Show file tree
Hide file tree
Showing 15 changed files with 1,101 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ function itemsWithChanges({
assert(delta._t === 'a', 'delta._t is not a');
const toRemove = Object.keys(delta)
.filter((key) => key.startsWith('_') && key !== '_t')
.map((key) => key.slice(1) as unknown as number);
.map((key) => parseInt(key.slice(1), 10));

// Removed indexes refer to the original (left) which is why we remove in a
// separate pass before updating/adding
Expand All @@ -404,13 +404,6 @@ function itemsWithChanges({
} else {
assert(false, `item with index "${index}" does not exist`);
}

// adjust the indexes of all items after this one
for (const item of items) {
if (item.index > index) {
item.index = item.index - 1;
}
}
}

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

const index = _index as unknown as number;
const index = parseInt(_index, 10);
assert(Array.isArray(change), 'unexpected non-array');
assert(change.length !== 3, 'array moves are not supported');
assert(change.length !== 2, 'array changes are not supported'); // always add and remove
Expand Down
31 changes: 31 additions & 0 deletions packages/compass-crud/test/before-after-fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,37 @@ export const fixtureGroups: FixtureGroup[] = [
before: { foo: [0, { bar: 'baz' }] },
after: { foo: [0, { bar: 'bazz' }] },
},
{
name: 'many items',
before: {
foo: [
{ i: 0 },
{ i: 1 },
{ i: 2 },
{ i: 3 },
{ i: 4 },
{ i: 5 },
{ i: 6 },
{ i: 7 },
{ i: 8 },
{ i: 9 },
],
},
after: {
foo: [
{ i: 0, newField: 1 },
{ i: 1, newField: 1 },
{ i: 2, newField: 1 },
{ i: 3, newField: 1 },
{ i: 4, newField: 1 },
{ i: 5, newField: 1 },
{ i: 6, newField: 1 },
{ i: 7, newField: 1 },
{ i: 8, newField: 1 },
{ i: 9, newField: 1 },
],
},
},
],
},
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cf49c7f

Please sign in to comment.