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

provide some kind of defaultDecode as a fallback for decoder option #206

Closed
seansfkelley opened this issue Apr 11, 2017 · 2 comments
Closed

Comments

@seansfkelley
Copy link

seansfkelley commented Apr 11, 2017

I ran into an issue recently where I wanted to parse out boolean values to a proper boolean type:

const PARSE_OPTIONS = {
  decoder: value => {
    if (value === 'false') {
      return false;
    } else if (value === 'true') {
      return true;
    } else {
      return value;
    }
  }
};

Turns out this breaks parsing of URI-encoded array types:

> qs.parse("foo%5B%5D=bar", {});
{ "foo": [ "bar" ] }

> qs.parse("foo%5B%5D=bar", PARSE_OPTIONS);
{ "foo%5B%5D": "bar" }

Which I guess is reasonable (though, aside, the decoder API is not well-documented and has a lot of tricky subtleties like this). I wanted the last branch to just "do the default thing", but unfortunately that option is not available to me, so I'm stuck choosing between explicitly importing the private utils file and hoping it doesn't change or move, or re-implementing the default and hoping it doesn't change (which it looks like it might).

Providing a default implementation, even for this simple function, would make this use-case much more robust.

@ljharb
Copy link
Owner

ljharb commented Apr 11, 2017

One possibility might be changing the encoder/decoder calls to pass the default encoder/decoder as the second argument to the function - although that might be breaking if anyone's checking arguments.length.

@seansfkelley
Copy link
Author

I would be okay with that solution as well. It'd keep everything in one place, too.

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

No branches or pull requests

2 participants