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] Prefix option for parse function #213

Merged
merged 1 commit into from Jun 13, 2017
Merged

[New] Prefix option for parse function #213

merged 1 commit into from Jun 13, 2017

Conversation

dlwalsh
Copy link
Contributor

@dlwalsh dlwalsh commented Jun 13, 2017

The primary motivation for this change is that react-router v4 returns the location.search property with a leading question mark. Whilst it's trivial to remove this, it's nicer still to have the library handle it.

Fixes #177.

@dlwalsh dlwalsh changed the title [New] prefix option [New] prefix option for parse function Jun 13, 2017
@dlwalsh dlwalsh changed the title [New] prefix option for parse function [New] Prefix option for parse function Jun 13, 2017
@dlwalsh
Copy link
Contributor Author

dlwalsh commented Jun 13, 2017

I also contemplated making prefix either a string or a regular expression, as with delimiter. But I was unsure of the merits of regex in this instance. Thoughts?

Copy link
Owner

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we want to support a generic prefix - what's the use case for anything but "?"?

lib/parse.js Outdated
strictNullHandling: false
};

var parseValues = function parseQueryStringValues(str, options) {
var obj = {};
var parts = str.split(options.delimiter, options.parameterLimit === Infinity ? undefined : options.parameterLimit);
var cleanStr = str.indexOf(options.prefix) === 0 ? str.substr(options.prefix.length) : str;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please never use substr; it's either nonstandard or deprecated. Always use slice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@dlwalsh
Copy link
Contributor Author

dlwalsh commented Jun 13, 2017

No particular use case for a generic prefix, other than my inclination towards a flexible API.

I'm happy to change it to a boolean option that would simply trim the leading ?. In that case, what should it be called? Perhaps ignoreQueryPrefix?

@ljharb
Copy link
Owner

ljharb commented Jun 13, 2017

I agree that your API is properly flexible :-) I think ignoreQueryPrefix might be simpler though, for this use case.

We might as well add an addQueryPrefix option to parse as well, so they can be symmetrical.

@dlwalsh
Copy link
Contributor Author

dlwalsh commented Jun 13, 2017

I've updated parse to use the ignoreQueryPrefix option.

I've tried to add addQueryPrefix to stringify. However, even the slightest change to this function throws complexity errors in the tests. It seems to me that stringify is in need of a more generic refactor.

Copy link
Owner

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parse changes LGTM. I'm fine deferring the stringify change til later, but you can feel free to just bump the complexity threshold in .eslintrc too :-)

lib/parse.js Outdated
@@ -18,7 +18,9 @@ var defaults = {

var parseValues = function parseQueryStringValues(str, options) {
var obj = {};
var parts = str.split(options.delimiter, options.parameterLimit === Infinity ? undefined : options.parameterLimit);
var cleanStr = options.ignoreQueryPrefix && str.slice(0, 1) === '?' ? str.slice(1) : str;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

str.charAt(0) === '?' might be cleaner than the slice(0, 1) here - alternatively, options.ignoreQueryPrefix ? str.replace(/^\?/, '') : str?

Also, please parenthesize the ternary conditional since there's multiple items.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@dlwalsh
Copy link
Contributor Author

dlwalsh commented Jun 13, 2017

I've modified parse as requested. I've also added addQueryPrefix to stringify, with some changes to .eslintrc.

Copy link
Owner

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks great!

@robcalcroft
Copy link

What version of qs will have this feature as the current NPM release doesn't?

@shri3k
Copy link

shri3k commented Jun 20, 2017

^would like to know this as well.

@ljharb
Copy link
Owner

ljharb commented Jun 20, 2017

Whatever the next one is. This is a minor, so it'll be in the next release that a proper ^ range covers.

t.test('adds query prefix', function (st) {
st.equal(qs.stringify({ a: 'b' }, { addQueryPrefix: true }), '?a=b');
st.end();
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, regarding the new addQueryPrefix option, which is wonderful news, I noticed the following situation:

> qs.stringify({ a: 1, b: 2}, { addQueryPrefix: true })
'?a=1&b=2'
> qs.stringify({}, { addQueryPrefix: true})
'?'

In my use case I don't want to keep the lonely ? if I'm stringifying an empty object.

What do you think about it?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems like a reasonable modification - I haven't released the feature yet, so can you get a PR up asap for that?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in #217, thanks!

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

Successfully merging this pull request may close these issues.

None yet

5 participants