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

New feature : Option to add leading "?" #205

Closed
nitinsh99 opened this issue Aug 29, 2019 · 7 comments · Fixed by #217
Closed

New feature : Option to add leading "?" #205

nitinsh99 opened this issue Aug 29, 2019 · 7 comments · Fixed by #217

Comments

@nitinsh99
Copy link

nitinsh99 commented Aug 29, 2019

Would you be open to PR for adding following option?

Example:

let params = {
"foo": bar
}
const url = `http://abc.com/xyz${stringify(params,{separator:true}})`
//prints http://abc.com/xyz?foo=bar

with falsy params

let params = undefined;
const url = `http://abc.com/xyz${stringify(params,{seperator:true})`
//prints http://abc.com/xyz

Rationale:
Avoid ternary checks like this everywhere:

const url = params? 'http://abc.com/xyz?${stringify(params)' : 'http://abc.com/xyz'

@nitinsh99 nitinsh99 changed the title Option to add leading "?" New feature : Option to add leading "?" Aug 29, 2019
@sindresorhus
Copy link
Owner

I think it would be better to add a method that does the inverse of .parseUrl().

@afilp
Copy link

afilp commented Sep 24, 2019

+1

I had exactly the same use case: Avoid using ternaries.

@komkanit
Copy link
Contributor

komkanit commented Oct 8, 2019

@sindresorhus Can I try to fix this? I'm thinking of creating a method named parseObject that you parse result of .parseUrl to receive new url

Example

const obj = {url: 'https://foo.bar', query: {foo: 'bar'}}
queryString.parseObject(obj)
//=> 'https://foo.bar?foo=bar'

@sindresorhus
Copy link
Owner

@komcal Sure.parseObject is not a good name. You don't parse an object. The name should be stringifyUrl.

@komkanit
Copy link
Contributor

komkanit commented Oct 8, 2019

const obj = {url: 'https://foo.bar', query: {foo: 'bar'}}
queryString.stringifyUrl(obj)
//=> 'https://foo.bar?foo=bar'

@sindresorhus Is this ok?

@sindresorhus
Copy link
Owner

Yes

@imaegoo
Copy link

imaegoo commented Nov 29, 2019

+1

My case:

const obj = {url: 'https://foo.bar', query: {foo: ['bar', 'baz']}}
const options = {arrayFormat: 'bracket'}
queryString.stringifyUrl(obj, options)
//=> 'https://foo.bar?foo[]=bar&foo[]=baz'

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.

5 participants