Skip to content

Commit

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

Headers.prototype.values = function() {
var items = []
this.forEach(function(value) { items.push(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 @@ -232,6 +232,18 @@ suite('Headers', function() {
assert.deepEqual({done: false, value: 'content-type'}, iterator.next())
assert.deepEqual({done: true, value: undefined}, iterator.next())
})
test('is iterable with values', function() {
var headers = new Headers()
headers.append('Accept', 'application/json')
headers.append('Accept', 'text/plain')
headers.append('Content-Type', 'text/html')

var iterator = headers.values()
assert.deepEqual({done: false, value: 'application/json'}, iterator.next())
assert.deepEqual({done: false, value: 'text/plain'}, iterator.next())
assert.deepEqual({done: false, value: '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 6f7a588

Please sign in to comment.