-
Notifications
You must be signed in to change notification settings - Fork 30k
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
url: extend URLSearchParams constructor #11060
Conversation
doc/api/url.md
Outdated
getSingleQueryPair(2) | ||
]); | ||
console.log(params.toString()); | ||
// Prints 'user=abc&query=first&query=second' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The generator examples are good but I would guess that most people probably won’t be interested in that… how about wrapping it in a <details>
tag? Does that work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@addaleax, if that is the case I'd rather just remove it. I agree the second generator example is a bit obscure, and it is in fact only compatible with URLSearchParams
, not with Map
(referenced earlier). Though the first example does seem useful to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m fine with any of those options… maybe somebody else has a stronger opinion but otherwise just go with whatever you prefer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I kinda can't get the merit of the second generator example at first glance. Maybe we can leave that later with a better use case as an example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joyeecheung, fair.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! Left a few comments, please take a look.
lib/internal/url.js
Outdated
if (init instanceof URLSearchParams) { | ||
const childParams = init[searchParams]; | ||
this[searchParams] = childParams.slice(); | ||
if (init === null || init === undefined) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we have init = ''
as default then init
can't be undefined
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The user can do new URLSearchParams(undefined)
. The default is there to echo the = ""
in the spec, though now I agree it's a bit pointless.
lib/internal/url.js
Outdated
const childParams = init[searchParams]; | ||
this[searchParams] = childParams.slice(); | ||
if (init === null || init === undefined) { | ||
// record<USVString, USVString> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment can be removed I believe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
@@ -16,23 +16,24 @@ assert.strictEqual(params + '', 'a=b'); | |||
params = new URLSearchParams(params); | |||
assert.strictEqual(params + '', 'a=b'); | |||
|
|||
// URLSearchParams constructor, empty. | |||
// URLSearchParams constructor, no arguments | |||
params = new URLSearchParams(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have new URLSearchParams(null)
test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not yet, nor do we have a new URLSearchParams(undefined)
test. Both are added.
doc/api/url.md
Outdated
getSingleQueryPair(2) | ||
]); | ||
console.log(params.toString()); | ||
// Prints 'user=abc&query=first&query=second' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I kinda can't get the merit of the second generator example at first glance. Maybe we can leave that later with a better use case as an example?
const params = new URLSearchParams(val.input); | ||
let i = 0; | ||
for (const param of params) { | ||
assert.deepStrictEqual(param, val.output[i], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about assert.deepStrictEqual(Array.from(params), val.output, ...)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. Fixed also.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Woohoo! happy to see this. Left a couple of comments but otherwise LGTM
doc/api/url.md
Outdated
@@ -525,7 +525,8 @@ value returned is equivalent to that of `url.href`. | |||
### Class: URLSearchParams | |||
|
|||
The `URLSearchParams` object provides read and write access to the query of a | |||
`URL`. | |||
`URL`. It can also be used standalone with one of the four following | |||
constructors. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a fan of starting a sentence with "It can..."... perhaps something like:
The object can also be used standalone...
doc/api/url.md
Outdated
Instantiate a new `URLSearchParams` object with a query hash map. The key and | ||
value of each property of `obj` are always coerced to strings. | ||
|
||
Warning: Unlike [`querystring`][] module, duplicate keys in the form of array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/^Warning:/^*Note*:/
CI: https://ci.nodejs.org/job/node-test-pull-request/6141/ |
PR-URL: #11060 Fixes: #10635 Ref: whatwg/url#175 Ref: web-platform-tests/wpt#4523 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
PR-URL: #11060 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
PR-URL: #11060 Ref: whatwg/url#175 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
PR-URL: nodejs#11060 Fixes: nodejs#10635 Ref: whatwg/url#175 Ref: web-platform-tests/wpt#4523 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
PR-URL: nodejs#11060 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
PR-URL: nodejs#11060 Ref: whatwg/url#175 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Added backport-requested for v7.x since it doesn't look like the spec is gonna change any time soon (re #11185 (comment)). EDIT: it was backported in #12507, pending a merge there. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good
In addition to
URLSearchParams
andUSVString
, supportrecord<USVString, USVString>
andsequence<sequence<USVString>>
also.Fixes: #10635
Refs: whatwg/url#175
Refs: web-platform-tests/wpt#4523
CI: https://ci.nodejs.org/job/node-test-pull-request/6089/
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
url