From a95319074240a869dce088fa580e46a067a8e7d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BF=B7=E6=B8=A1?= Date: Mon, 17 Jun 2019 23:39:23 +0800 Subject: [PATCH] fix urlSearchParams custom symbol iterator (#2537) --- js/url_search_params.ts | 11 ++++------- js/url_search_params_test.ts | 9 +++++++++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/js/url_search_params.ts b/js/url_search_params.ts index 666ba69515..8e122191d4 100644 --- a/js/url_search_params.ts +++ b/js/url_search_params.ts @@ -12,16 +12,11 @@ export class URLSearchParams { return; } - if (Array.isArray(init)) { + if (Array.isArray(init) || isIterable(init)) { this._handleArrayInitialization(init); return; } - if (isIterable(init)) { - this.params = [...init]; - return; - } - if (Object(init) !== init) { return; } @@ -285,7 +280,9 @@ export class URLSearchParams { } } - private _handleArrayInitialization(init: string[][]): void { + private _handleArrayInitialization( + init: string[][] | Iterable<[string, string]> + ): void { // Overload: sequence> for (const tuple of init) { // If pair does not contain exactly two items, then throw a TypeError. diff --git a/js/url_search_params_test.ts b/js/url_search_params_test.ts index e270dd7ff6..08b0c5a1f5 100644 --- a/js/url_search_params_test.ts +++ b/js/url_search_params_test.ts @@ -227,3 +227,12 @@ test(function urlSearchParamsCustomSymbolIterator(): void { const params1 = new URLSearchParams((params as unknown) as string[][]); assertEquals(params1.get("a"), "b"); }); + +test(function urlSearchParamsCustomSymbolIteratorWithNonStringParams(): void { + const params = {}; + params[Symbol.iterator] = function*(): IterableIterator<[number, number]> { + yield [1, 2]; + }; + const params1 = new URLSearchParams((params as unknown) as string[][]); + assertEquals(params1.get("1"), "2"); +});