|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | | -const { isBlobLike, isFileLike, toUSVString } = require('./util') |
| 3 | +const { isBlobLike, isFileLike, toUSVString, makeIterator } = require('./util') |
4 | 4 | const { kState } = require('./symbols') |
5 | 5 | const { File, FileLike } = require('./file') |
6 | 6 | const { Blob } = require('buffer') |
@@ -187,45 +187,68 @@ class FormData { |
187 | 187 | return this.constructor.name |
188 | 188 | } |
189 | 189 |
|
190 | | - * entries () { |
| 190 | + entries () { |
191 | 191 | if (!(this instanceof FormData)) { |
192 | 192 | throw new TypeError('Illegal invocation') |
193 | 193 | } |
194 | 194 |
|
195 | | - for (const pair of this) { |
196 | | - yield pair |
197 | | - } |
| 195 | + return makeIterator( |
| 196 | + makeIterable(this[kState], 'entries'), |
| 197 | + 'FormData' |
| 198 | + ) |
198 | 199 | } |
199 | 200 |
|
200 | | - * keys () { |
| 201 | + keys () { |
201 | 202 | if (!(this instanceof FormData)) { |
202 | 203 | throw new TypeError('Illegal invocation') |
203 | 204 | } |
204 | 205 |
|
205 | | - for (const [key] of this) { |
206 | | - yield key |
| 206 | + return makeIterator( |
| 207 | + makeIterable(this[kState], 'keys'), |
| 208 | + 'FormData' |
| 209 | + ) |
| 210 | + } |
| 211 | + |
| 212 | + values () { |
| 213 | + if (!(this instanceof FormData)) { |
| 214 | + throw new TypeError('Illegal invocation') |
207 | 215 | } |
| 216 | + |
| 217 | + return makeIterator( |
| 218 | + makeIterable(this[kState], 'values'), |
| 219 | + 'FormData' |
| 220 | + ) |
208 | 221 | } |
209 | 222 |
|
210 | | - * values () { |
| 223 | + /** |
| 224 | + * @param {(value: string, key: string, self: FormData) => void} callbackFn |
| 225 | + * @param {unknown} thisArg |
| 226 | + */ |
| 227 | + forEach (callbackFn, thisArg = globalThis) { |
211 | 228 | if (!(this instanceof FormData)) { |
212 | 229 | throw new TypeError('Illegal invocation') |
213 | 230 | } |
214 | 231 |
|
215 | | - for (const [, value] of this) { |
216 | | - yield value |
| 232 | + if (arguments.length < 1) { |
| 233 | + throw new TypeError( |
| 234 | + `Failed to execute 'forEach' on 'FormData': 1 argument required, but only ${arguments.length} present.` |
| 235 | + ) |
217 | 236 | } |
218 | | - } |
219 | 237 |
|
220 | | - * [Symbol.iterator] () { |
221 | | - // The value pairs to iterate over are this’s entry list’s entries with |
222 | | - // the key being the name and the value being the value. |
223 | | - for (const { name, value } of this[kState]) { |
224 | | - yield [name, value] |
| 238 | + if (typeof callbackFn !== 'function') { |
| 239 | + throw new TypeError( |
| 240 | + "Failed to execute 'forEach' on 'FormData': parameter 1 is not of type 'Function'." |
| 241 | + ) |
| 242 | + } |
| 243 | + |
| 244 | + for (const [key, value] of this) { |
| 245 | + callbackFn.apply(thisArg, [value, key, this]) |
225 | 246 | } |
226 | 247 | } |
227 | 248 | } |
228 | 249 |
|
| 250 | +FormData.prototype[Symbol.iterator] = FormData.prototype.entries |
| 251 | + |
229 | 252 | function makeEntry (name, value, filename) { |
230 | 253 | // To create an entry for name, value, and optionally a filename, run these |
231 | 254 | // steps: |
@@ -267,4 +290,18 @@ function makeEntry (name, value, filename) { |
267 | 290 | return entry |
268 | 291 | } |
269 | 292 |
|
| 293 | +function * makeIterable (entries, type) { |
| 294 | + // The value pairs to iterate over are this’s entry list’s entries |
| 295 | + // with the key being the name and the value being the value. |
| 296 | + for (const { name, value } of entries) { |
| 297 | + if (type === 'entries') { |
| 298 | + yield [name, value] |
| 299 | + } else if (type === 'values') { |
| 300 | + yield value |
| 301 | + } else { |
| 302 | + yield name |
| 303 | + } |
| 304 | + } |
| 305 | +} |
| 306 | + |
270 | 307 | module.exports = { FormData } |
0 commit comments