Skip to content

arrayFormat for parse#329

Draft
dreyks wants to merge 1 commit into
ljharb:mainfrom
dreyks:parse-array-format
Draft

arrayFormat for parse#329
dreyks wants to merge 1 commit into
ljharb:mainfrom
dreyks:parse-array-format

Conversation

@dreyks

@dreyks dreyks commented Sep 6, 2019

Copy link
Copy Markdown
Contributor

added basic tests. without arrayFormat parse's behavior does not change

don't think we want to allow specifying several formats simultaneously?

@ljharb

ljharb commented Sep 8, 2019

Copy link
Copy Markdown
Owner

Are these tests expected to pass as-is, or fail?

@dreyks

dreyks commented Sep 8, 2019

Copy link
Copy Markdown
Contributor Author

they’re expected to fail. i just wanted to be sure that this is the behavior we want. if you’re ok with it - i’ll proceed to the implementation

@ljharb

ljharb commented Sep 8, 2019

Copy link
Copy Markdown
Owner

@dreyks i think it would be helpful to have two commits - one with passing tests, and another with the tests that fail. It makes it much easier for me to see what's breaking or not, and what requires a new option or not.

@dreyks

dreyks commented Sep 8, 2019

Copy link
Copy Markdown
Contributor Author

oh. gotcha. ok will do

@dreyks dreyks force-pushed the parse-array-format branch from a2bd7e1 to 136b204 Compare September 8, 2019 07:28
@dreyks

dreyks commented Sep 8, 2019

Copy link
Copy Markdown
Contributor Author

done

Comment thread test/parse.js

t.test('arrayFormat: brackets allows only explicit arrays', function (st) {
st.deepEqual(qs.parse('a[]=b&a[]=c', { arrayFormat: 'brackets' }), { a: ['b', 'c'] });
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayFormat: 'brackets' }), { 'a[0]': 'b', 'a[1]': 'c' });

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see how this seems correct, because it's currently set to brackets but is parsing indices.

However, the current behavior matches what PHP, Rails, and express all do, so I'm not sure it should change.

@dreyks dreyks Sep 21, 2019

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, i'm not very sure on this one too, though you have to explicitly state you want this behavior so maybe it's ok

alternatively allowing arrayFormat to be an array itself could solve it

t.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayFormat: ['brackets', 'indices'] }), { 'a': ['b',  'c'] });
t.deepEqual(qs.parse('a[]=b&a[]=c', { arrayFormat: ['brackets', 'indices'] }), { 'a': ['b',  'c'] });

Comment thread test/parse.js
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayFormat: 'brackets' }), { 'a[0]': 'b', 'a[1]': 'c' });
st.deepEqual(qs.parse('a=b,c', { arrayFormat: 'brackets' }), { a: 'b,c' });
st.deepEqual(qs.parse('a=b&a=c', { arrayFormat: 'brackets' }), { a: ['b', 'c'] });
st.deepEqual(qs.parse('a=b&a=c', { arrayFormat: 'brackets' }), { a: 'c' });

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see how this seems correct, because it's currently set to brackets but is parsing as repeat.

However, the current behavior matches what PHP, Rails, and express all do, so I'm not sure it should change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rails and PHP actually parse it as overwriting

this is the reason I've started all this: in my rails app I've switched from "chaos and anarchy " to qs and got one spec failure where the suite was appending params and with qs that lead to an array

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, thanks for correcting me then. i'll think on this one.

@AlanKnightly

This comment was marked as spam.

@ljharb

This comment was marked as resolved.

@ljharb

ljharb commented Sep 14, 2023

Copy link
Copy Markdown
Owner

@dreyks ok, so, here's how i think this needs to work:

  • arrayFormat must take all the options stringify does - brackets, indices, repeat, comma - and produce an object that, when re-stringified with the same format, produces the original parse input (ie, roundtripping)
  • The current behavior must be described by one of the arrayFormat options. if none of the 4 formats above match, we may need to make a 5th one.

One important goal is that the default behavior of Rails, PHP, and express should all be expressible with an option - ideally the same one, and I suspect that's "brackets".

It'd be great to get this revived :-)

@dreyks

dreyks commented Sep 15, 2023

Copy link
Copy Markdown
Contributor Author

oof, lemme recollect what's going on here, it's been 4 years and not the easiest ones :D

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants