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

stringify, request supporting 'dot' as option for arrayFormat #516

Open
msg558 opened this issue Nov 1, 2024 · 3 comments
Open

stringify, request supporting 'dot' as option for arrayFormat #516

msg558 opened this issue Nov 1, 2024 · 3 comments

Comments

@msg558
Copy link

msg558 commented Nov 1, 2024

parse can successfully convert 'a.0.name=test&a.0.number=5&a.1.name=test2&a.1.number=6' to:

{ a: [ { name: 'test', number: '5'}, { name: 'test2', number: '6' }] }

so it would be great if there were a stringify option that could convert the object back into the original dot notation.

@ljharb
Copy link
Owner

ljharb commented Nov 1, 2024

There is allowDots:

qs.stringify({ a: [ { name: 'test', number: '5'}, { name: 'test2', number: '6' }] }, { encodeValuesOnly: true, allowDots: true  })
// 'a[0].name=test&a[0].number=5&a[1].name=test2&a[1].number=6'

which is pretty close! other than just being able to round trip (which is important), can you explain why it's important to be able to use dots for the numbers?

@msg558
Copy link
Author

msg558 commented Nov 4, 2024

I am using URL parameters to inform my web-app to perform a stack of commands:

?c.0.id='command1'&c.0.value=5&c.1.id='command2'&c.1.value=test

do command 1 with some value of 5, then do command using some value of 'test'...etc

For this context, using a dot instead of square brackets does a couple things for me:

  • doesn't get encoded, so it's more readable by the user or for debugging
  • reduces the number of characters in the URL, which allows for potentially more commands

As previously stated, the parsing of this format already works, but I am interested in a UI to help users generate these 'macros' and being able to round-trip would be really helpful for that.

@ljharb
Copy link
Owner

ljharb commented Nov 4, 2024

Seems like we need a stringify option allowDotsForNumbers, that throws if allowDots is present and not set to true.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants