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

How to skip duplicate key in query string #293

Open
kingpinzs opened this issue Nov 9, 2020 · 5 comments
Open

How to skip duplicate key in query string #293

kingpinzs opened this issue Nov 9, 2020 · 5 comments

Comments

@kingpinzs
Copy link

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?

@sindresorhus
Copy link
Owner

It's not immediately clear to me exactly what you are asking. An example might go a long way.

@kingpinzs
Copy link
Author

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

@sindresorhus
Copy link
Owner

I think #210 would solve this, as you could define that utc_source is a string and it would only use the first value.

@wbobeirne
Copy link

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);

@nicovigil1
Copy link

+1 on this

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

No branches or pull requests

4 participants