Skip to content

Commit

Permalink
Add Headers#keys iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
dgraham committed Mar 12, 2016
1 parent edb7c73 commit 3ebc7ed
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 @@ -77,6 +77,17 @@
}, this)
}

Headers.prototype.keys = function() {
var items = []
this.forEach(function(value, name) { items.push(name) })
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 @@ -220,6 +220,18 @@ suite('Headers', function() {
assert.equal(this, thisArg)
}, thisArg)
})
test('is iterable with keys', function() {
var headers = new Headers()
headers.append('Accept', 'application/json')
headers.append('Accept', 'text/plain')
headers.append('Content-Type', 'text/html')

var iterator = headers.keys()
assert.deepEqual({done: false, value: 'accept'}, iterator.next())
assert.deepEqual({done: false, value: 'accept'}, iterator.next())
assert.deepEqual({done: false, value: 'content-type'}, iterator.next())
assert.deepEqual({done: true, value: undefined}, iterator.next())
})
})

// https://fetch.spec.whatwg.org/#request-class
Expand Down

0 comments on commit 3ebc7ed

Please sign in to comment.