adobe-client-data-layer icon indicating copy to clipboard operation
adobe-client-data-layer copied to clipboard

Clear Arrays when pushing an Array

Open nikoroz opened this issue 1 year ago • 0 comments

Description

  • Add an if-statement in the customizer-function to check if srcValue is a typeof Array.
  • Use Array.map to convert all values that are undefined or null to null.
    • One could also directly filter out all undefined or null values using Array.filter as the end result is the same but I decided on Array.map in order to mimic the behavior of the existing if-statement above the new code.
  • 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 Array to an existing Array-property merges the new and existing Array's together.

  • Mitigates the need to explicitly assign a null value to an existing Array-property before pushing a new Array.

  • Allows for clearing Array's by pushing an empty Array.

How Has This Been Tested?

  • npm run test in forked repository workspace.
  • npm run dev in forked repository and testing using the browser console on the provided localhost test page.
    • Tested pushing new Array-property to existing Array-property.
    • Tested pushing new Array-property to existing Array-property nested in another Object.

Screenshots

npm run test

image

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.

nikoroz avatar Apr 29 '24 16:04 nikoroz