mirror of
https://github.com/denoland/deno.git
synced 2025-01-22 06:09:25 -05:00
feat: URLSearchParams should work with custom iterator (#2512)
This commit is contained in:
parent
dc60fe9f30
commit
52448f351d
3 changed files with 29 additions and 1 deletions
|
@ -1,6 +1,6 @@
|
||||||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||||
import { URL } from "./url";
|
import { URL } from "./url";
|
||||||
import { requiredArguments } from "./util";
|
import { requiredArguments, isIterable } from "./util";
|
||||||
|
|
||||||
export class URLSearchParams {
|
export class URLSearchParams {
|
||||||
private params: Array<[string, string]> = [];
|
private params: Array<[string, string]> = [];
|
||||||
|
@ -17,6 +17,11 @@ export class URLSearchParams {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isIterable(init)) {
|
||||||
|
this.params = [...init];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (Object(init) !== init) {
|
if (Object(init) !== init) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -217,3 +217,13 @@ test(function urlSearchParamsDeletingAppendedMultiple(): void {
|
||||||
params.delete("first");
|
params.delete("first");
|
||||||
assertEquals(params.has("first"), false);
|
assertEquals(params.has("first"), false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ref: https://github.com/web-platform-tests/wpt/blob/master/url/urlsearchparams-constructor.any.js#L176-L182
|
||||||
|
test(function urlSearchParamsCustomSymbolIterator(): void {
|
||||||
|
const params = new URLSearchParams();
|
||||||
|
params[Symbol.iterator] = function*(): IterableIterator<[string, string]> {
|
||||||
|
yield ["a", "b"];
|
||||||
|
};
|
||||||
|
const params1 = new URLSearchParams((params as unknown) as string[][]);
|
||||||
|
assertEquals(params1.get("a"), "b");
|
||||||
|
});
|
||||||
|
|
13
js/util.ts
13
js/util.ts
|
@ -122,6 +122,19 @@ export function isObject(o: unknown): o is object {
|
||||||
return o != null && typeof o === "object";
|
return o != null && typeof o === "object";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns whether o is iterable.
|
||||||
|
export function isIterable<T, P extends keyof T, K extends T[P]>(
|
||||||
|
o: T
|
||||||
|
): o is T & Iterable<[P, K]> {
|
||||||
|
// checks for null and undefined
|
||||||
|
if (o == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
typeof ((o as unknown) as Iterable<[P, K]>)[Symbol.iterator] === "function"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// @internal
|
// @internal
|
||||||
export function requiredArguments(
|
export function requiredArguments(
|
||||||
name: string,
|
name: string,
|
||||||
|
|
Loading…
Add table
Reference in a new issue