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

Fix #119: Fix constructing more complex object with arrays #126

Merged
merged 2 commits into from
Jan 5, 2025

Conversation

meex28
Copy link
Contributor

@meex28 meex28 commented Dec 23, 2024

Fixing the bug #119

Description

Fixed incorrect array handling when combining complex hx-vals with json-enc. Previously, when processing:
<button hx-ext="json-enc" hx-vals='{"Id":1,"Items":["abc","xyz"]}'> ...</button>

The encodeParameters function gets as input FormData object like:

  • Id: '1'
  • Items: "abc"
  • Items: "xyz"

When retrieving types from hx-vals, for each entry with an Items key, whole array was added to the result object:

{
    Id: 1,
    Items: [ ["abc", "xyz"], ["abc", "xyz"] ]
}

Instead of:

{
    Id: 1,
    Items: ["abc", "xyz"]
}

To change this behaviour, the function now first groups FormData parameters by keys, and then maps raw values to types from hx-vals.

Testing

In the test handles hx-vals properly added an array-type value ('array': ['text', 123, {'key': 'value'}]) and expects it to to be properly included in the body.

Checklist

  • I have read the contribution guidelines
  • I ran the test suite locally (npm run test) and verified that it succeeded

…revent duplicating arrays elements in final object
Copy link

netlify bot commented Dec 23, 2024

Deploy Preview for htmx-extensions canceled.

Name Link
🔨 Latest commit ef7073c
🔍 Latest deploy log https://app.netlify.com/sites/htmx-extensions/deploys/677a41cbbb1c720008cad92e

@meex28 meex28 changed the title CLOSES #119: Fix constructing more complex object with arrays Fix #119: Fix constructing more complex object with arrays Dec 23, 2024
@Telroshan
Copy link
Collaborator

Hey, thanks for your work @meex28 !
While I understand the desire to refactor with a more modern JS syntax, I'm not convinced it's necessary to introduce so many tricks (array from, reduce, map, from entries) when all that's needed is to split the overriding with hx-vals/hx-vars values, to a second iteration instead of a value-by-value basis in a single loop.

Hence why I took the liberty to simplify your code to reduce the diff compared to the previous code, I hope you understand!
The idea is that the more the new code looks like the previous one, the easier it is to compare & review (as the diff is smaller).
To each their own of course, but I find a good old foreach way easier to review & process mentally rather than a chain of array/object callback-based functions

Thanks again for your work & patience!

@Telroshan Telroshan merged commit b0f44b2 into bigskysoftware:main Jan 5, 2025
5 checks passed
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.

3 participants