query-string
query-string copied to clipboard
How to skip duplicate key in query string
Could not find a from to post this question in so posting it here. How can I skip a key that has already been parsed/added? Is there an option I can add to queryString.parse(location.search) to ignore duplicates?
It's not immediately clear to me exactly what you are asking. An example might go a long way.
For example
?utm_source=google&utm_medium=ppc&utm_campaign=brand&utm_source=google
As of now utm_source becomes an array but I would prefer to ignore the second utm_source
I think https://github.com/sindresorhus/query-string/issues/210 would solve this, as you could define that utc_source
is a string and it would only use the first value.
I'd also really like to see this for making TypeScript code more succinct. Maybe something like arrayFormat: 'ignore'
?
Current code looks like:
function doThingWithParam(param: string) {
// ...
}
// param type is string | string[] | null
let { param } = qs.parse(search);
if (Array.isArray(param)) {
param = param[0];
}
doThingWithParam(param);
ideally it'd be something like
// param type is string | null
let { param } = qs.parse(search, { arrayFormat: 'ignore' });
doThingWithParam(param);
+1 on this