Clear Arrays when pushing an Array
Description
- Add an
if-statement in thecustomizer-function to check ifsrcValueis atypeofArray. - Use
Array.mapto convert all values that areundefinedornulltonull.- One could also directly filter out all
undefinedornullvalues usingArray.filteras the end result is the same but I decided onArray.mapin order to mimic the behavior of the existingif-statement above the new code.
- One could also directly filter out all
- Return the new modified
Array.
With these changes, pushing an Array to an existing Array-property will completely override the existing Array instead of merging the two together. Pushing an empty Array will clear the existing Array.
Current Behavior
adobeDataLayer.getState('customArray')
// (5) [1, 2, 3, 4, 5]
adobeDataLayer.push({
"customArray": [6, 7, 8]
})
adobeDataLayer.getState('customArray')
// (5) [6, 7, 8, 4, 5]
adobeDataLayer.push({
"customArray": []
})
adobeDataLayer.getState('customArray')
// (5) [6, 7, 8, 4, 5]
New Behavior
adobeDataLayer.getState('customArray')
// (5) [1, 2, 3, 4, 5]
adobeDataLayer.push({
"customArray": [6, 7, 8]
})
adobeDataLayer.getState('customArray')
// (3) [6, 7, 8]
adobeDataLayer.push({
"customArray": []
})
adobeDataLayer.getState('customArray')
// []
Related Issue
- https://github.com/adobe/adobe-client-data-layer/issues/125
Motivation and Context
-
The documentation under Pushing an Object to Update an Existing Array states:
One important detail to clarify is what happens when pushing data to an already existing array: it will overwrite the data of the inner array.
This statement is contradictory to the current behavior.
-
It is unexpected behavior that assigning a new
Arrayto an existingArray-property merges the new and existingArray's together. -
Mitigates the need to explicitly assign a
nullvalue to an existingArray-property before pushing a newArray. -
Allows for clearing
Array's by pushing an emptyArray.
How Has This Been Tested?
-
npm run testin forked repository workspace. -
npm run devin forked repository and testing using the browser console on the providedlocalhosttest page.- Tested pushing new
Array-property to existingArray-property. - Tested pushing new
Array-property to existingArray-property nested in anotherObject.
- Tested pushing new
Screenshots
npm run test
Types of Changes
- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
Checklist:
- [x] I have signed the Adobe Open Source CLA.
- [x] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [x] I have read the CONTRIBUTING document.
- [ ] I have added tests to cover my changes.
- [x] All new and existing tests passed.