-
Notifications
You must be signed in to change notification settings - Fork 146
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
Comments
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 |
const url = new URL("protocol://domain?query=value1&query=value2&query2=value");
console.log(url.searchParams.size); // 3 |
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 |
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. |
Leaving this open for now as documenting these multimap design decisions somewhere is probably worthwhile. |
@domenic what is the likelihood of TC39 adding such an extension? An alternative is that we have |
It's really hard for me to say... we could try proposing it. |
Given the use of MultiMaps in the CSS Typed OM, we really should make this a formal thing. |
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 |
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.. const qs = urlParams.toString();
if (qs.length) {
return `?${qs}`;
}
return ''; Not quite as nice looking, but it got the job done. |
Hmm, |
Would the iterator helpers proposals be of use here: https://github.com/tc39/proposal-iterator-helpers |
Iterator helpers doesn't have a way to count an arbitrary iterable, so no. |
That said, since the behavior of URLSearchParams doesn't provide any simple way to get just the distinct keys; for that you need to manually uniquify them with, say, |
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 ? |
I wouldn't mind that, but with most JS collection APIs having either a |
I think that there are too many overloads. The second 2 cases can be solved via |
URLSearchParams
is basically aMap
and should have asize
property. The unfortunate alternative is:The text was updated successfully, but these errors were encountered: