Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address comments
  • Loading branch information
TimothyGu committed Jan 29, 2017
commit 1fcac0203fb64d115027fcd42e2f171ee3fb6662
24 changes: 4 additions & 20 deletions doc/api/url.md
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,10 @@ console.log(params.toString());

Instantiate a new `URLSearchParams` object with an iterable map in a way that
is similar to [`Map`][]'s constructor. `iterable` can be an Array or any
iterable object. Elements of `iterable` are key-value pairs, and can themselves
be any iterable object.
iterable object. That means `iterable` can be another `URLSearchParams`, in
which case the constructor will simply create a clone of the provided
`URLSearchParams`. Elements of `iterable` are key-value pairs, and can
themselves be any iterable object.

Duplicate keys are allowed.

Expand Down Expand Up @@ -635,24 +637,6 @@ params = new URLSearchParams(getQueryPairs());
console.log(params.toString());
// Prints 'user=abc&query=first&query=second'

// Using a generator function for key-value pairs
function* getSingleQueryPair(idx) {
if (idx === 0) {
yield 'user'; yield 'abc';
} else if (idx === 1) {
yield 'query'; yield 'first';
} else {
yield 'query'; yield 'second';
}
}
params = new URLSearchParams([
getSingleQueryPair(0),
getSingleQueryPair(1),
getSingleQueryPair(2)
]);
console.log(params.toString());
// Prints 'user=abc&query=first&query=second'

// Each key-value pair must have exactly two elements
new URLSearchParams([
['user', 'abc', 'error']
Expand Down
3 changes: 1 addition & 2 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -648,9 +648,8 @@ function getObjectFromParams(array) {
}

class URLSearchParams {
constructor(init = '') {
constructor(init) {
if (init === null || init === undefined) {
Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Member Author

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.

// record<USVString, USVString>
this[searchParams] = [];
} else if (typeof init === 'object') {
const method = init[Symbol.iterator];
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-whatwg-url-searchparams-constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ assert.strictEqual(params.toString(), '');
assert.throws(() => URLSearchParams(), TypeError,
'Calling \'URLSearchParams\' without \'new\' should throw.');

// URLSearchParams constructor, undefined and null as argument
params = new URLSearchParams(undefined);
assert.strictEqual(params.toString(), '');
params = new URLSearchParams(null);
assert.strictEqual(params.toString(), '');

// URLSearchParams constructor, empty string as argument
params = new URLSearchParams('');
// eslint-disable-next-line no-restricted-properties
Expand Down