mirror of
https://github.com/denoland/deno.git
synced 2025-01-03 04:48:52 -05:00
Fix URLSearchParams set() and constructor() (#1368)
This commit is contained in:
parent
528eb2adb3
commit
3dbd18af09
2 changed files with 57 additions and 4 deletions
|
@ -24,6 +24,12 @@ export class URLSearchParams {
|
|||
} else if (Array.isArray(init)) {
|
||||
// Overload: sequence<sequence<USVString>>
|
||||
for (const tuple of init) {
|
||||
// If pair does not contain exactly two items, then throw a TypeError.
|
||||
if (tuple.length !== 2) {
|
||||
const errMsg =
|
||||
"Each query pair must be an iterable [name, value] tuple";
|
||||
throw new TypeError(errMsg);
|
||||
}
|
||||
this.append(tuple[0], tuple[1]);
|
||||
}
|
||||
} else if (Object(init) === init) {
|
||||
|
@ -106,8 +112,29 @@ export class URLSearchParams {
|
|||
* searchParams.set('name', 'value');
|
||||
*/
|
||||
set(name: string, value: string): void {
|
||||
this.delete(name);
|
||||
this.append(name, value);
|
||||
// If there are any name-value pairs whose name is name, in list,
|
||||
// set the value of the first such name-value pair to value
|
||||
// and remove the others.
|
||||
let found = false;
|
||||
let i = 0;
|
||||
while (i < this.params.length) {
|
||||
if (this.params[i][0] === name) {
|
||||
if (!found) {
|
||||
this.params[i][1] = value;
|
||||
found = true;
|
||||
} else {
|
||||
this.params.splice(i, 1);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
// Otherwise, append a new name-value pair whose name is name
|
||||
// and value is value, to list.
|
||||
if (!found) {
|
||||
this.append(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
/** Sort all key/value pairs contained in this object in place and
|
||||
|
|
|
@ -59,11 +59,18 @@ test(function urlSearchParamsHasSuccess() {
|
|||
assert(!searchParams.has("c"));
|
||||
});
|
||||
|
||||
test(function urlSearchParamsSetSuccess() {
|
||||
test(function urlSearchParamsSetReplaceFirstAndRemoveOthers() {
|
||||
const init = "a=54&b=true&a=true";
|
||||
const searchParams = new URLSearchParams(init);
|
||||
searchParams.set("a", "false");
|
||||
assertEqual(searchParams.toString(), "b=true&a=false");
|
||||
assertEqual(searchParams.toString(), "a=false&b=true");
|
||||
});
|
||||
|
||||
test(function urlSearchParamsSetAppendNew() {
|
||||
const init = "a=54&b=true&a=true";
|
||||
const searchParams = new URLSearchParams(init);
|
||||
searchParams.set("c", "foo");
|
||||
assertEqual(searchParams.toString(), "a=54&b=true&a=true&c=foo");
|
||||
});
|
||||
|
||||
test(function urlSearchParamsSortSuccess() {
|
||||
|
@ -112,3 +119,22 @@ test(function urlSearchParamsMissingPair() {
|
|||
const searchParams = new URLSearchParams(init);
|
||||
assertEqual(searchParams.toString(), "c=4&a=54");
|
||||
});
|
||||
|
||||
// If pair does not contain exactly two items, then throw a TypeError.
|
||||
// ref https://url.spec.whatwg.org/#interface-urlsearchparams
|
||||
test(function urlSearchParamsShouldThrowTypeError() {
|
||||
let hasThrown = 0;
|
||||
|
||||
try {
|
||||
new URLSearchParams([["1"]]);
|
||||
hasThrown = 1;
|
||||
} catch (err) {
|
||||
if (err instanceof TypeError) {
|
||||
hasThrown = 2;
|
||||
} else {
|
||||
hasThrown = 3;
|
||||
}
|
||||
}
|
||||
|
||||
assertEqual(hasThrown, 2);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue