Skip to content

Preserves the order of inserted field values#41

Merged
AugmenTab merged 2 commits intomainfrom
tbaum/preserve-field-value-order
Jan 8, 2026
Merged

Preserves the order of inserted field values#41
AugmenTab merged 2 commits intomainfrom
tbaum/preserve-field-value-order

Conversation

@AugmenTab
Copy link
Contributor

When a form includes multiple fields with the same name (and thus multiple values for that field), we were inserting them with Map.insertWith (<>). The order of arguments to Map.insertWith is the new value first, followed by the old value, so we were building the list in reverse order while merging. This corrects to building the list using the same order as in the request, and includes a simple test to verify.

When a form includes multiple fields with the same name (and thus
multiple values for that field), we were inserting them with
`Map.insertWith (<>)`. The order of arguments to `Map.insertWith`
is the new value first, followed by the old value, so we were
building the list in reverse order while merging. This corrects to
building the list using the same order as in the request, and
includes a simple test to verify.
@onslaughtq
Copy link
Member

I'm curious as to why the order matters, and if maybe we should have functions that don't care about the order and ones that do with appropriate names.

@OwenGraves
Copy link

So is appendField just flip (<>)? Not sure which definition is clearer

I think this is a good change, curious what caused you to notice it or if it was causing issues somewhere

@AugmenTab
Copy link
Contributor Author

To answer both questions, I noticed it and am attempting to fix it because I was playing around with this HTMX/Sortable.js widget. The order matters to preserve the user's sorting of elements.

appendField is indeed just flip (<>). I implemented it with explicit named arguments rather than flip since we seem to prefer not using flip whenever possible.

As far as having separate functions, I wouldn't be opposed to it, but I would assume that either a user would want the order preserved or wouldn't care, at which point we might as well preserve the order since (<>) and appendField/flip (<>) are the same Big O.

@onslaughtq
Copy link
Member

I think using flip is just fine here. Our main aversion to flip is using it when we control the functions and can easily swap the order of parameters, instead of using an unnecessary flip.

To answer both questions, I noticed it and am attempting to fix it because I was playing around with this HTMX/Sortable.js widget. The order matters to preserve the user's sorting of elements.

appendField is indeed just flip (<>). I implemented it with explicit named arguments rather than flip since we seem to prefer not using flip whenever possible.

As far as having separate functions, I wouldn't be opposed to it, but I would assume that either a user would want the order preserved or wouldn't care, at which point we might as well preserve the order since (<>) and appendField/flip (<>) are the same Big O.

@AugmenTab AugmenTab merged commit 1510843 into main Jan 8, 2026
5 checks passed
@AugmenTab AugmenTab deleted the tbaum/preserve-field-value-order branch January 8, 2026 02:46
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

Comments