From 4bdc4dea6daae61bf7ae505df17b14eaad64aadf Mon Sep 17 00:00:00 2001 From: Tristan Huet Date: Fri, 15 Mar 2024 11:33:26 +0100 Subject: [PATCH] fix: fix webapp crash when parameter list contains a nullish element - remove nullish elements from arrays when using mergeArraysByElementsIds - mergeArraysByElementsIds no longer modify the input arrays --- src/utils/ArrayDictUtils.js | 9 +++------ src/utils/__test__/ArrayDictUtils.spec.js | 4 ++++ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/utils/ArrayDictUtils.js b/src/utils/ArrayDictUtils.js index 117fa9425..f8b0a92c5 100644 --- a/src/utils/ArrayDictUtils.js +++ b/src/utils/ArrayDictUtils.js @@ -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; diff --git a/src/utils/__test__/ArrayDictUtils.spec.js b/src/utils/__test__/ArrayDictUtils.spec.js index a91b344a8..36e7c177d 100644 --- a/src/utils/__test__/ArrayDictUtils.spec.js +++ b/src/utils/__test__/ArrayDictUtils.spec.js @@ -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`