Skip to content

Commit

Permalink
Add Headers#entries iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
dgraham committed Mar 12, 2016
1 parent 6f7a588 commit 12e692e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
11 changes: 11 additions & 0 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 12e692e

Please sign in to comment.