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.

When parsing with `comma: true`, params with a single or no value return a string rather than as an array. Is there an option to always return an array?

See original GitHub issue

I looked at the docs and issues but I am not sure if there is an option built into qs that could do this.

Given a query string like:

?a=1&b=2,3&c=

When parsing with comma: true, the result is:

{
  a: "1",
  b: ["2", "3"]
  c: ""
}

What I want is a and c to be an array format instead. Is there an option to always return an array when parsing?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
TimeDropsSBcommented, Nov 15, 2019

In my use case, yes, I want everything as an array because, using the query example I mentioned, a and c do take comma-separated values. Therefore, having keys which are all normalized to the same type - string[] - makes data manipulation ib the Javascript side much easier. All of this, of course, with the necessity that none of the params are suffixed with [].

Ultimately, what I was hoping was that qs had a built-in option to simply convert each one to a string array so that I don’t have to do the normalizing myself. Given your answer, I think it makes sense why qs wouldn’t have a built-in method for such.

0reactions
ljharbcommented, Nov 15, 2019

You can do [].concat(arrayOrString) whenever you need to ensure you have an array, but yes i think you need to do that normalization yourself.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I convert a comma-separated string to an array?
If you intend to use the elements of your array as integers and not as strings after splitting the string, consider converting them...
Read more >
Trailing commas - JavaScript - MDN Web Docs
JavaScript allows trailing commas wherever a comma-separated list of values is accepted and more values may be expected after the last item.
Read more >
Parameter Serialization - Swagger
OpenAPI 3.0 supports arrays and objects in operation parameters (path, ... Header parameters always use the simple style, that is, comma-separated values.
Read more >
Parsing arguments and building values — Python 3.11.1 ...
The keywords argument is a NULL -terminated array of keyword parameter names. Empty names denote positional-only parameters. Returns true on success; on failure ......
Read more >
ARRAYTOTEXT function - Microsoft Support
Syntax · array. The array to return as text. Required. · format. The format of the returned data. Optional. It can be one...
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