Skip to content

Commit

Permalink
Merge pull request JakeChampion#317 from Xuefeng-Zhu/patch-1
Browse files Browse the repository at this point in the history
refactor Header iterator methods
  • Loading branch information
dgraham committed Apr 19, 2016
2 parents 7f71c9b + 65b611e commit 6e3aa34
Showing 1 changed file with 21 additions and 42 deletions.
63 changes: 21 additions & 42 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@
return value
}

// Build a destructive iterator for the value list
function iteratorFor(items) {
var iterator = {
next: function() {
var value = items.shift()
return {done: value === undefined, value: value}
}
}

if (support.iterable) {
iterator[Symbol.iterator] = function() {
return iterator
}
}

return iterator
}

function Headers(headers) {
this.map = {}

Expand Down Expand Up @@ -95,58 +113,19 @@
Headers.prototype.keys = function() {
var items = []
this.forEach(function(value, name) { items.push(name) })
var iterator = {
next: function() {
var value = items.shift()
return {done: value === undefined, value: value}
}
}

if (support.iterable) {
iterator[Symbol.iterator] = function() {
return iterator
}
}

return iterator
return iteratorFor(items)
}

Headers.prototype.values = function() {
var items = []
this.forEach(function(value) { items.push(value) })
var iterator = {
next: function() {
var value = items.shift()
return {done: value === undefined, value: value}
}
}

if (support.iterable) {
iterator[Symbol.iterator] = function() {
return iterator
}
}

return iterator
return iteratorFor(items)
}

Headers.prototype.entries = function() {
var items = []
this.forEach(function(value, name) { items.push([name, value]) })
var iterator = {
next: function() {
var value = items.shift()
return {done: value === undefined, value: value}
}
}

if (support.iterable) {
iterator[Symbol.iterator] = function() {
return iterator
}
}

return iterator
return iteratorFor(items)
}

if (support.iterable) {
Expand Down

0 comments on commit 6e3aa34

Please sign in to comment.