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

Parsing array that contains elements whose contain encoded pipe char (%7C instead of '|') #336

Open
ShimShum2021 opened this issue Dec 23, 2021 · 1 comment

Comments

@ShimShum2021
Copy link

ShimShum2021 commented Dec 23, 2021

Hello.
I am trying to parse an array of elements separated by the pipe character '|'.
The elements themselves might contain the pipe char, hence those should be encoded first (to %7C).
With array of 2 elements or above it works just fine.
However with array of one element it doesn't. My guess is that parse doesn't see explicitly pipe char, it decodes %7C into pipe char and creates array of 2 elements.
(A side note: this method with other special characters works just fine.)
please refer the following code example:

// begin of code
function TestQueryString(): void {
  const value1: string = encodeURIComponent('a|b'); 
  const twoElements = parse(`foo=${value1}|2`, {
    arrayFormat: 'separator',
    arrayFormatSeparator: '|',
  });
  console.log(twoElements); // Works as expected got: [ 'a|b', '2']

  const oneElement = parse(`bar=${value1}`, {
    arrayFormat: 'separator',
    arrayFormatSeparator: '|',
  });
  console.log(oneElement); // Here is the issue. Expected: 'a|b' but got [ 'a', 'b']
}
// end of code

Screenshot with '|'. Not expected behavior in one element case:
image

Screenshot with '%'. Expected behavior as another special characters:
image

Thanks,

@ShimShum2021 ShimShum2021 changed the title Parsing array that contains elements contains encoded pipe char Parsing array that contains elements whose contain encoded pipe char (%7C instead of '|') Dec 23, 2021
@sindresorhus
Copy link
Owner

// @jpoehnelt

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

2 participants