2
2
3
3
const {
4
4
Array,
5
+ ArrayIsArray,
5
6
ArrayPrototypeJoin,
6
7
ArrayPrototypeMap,
7
8
ArrayPrototypePush,
@@ -150,52 +151,64 @@ function isURLSearchParams(self) {
150
151
}
151
152
152
153
class URLSearchParams {
154
+ [ searchParams ] = [ ] ;
155
+
156
+ // "associated url object"
157
+ [ context ] = null ;
158
+
153
159
// URL Standard says the default value is '', but as undefined and '' have
154
160
// the same result, undefined is used to prevent unnecessary parsing.
155
161
// Default parameter is necessary to keep URLSearchParams.length === 0 in
156
162
// accordance with Web IDL spec.
157
163
constructor ( init = undefined ) {
158
- if ( init === null || init === undefined ) {
159
- this [ searchParams ] = [ ] ;
164
+ if ( init == null ) {
165
+ // Do nothing
160
166
} else if ( typeof init === 'object' || typeof init === 'function' ) {
161
167
const method = init [ SymbolIterator ] ;
162
168
if ( method === this [ SymbolIterator ] ) {
163
169
// While the spec does not have this branch, we can use it as a
164
170
// shortcut to avoid having to go through the costly generic iterator.
165
171
const childParams = init [ searchParams ] ;
166
172
this [ searchParams ] = childParams . slice ( ) ;
167
- } else if ( method !== null && method !== undefined ) {
173
+ } else if ( method != null ) {
168
174
if ( typeof method !== 'function' ) {
169
175
throw new ERR_ARG_NOT_ITERABLE ( 'Query pairs' ) ;
170
176
}
171
177
172
178
// Sequence<sequence<USVString>>
173
- // Note: per spec we have to first exhaust the lists then process them
174
- const pairs = [ ] ;
175
179
for ( const pair of init ) {
176
- if ( ( typeof pair !== 'object' && typeof pair !== 'function' ) ||
177
- pair === null ||
178
- typeof pair [ SymbolIterator ] !== 'function' ) {
180
+ if ( pair == null ) {
179
181
throw new ERR_INVALID_TUPLE ( 'Each query pair' , '[name, value]' ) ;
180
- }
181
- const convertedPair = [ ] ;
182
- for ( const element of pair )
183
- ArrayPrototypePush ( convertedPair , toUSVString ( element ) ) ;
184
- ArrayPrototypePush ( pairs , convertedPair ) ;
185
- }
182
+ } else if ( ArrayIsArray ( pair ) ) {
183
+ // If innerSequence's size is not 2, then throw a TypeError.
184
+ if ( pair . length !== 2 ) {
185
+ throw new ERR_INVALID_TUPLE ( 'Each query pair' , '[name, value]' ) ;
186
+ }
187
+ // Append (innerSequence[0], innerSequence[1]) to querys list.
188
+ ArrayPrototypePush ( this [ searchParams ] , toUSVString ( pair [ 0 ] ) , toUSVString ( pair [ 1 ] ) ) ;
189
+ } else {
190
+ if ( ( ( typeof pair !== 'object' && typeof pair !== 'function' ) ||
191
+ typeof pair [ SymbolIterator ] !== 'function' ) ) {
192
+ throw new ERR_INVALID_TUPLE ( 'Each query pair' , '[name, value]' ) ;
193
+ }
186
194
187
- this [ searchParams ] = [ ] ;
188
- for ( const pair of pairs ) {
189
- if ( pair . length !== 2 ) {
190
- throw new ERR_INVALID_TUPLE ( 'Each query pair' , '[name, value]' ) ;
195
+ let length = 0 ;
196
+
197
+ for ( const element of pair ) {
198
+ length ++ ;
199
+ ArrayPrototypePush ( this [ searchParams ] , element ) ;
200
+ }
201
+
202
+ // If innerSequence's size is not 2, then throw a TypeError.
203
+ if ( length !== 2 ) {
204
+ throw new ERR_INVALID_TUPLE ( 'Each query pair' , '[name, value]' ) ;
205
+ }
191
206
}
192
- ArrayPrototypePush ( this [ searchParams ] , pair [ 0 ] , pair [ 1 ] ) ;
193
207
}
194
208
} else {
195
209
// Record<USVString, USVString>
196
210
// Need to use reflection APIs for full spec compliance.
197
211
const visited = { } ;
198
- this [ searchParams ] = [ ] ;
199
212
const keys = ReflectOwnKeys ( init ) ;
200
213
for ( let i = 0 ; i < keys . length ; i ++ ) {
201
214
const key = keys [ i ] ;
@@ -217,13 +230,10 @@ class URLSearchParams {
217
230
}
218
231
}
219
232
} else {
220
- // USVString
233
+ // https://url.spec.whatwg.org/#dom-urlsearchparams-urlsearchparams
221
234
init = toUSVString ( init ) ;
222
235
this [ searchParams ] = init ? parseParams ( init ) : [ ] ;
223
236
}
224
-
225
- // "associated url object"
226
- this [ context ] = null ;
227
237
}
228
238
229
239
[ inspect . custom ] ( recurseTimes , ctx ) {
0 commit comments