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

size (or length) for URLSearchParams #163

Closed
stevenvachon opened this issue Nov 7, 2016 · 17 comments · Fixed by #734
Closed

size (or length) for URLSearchParams #163

stevenvachon opened this issue Nov 7, 2016 · 17 comments · Fixed by #734
Labels
addition/proposal New features or enhancements clarification Standard could be clearer needs implementer interest Moving the issue forward requires implementers to express interest topic: api

Comments

@stevenvachon
Copy link

stevenvachon commented Nov 7, 2016

URLSearchParams is basically a Map and should have a size property. The unfortunate alternative is:

Array.from(url.searchParams).length
@annevk
Copy link
Member

annevk commented Nov 7, 2016

Well, it's a "multi-map". You can have duplicate keys. But I guess this would just return the length of the underlying list?

@domenic @tabatkins thoughts on naming?

If we add this, we should also update FormData and possibly Headers.

@stevenvachon
Copy link
Author

const url = new URL("protocol://domain?query=value1&query=value2&query2=value");
console.log(url.searchParams.size);  // 3

@domenic
Copy link
Member

domenic commented Nov 7, 2016

Oh, I would have expected 2, not 3 :(. That indicates to me this may be too confusing of a concept to add, and just using Array.from().length, or asking TC39 to add an %IteratorPrototype%.length so that you can do url.searchParams.keys().length vs. url.searchParams.values().length, would be better.

@tabatkins
Copy link
Contributor

Yeah, MultiMaps are intended to be usable as Maps, too, and the difference in expected size between the two use-cases is a footgun if you make the choice either way. +1 to adding .length to iterators.

@annevk
Copy link
Member

annevk commented Nov 8, 2016

Leaving this open for now as documenting these multimap design decisions somewhere is probably worthwhile.

@annevk
Copy link
Member

annevk commented Feb 15, 2017

@domenic what is the likelihood of TC39 adding such an extension? An alternative is that we have size and sizeAll, in line with other members...

@domenic
Copy link
Member

domenic commented Feb 15, 2017

It's really hard for me to say... we could try proposing it.

@tabatkins
Copy link
Contributor

Given the use of MultiMaps in the CSS Typed OM, we really should make this a formal thing.

@eligrey
Copy link

eligrey commented Jul 1, 2022

Lack of a built-in size getter results in developers writing code like this:

if ([...searchParams].length !== 0) {
  // ...
}

as opposed to this (more efficient, but harder to read):

if (!searchParams.entries().next().done) {
  // ...
}

A size getter could be optimized by the JS implementation so that searchParams.size !== 0 checks could be more efficient than [...searchParams].length !== 0.

@annevk annevk added needs implementer interest Moving the issue forward requires implementers to express interest addition/proposal New features or enhancements labels Dec 9, 2022
@WORMSS
Copy link

WORMSS commented Jan 13, 2023

Yes, as @eligrey has pointed out, I was in the need of a 'do you have any values, yes? no?' I didn't actually care that much about the full count..
In the end I actually just did

const qs = urlParams.toString();
if (qs.length) {
  return `?${qs}`;
}
return '';

Not quite as nice looking, but it got the job done.

@annevk
Copy link
Member

annevk commented Jan 13, 2023

Hmm, isEmpty() doesn't seem entirely out of the question. Though there's no precedent in JS land.

@jasnell
Copy link
Collaborator

jasnell commented Jan 13, 2023

Would the iterator helpers proposals be of use here: https://github.com/tc39/proposal-iterator-helpers

@tabatkins
Copy link
Contributor

Iterator helpers doesn't have a way to count an arbitrary iterable, so no.

@tabatkins
Copy link
Contributor

That said, since the behavior of [...url.searchParams] is to produce a list of entries, I think the clear winner between "count distinct keys" and "count entries" is the latter; aka the earlier example should return 3.

URLSearchParams doesn't provide any simple way to get just the distinct keys; for that you need to manually uniquify them with, say, [...new Set(URLSearchParams.keys())]. So it shouldn't bias towards counting unique keys.

annevk added a commit that referenced this issue Jan 16, 2023
@ricea
Copy link

ricea commented Feb 17, 2023

Sorry to show up towing a bikeshed, but how about

unsigned long count(optional USVString key, optional USVString value);

allowing you to do

const p = new URLSearchParams('a=1&b=2&b=3&b=3');
p.count();  // 4
p.count('b');  // 3
p.count('b', '3');  // 2

?

@annevk
Copy link
Member

annevk commented Feb 17, 2023

I wouldn't mind that, but with most JS collection APIs having either a size or length getter shouldn't we have that as well?

@zloirock
Copy link

I think that there are too many overloads.

The second 2 cases can be solved via .getAll().length and .getAll().filter().length.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition/proposal New features or enhancements clarification Standard could be clearer needs implementer interest Moving the issue forward requires implementers to express interest topic: api
Development

Successfully merging a pull request may close this issue.

9 participants