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

Feature request: depth: false or parseDepth: false for qs.parse #261

Closed
leoyli opened this issue May 13, 2018 · 6 comments · Fixed by #326
Closed

Feature request: depth: false or parseDepth: false for qs.parse #261

leoyli opened this issue May 13, 2018 · 6 comments · Fixed by #326

Comments

@leoyli
Copy link

leoyli commented May 13, 2018

Similar to the relationship arrayLimit: 0 and parseArrays: false:

qs.parse('a[0]=na', { arrayLimit: 0 }) === { a: [ 'na' ] }
qs.parse('a[0]=na', { parseArrays: false }) === { a: { '0': 'na' } }

Therefore for this:

qs.parse('a[i]=na', { depth: 1 }) === { a: { i: 'na' } }
qs.parse('a[i]=na', { depth: 0 }) === { a: { '[i]': 'na' } }

I believe in some scenario, people may want to suppress parse into any depth, i.e.:

qs.parse('a[]=na', { depth: false }) === { 'a[0]': 'na' }  // proposed behaviour

so that we can expect for any query key, its value will never be an object since they are all decoupled,

qs.parse('a=A&a[]=na', { depth: false }) === { 'a': 'A', 'a[0]': 'na' }  // proposed behaviour

Any opinion?

@ljharb
Copy link
Owner

ljharb commented May 13, 2018

This seems like a duplicate of #246?

@leoyli
Copy link
Author

leoyli commented May 13, 2018

@ljharb I read that. It actually not a duplicated since this request added new option instead of modified the current behaviour. And Indeed depth: 0 makes no sense to me, but it is consisted with the current logic, and remains the flexibility, which could be a case for people to choose. Also I think added false option should completely turn the depth parsing off, but 0 should behave literally 0depth.

@ljharb
Copy link
Owner

ljharb commented May 14, 2018

qs.parse('a[i]=na', { depth: 0 }) === { a: { '[i]': 'na' } } seems weird to me - zero depth, to me, seems like it would be { 'a[0]': 'na' } .

In other words, I don't think { a: { '[i]': 'na' } } ever makes sense (although I don't want to make a breaking change if I can avoid it)

@leoyli
Copy link
Author

leoyli commented May 14, 2018

+1, I thought it were working like this way { 'a[0]': 'na' } when I was trying to configuring the parser function in my express app. So I'm wondering if it should be fixed like a bug, or, like what I suggested, introducing another option and have no breaking changes.

@ljharb
Copy link
Owner

ljharb commented May 14, 2018

There's no tests for a depth of 0, at least. Only 1, 5000, and the default of 5.

That said, maybe it'd be better to add support for false, and then in the next semver-major, make 0 work like false.

@rentalhost
Copy link

rentalhost commented Jul 31, 2018

Just to who needs that it works like this report expectative:

// Input:         { a: 1, b: { c: 2 } }
// Output (spaced): a = 1 & b[c] = 2 
// Note: using lodash.
_.mapKeys(qs.parse(qs.stringify({ data: postData }), { depth: 0 }).data, (v, key) => {
    return key.replace(/^\[(\w+)]/, '$1');
})

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

Successfully merging a pull request may close this issue.

3 participants