ljharb/qs

Is there an option to prevent value of the same key being populating into an array?

Open

#259 aperta il 13 mag 2018

Vedi su GitHub
 (10 commenti) (6 reazioni) (0 assegnatari)JavaScript (744 fork)batch import
feature requesthelp wantedparse

Metriche repository

Star
 (8015 star)
Metriche merge PR
 (Merge medio 6g 22h) (2 PR mergiate in 30 g)

Descrizione

Hi there,

I'm trying to figure out if qs.parse allow us to update the query key value instead of collecting those as an array from the doc, but I can not find anything about that so I'm asking here.

By default,

qs.parse('?num=1&num=2') === { num: ['1', '2'] };

Instead, I just care about the last value assigned to num in the query. i.e.

qs.parse('?num=1&num=2', { some_option: true }) === { num: '2' };

Is there a some_option to do so?

I'm working on an API and want a consistent way to handle the value. It is annoying to check if the query value is an array for a given key every single time (and it can be an object sometimes)... and at this point I decided to ignore all other value but the latest bound to the key.

But I guess people may also want only the first key value is preserved?! (e.g. that is how StarkOverflow handled their duplicated query key.)

Thanks!

Guida contributor