question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parsing of `arrayFormat: 'comma'` not working for queries of single value with comma

See original GitHub issue

Say we have an Object we are stringifying to become a query param of the following shape (notice the comma in the value)

  1. Create an object with its value as a single string / array of one string.
const singleQueryObject = { not_important: [ "I, am, one, single, value"]};

// OR (the value can also just be a plain string, not an array like the above)

const singleQueryObject = { not_important: "I, am, one, single, value" };
  1. We stringify it:
const stringifyResult = queryString.stringify(singleQueryObject, { arrayFormat: 'comma' });
//=>  'not_important=I%2C%20am%2C%20one%2C%20single%2C%20value'
  1. We parse it (Actual)
const parsedResult = queryString.parse(stringifyResult, { arrayFormat: 'comma' });
//=> { not_important: [ 'I', ' am', ' one', ' single', ' value' ] }

(Expected)

{ not_important: [ 'I, am, one, single, value' ] };

Work around (?)

This bug only occurs when there is a single value. So say if we run the same step as above with this object instead:

const singleQueryObject = { not_important: [ 'I, am, one, single, value', 'It works!'] };
const stringifyResult = queryString.stringify(singleQueryObject, { arrayFormat: 'comma' });
const parsedResult = queryString.parse(stringifyResult, { arrayFormat: 'comma' });

//=> { not_important: [ 'I, am, one, single, value', 'It works!' ] }

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
cainlevycommented, Jan 12, 2021

The current parse API doesn’t appear to have enough information to solve this problem.

One solution might be an option to encode array-ness into the parameter key during stringify, resulting in something like myArray[]=a,b,c. I could imagine this gracefully degrading for backwards compatible if the development philosophy requires it.

2reactions
Yashkochar20commented, Dec 7, 2020

@sindresorhus @mishugana any update on this ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Output array as comma separated with querySelector
I found a second argument arrayFormat: comma but even that did not work. I expect because it's an array inside an object.
Read more >
Parsing comma separated values - Microsoft Community Hub
Parsing comma separated values ... I'm trying to use the parse command to extract that data into new columns but ... Still trying...
Read more >
IO tools (text, CSV, HDF5, …) — pandas 1.5.2 documentation
Whether or not to include the default NaN values when parsing the data. ... delimited (not necessarily comma-separated) files, as pandas uses the...
Read more >
query-string - npm.io
'comma' : Parse arrays with elements separated by comma: const queryString = require('query-string'); queryString.parse('foo=1,2,3', {arrayFormat: 'comma'}) ...
Read more >
Array formulas and functions in Excel - examples and guidelines
Not only can an array formula deal with several values ... you separate each row by a semicolon and each column of data...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found