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

Issues parsing array arguments? #66

Open
maxguru opened this issue Mar 6, 2018 · 2 comments
Open

Issues parsing array arguments? #66

maxguru opened this issue Mar 6, 2018 · 2 comments

Comments

@maxguru
Copy link

maxguru commented Mar 6, 2018

Not sure if people consider this an issues, however, arrays are apparently padded with nulls for missing values,

> JSON.stringify(url('?','?t[0]=x&t[1]=y'));
"{"t":["x","y"]}"
> JSON.stringify(url('?','?t[0]=x&t[2]=y'));
"{"t":["x",null,"y"]}"

Maybe the better option would be to return objects for sparse arrays, like so,

> JSON.stringify(url('?','?t[0]=x&t[1]=y'));
"{"t":["x","y"]}"
> JSON.stringify(url('?','?t[0]=x&t[2]=y'));
"{"0":"x","2":"y"}"
@belden
Copy link

belden commented Feb 13, 2020

I think this is a complaint against how sparse arrays are represented in JSON, and not an issue with this library.

// an ordinary sparse array
var sparse = [0,,2];
console.log(`%o`, sparse); // [0, empty, 2]
console.log(JSON.stringify(sparse)); // [0, null, 2]

Notice that passing a sparse array through JSON.stringify results in information loss. These three different arrays can only be represented by one JSON object:

var sparse = [0,,2];
var undef = [0, undefined, 2];
var nully = [0, null, 2];

[sparse, undef, nully].forEach(a => console.log(JSON.stringify(a)));
// [0, null, 2]
// [0, null, 2]
// [0, null, 2]

Adding https://github.com/websanova/js-url into the mix isn't the source uncertainty. Representing a sparse array in JSON is the source of uncertainty.

var got = url('?','?t[0]=0&t[2]=2');
console.log(`%o`, got);    // {t: ["0", empty, "2"]}
console.log(JSON.stringify(got)); // '{"t": ["0", null, "2"]}'

I think js-url does not need to return a special sparse-array-object when it finds one, because it already returns properly sparse arrays.

@belden
Copy link

belden commented Feb 13, 2020

Vote to close per above

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