Repository navigation
refactor Header iterator methods - #317
Conversation
b980588 to
db8b719
Compare
|
|
||
| function getIterator(headers, addItem) { | ||
| var items = [] | ||
| headers.forEach(addItem, items) |
There was a problem hiding this comment.
Interesting solution to expose items as this within iteration callbacks
fc09fdc to
d5bc839
Compare
|
@dgraham This looks like good DRYing up to me. What say you, the author of the original code? |
| } | ||
|
|
||
| return iterator | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
It sounds like a better approach. Do you want me to update the code based on your comment?
There was a problem hiding this comment.
I have updated the code
|
@Xuefeng-Zhu Thanks for your contribution! |
remove duplicate code in values, keys, entries method for Header