Skip to content

Commit

Permalink
fix: fix webapp crash when parameter list contains a nullish element
Browse files Browse the repository at this point in the history
- remove nullish elements from arrays when using mergeArraysByElementsIds
- mergeArraysByElementsIds no longer modify the input arrays
  • Loading branch information
csm-thu committed Mar 18, 2024
1 parent 4f2ec25 commit 4bdc4de
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/utils/ArrayDictUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ const mergeArraysByElementsIds = (array1, array2) => {
if (array2 == null) return array1;

array2.forEach((el2) => {
const indexToPatch = array1.findIndex((el1) => el1.id === el2.id);
if (indexToPatch !== -1) {
array1[indexToPatch] = merge(array1[indexToPatch], el2);
} else {
array1.push(el2);
}
const indexToPatch = array1.findIndex((el1) => el1?.id === el2?.id);
if (el2 != null && indexToPatch !== -1) array1[indexToPatch] = merge(array1[indexToPatch], el2);
else array1.push(el2);
});

return array1;
Expand Down
4 changes: 4 additions & 0 deletions src/utils/__test__/ArrayDictUtils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ describe('mergeArraysByElementsIds', () => {
{ id: 'id1', value: 'value1', nested: { 1: 1, 2: 2 } },
{ id: 'id2', value: 'value2' },
{ id: 'id3', value: 'value3' },
null,
];
const overridingArray = [
{ id: 'id1', value: 'newValue1', nested: { 2: 'two', 3: 3 } },
{ id: 'id2', newAttribute: 'attribute2' },
{ id: 'id4', value: 'value4' },
undefined,
];
const mergedResult = [
{ id: 'id1', value: 'newValue1', nested: { 1: 1, 2: 'two', 3: 3 } },
{ id: 'id2', value: 'value2', newAttribute: 'attribute2' },
{ id: 'id3', value: 'value3' },
null,
{ id: 'id4', value: 'value4' },
undefined,
];

test.each`
Expand Down

0 comments on commit 4bdc4de

Please sign in to comment.