зеркало из https://github.com/github/fetch.git
Add Headers#entries iterator
This commit is contained in:
Родитель
6f7a588fd0
Коммит
12e692e7c9
11
fetch.js
11
fetch.js
|
@ -99,6 +99,17 @@
|
|||
}
|
||||
}
|
||||
|
||||
Headers.prototype.entries = function() {
|
||||
var items = []
|
||||
this.forEach(function(value, name) { items.push([name, value]) })
|
||||
return {
|
||||
next: function() {
|
||||
var value = items.shift()
|
||||
return {done: value === undefined, value: value}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function consumed(body) {
|
||||
if (body.bodyUsed) {
|
||||
return Promise.reject(new TypeError('Already read'))
|
||||
|
|
12
test/test.js
12
test/test.js
|
@ -244,6 +244,18 @@ suite('Headers', function() {
|
|||
assert.deepEqual({done: false, value: 'text/html'}, iterator.next())
|
||||
assert.deepEqual({done: true, value: undefined}, iterator.next())
|
||||
})
|
||||
test('is iterable with entries', function() {
|
||||
var headers = new Headers()
|
||||
headers.append('Accept', 'application/json')
|
||||
headers.append('Accept', 'text/plain')
|
||||
headers.append('Content-Type', 'text/html')
|
||||
|
||||
var iterator = headers.entries()
|
||||
assert.deepEqual({done: false, value: ['accept', 'application/json']}, iterator.next())
|
||||
assert.deepEqual({done: false, value: ['accept', 'text/plain']}, iterator.next())
|
||||
assert.deepEqual({done: false, value: ['content-type', 'text/html']}, iterator.next())
|
||||
assert.deepEqual({done: true, value: undefined}, iterator.next())
|
||||
})
|
||||
})
|
||||
|
||||
// https://fetch.spec.whatwg.org/#request-class
|
||||
|
|
Загрузка…
Ссылка в новой задаче