Skip to content

Commit

Permalink
feat: .paginate(options, mapFn)
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Nov 17, 2018
1 parent 34011c7 commit 8084afd
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions plugins/pagination/paginate.js
Expand Up @@ -2,19 +2,23 @@ module.exports = paginate

const iterator = require('./iterator')

function paginate (octokit, route, options) {
function paginate (octokit, route, options, mapFn) {
if (typeof options === 'function') {
mapFn = options
options = undefined
}
options = octokit.request.endpoint.merge(route, options)
return gather([], iterator(octokit, options)[Symbol.asyncIterator]())
return gather([], iterator(octokit, options)[Symbol.asyncIterator](), mapFn)
}

function gather (results, iterator) {
function gather (results, iterator, mapFn) {
return iterator.next()
.then(result => {
if (result.done) {
return results
}

results.push.apply(results, result.value.data)
return gather(results, iterator)
results.push.apply(results, mapFn ? mapFn(result.value) : result.value.data)
return gather(results, iterator, mapFn)
})
}

0 comments on commit 8084afd

Please sign in to comment.