Skip to content

fix: added support for merging items into an existing array #27

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

Merged
merged 2 commits into from
Apr 2, 2024

Conversation

raing3
Copy link
Contributor

@raing3 raing3 commented Mar 20, 2024

PR makes the following changes:

  1. Object array items which are set with another object are merged. Previous behaviour was to do nothing. There is a comment indicating merging was perhaps being considered but the action was left unhandled.
  2. Previously the set function could clear items adjacent to the element being set. This feels undesirable as the set operation is altering part of the object outside of the path that is being targeted. That should no longer happen.

The changes don't break any of the existing tests which lead me to believe they just haven't been accomodated for. Below shows the newly added tests which fail when run against the code in the main branch.

  1) set
       array
         should merge into object nested in single-level array:

      AssertionError: expected [ { text: 'A' }, { text: 'B' } ] to deeply equal [ { text: 'A' }, …(1) ]
      + expected - actual

         {
           "text": "A"
         }
         {
      +    "additionalText": "C"
           "text": "B"
         }
       ]

      at Context.<anonymous> (test/unit/set.test.ts:166:36)
      at processImmediate (node:internal/timers:476:21)

  2) set
       array
         should merge into object nested in multi-level array:

      AssertionError: expected [ [ undefined, …(1) ] ] to deeply equal [ [ { text: 'A' }, …(1) ] ]
      + expected - actual

       [
         [
           {
      +      "text": "A"
      +    }
      +    {
             "additionalText": "C"
      +      "text": "B"
           }
         ]
       ]

      at Context.<anonymous> (test/unit/set.test.ts:195:36)
      at processImmediate (node:internal/timers:476:21)

  3) set
       array
         should replace into object nested in multi-level array:

      AssertionError: expected [ [ undefined, …(1) ] ] to deeply equal [ [ { text: 'A' }, …(1) ] ]
      + expected - actual

       [
         [
           {
      +      "text": "A"
      +    }
      +    {
             "additionalText": "C"
           }
         ]
       ]

      at Context.<anonymous> (test/unit/set.test.ts:205:36)
      at processImmediate (node:internal/timers:476:21)

  4) set
       array
         should insert into object nested in multi-level array:

      AssertionError: expected [ [ undefined, …(1) ], …(1) ] to deeply equal [ [ { text: 'A' }, …(2) ] ]
      + expected - actual

       [
         [
           {
      -      "additionalText": "C"
      +      "text": "A"
           }
      -  ]
      -  [
           {
      -      "text": "A"
      +      "additionalText": "C"
           }
           {
             "text": "B"
           }

      at Context.<anonymous> (test/unit/set.test.ts:215:36)
      at processImmediate (node:internal/timers:476:21)

Hopefully this can be accepted. Please let me know if anything more is needed to get this merged and released.

Thanks!

@sagold
Copy link
Owner

sagold commented Mar 20, 2024

Nice! I will find time soon to go through this MR. I appreciate and support your MR 👏

@sagold sagold merged commit d0b64ed into sagold:main Apr 2, 2024
@sagold
Copy link
Owner

sagold commented Apr 2, 2024

Your PR has been merged and the changes were published with @sagold/json-query@6.2.0.

Thank you for your contribution!

@raing3
Copy link
Contributor Author

raing3 commented Apr 2, 2024

Excellent, thanks very much @sagold!

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.

2 participants