Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear Arrays when pushing an Array #158

Closed
wants to merge 1 commit into from
Closed

Clear Arrays when pushing an Array #158

wants to merge 1 commit into from

Conversation

ghost
Copy link

@ghost ghost commented Apr 29, 2024

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

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

  • 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:

  • I have signed the Adobe Open Source CLA.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@ghost ghost closed this by deleting the head repository Jul 1, 2024
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants