From 65b611eb0fe7878704df190e36dedfd0a9071c3a Mon Sep 17 00:00:00 2001 From: Xuefeng Zhu Date: Sat, 16 Apr 2016 00:29:04 -0500 Subject: [PATCH] refactor Header iterator methods --- fetch.js | 63 +++++++++++++++++++------------------------------------- 1 file changed, 21 insertions(+), 42 deletions(-) diff --git a/fetch.js b/fetch.js index 5717106b..d0652dea 100644 --- a/fetch.js +++ b/fetch.js @@ -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 = {} @@ -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) {