Skip to content

refactor Header iterator methods - #317

Merged
dgraham merged 1 commit into
JakeChampion:masterfrom
Xuefeng-Zhu:patch-1
Apr 19, 2016
Merged

dgraham merged 1 commit into
JakeChampion:masterfrom
Xuefeng-Zhu:patch-1

Conversation

@Xuefeng-Zhu

Copy link
Copy Markdown
Contributor

remove duplicate code in values, keys, entries method for Header

@Xuefeng-Zhu
Xuefeng-Zhu force-pushed the patch-1 branch 3 times, most recently from b980588 to db8b719 Compare April 16, 2016 05:46
Comment thread fetch.js Outdated

function getIterator(headers, addItem) {
var items = []
headers.forEach(addItem, items)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting solution to expose items as this within iteration callbacks

@Xuefeng-Zhu
Xuefeng-Zhu force-pushed the patch-1 branch 6 times, most recently from fc09fdc to d5bc839 Compare April 18, 2016 18:31
@mislav

mislav commented Apr 18, 2016

Copy link
Copy Markdown
Contributor

@dgraham This looks like good DRYing up to me. What say you, the author of the original code?

Comment thread fetch.js
}

return iterator
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll get most of the DRY benefit with less complexity if we reduce getIterator's responsibilities to just creating and returning an iterator object over a list of values. It's currently also responsible for building the value list from a function parameter, which complicates each call site.

  // 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
  }

// Each call site still builds its own value list.
Headers.prototype.keys = function() {
  var items = []
  this.forEach(function(value, name) { items.push(name) })
  return iteratorFor(items)
}

I used iteratorFor as the function name in this example because a "get" prefix implies the function returns a saved, pre-calculated value. It's a factory function, though, so maybe iteratorFor, iteratorFrom, createIterator, or simply the noun iterator would be better names.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds like a better approach. Do you want me to update the code based on your comment?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the code

@dgraham
dgraham merged commit 6e3aa34 into JakeChampion:master Apr 19, 2016
@dgraham

dgraham commented Apr 19, 2016

Copy link
Copy Markdown
Contributor

@Xuefeng-Zhu Thanks for your contribution!

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Oct 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants