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

check the arguments number of URLSearchParams #1390

Merged
merged 4 commits into from
Dec 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 21 additions & 3 deletions js/url_search_params.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { requiredArguments } from "./util";

// Copyright 2018 the Deno authors. All rights reserved. MIT license.
export class URLSearchParams {
private params: Array<[string, string]> = [];
Expand Down Expand Up @@ -46,7 +48,8 @@ export class URLSearchParams {
* searchParams.append('name', 'second');
*/
append(name: string, value: string): void {
this.params.push([name, value]);
requiredArguments("URLSearchParams.append", arguments.length, 2);
this.params.push([String(name), value]);
}

/** Deletes the given search parameter and its associated value,
Expand All @@ -55,6 +58,8 @@ export class URLSearchParams {
* searchParams.delete('name');
*/
delete(name: string): void {
requiredArguments("URLSearchParams.delete", arguments.length, 1);
name = String(name);
let i = 0;
while (i < this.params.length) {
if (this.params[i][0] === name) {
Expand All @@ -71,6 +76,8 @@ export class URLSearchParams {
* searchParams.getAll('name');
*/
getAll(name: string): string[] {
requiredArguments("URLSearchParams.getAll", arguments.length, 1);
name = String(name);
const values = [];
for (const entry of this.params) {
if (entry[0] === name) {
Expand All @@ -86,6 +93,8 @@ export class URLSearchParams {
* searchParams.get('name');
*/
get(name: string): string | null {
requiredArguments("URLSearchParams.get", arguments.length, 1);
name = String(name);
for (const entry of this.params) {
if (entry[0] === name) {
return entry[1];
Expand All @@ -101,6 +110,8 @@ export class URLSearchParams {
* searchParams.has('name');
*/
has(name: string): boolean {
requiredArguments("URLSearchParams.has", arguments.length, 1);
name = String(name);
return this.params.some(entry => entry[0] === name);
}

Expand All @@ -112,22 +123,26 @@ export class URLSearchParams {
* searchParams.set('name', 'value');
*/
set(name: string, value: string): void {
requiredArguments("URLSearchParams.set", arguments.length, 2);

// 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.
name = String(name);
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;
i++;
} else {
this.params.splice(i, 1);
continue;
}
} else {
i++;
}
i++;
}

// Otherwise, append a new name-value pair whose name is name
Expand Down Expand Up @@ -163,9 +178,12 @@ export class URLSearchParams {
// tslint:disable-next-line:no-any
thisArg?: any
) {
requiredArguments("URLSearchParams.forEach", arguments.length, 1);

if (typeof thisArg !== "undefined") {
callbackfn = callbackfn.bind(thisArg);
}

for (const [key, value] of this.entries()) {
callbackfn(value, key, this);
}
Expand Down
38 changes: 38 additions & 0 deletions js/url_search_params_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,41 @@ test(function urlSearchParamsShouldThrowTypeError() {

assertEqual(hasThrown, 2);
});

test(function urlSearchParamsAppendArgumentsCheck() {
const methodRequireOneParam = ["delete", "getAll", "get", "has", "forEach"];

const methodRequireTwoParams = ["append", "set"];

methodRequireOneParam.concat(methodRequireTwoParams).forEach(method => {
const searchParams = new URLSearchParams();
let hasThrown = 0;
try {
searchParams[method]();
hasThrown = 1;
} catch (err) {
if (err instanceof TypeError) {
hasThrown = 2;
} else {
hasThrown = 3;
}
}
assertEqual(hasThrown, 2);
});

methodRequireTwoParams.forEach(method => {
const searchParams = new URLSearchParams();
let hasThrown = 0;
try {
searchParams[method]("foo");
hasThrown = 1;
} catch (err) {
if (err instanceof TypeError) {
hasThrown = 2;
} else {
hasThrown = 3;
}
}
assertEqual(hasThrown, 2);
});
});
14 changes: 14 additions & 0 deletions js/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,17 @@ export function isTypedArray(x: unknown): x is TypedArray {
export function isObject(o: unknown): o is object {
return o != null && typeof o === "object";
}

// @internal
export function requiredArguments(
name: string,
length: number,
required: number
): void {
if (length < required) {
const errMsg = `${name} requires at least ${required} argument${
required === 1 ? "" : "s"
}, but only ${length} present`;
throw new TypeError(errMsg);
}
}