paginatedPatches.js

import flatPatchesFunc from './flatPatches.js'

export default async () => {
  const flatPatches = await flatPatchesFunc()

  let paginatedPatches = []
  const patchesPerPage = 100

  flatPatches.forEach((patch) => {
    const index = paginatedPatches.findIndex((page) => {
      return (
        page.repoName === patch.repoName
        && page.branchName == patch.branchName
        && page.patches.length <= patchesPerPage
      )
    })

    if (index === -1) {
      const pageNumber = paginatedPatches.filter(page => (page.repoName === patch.repoName && page.branchName === patch.branchName)).length + 1
      paginatedPatches.push({
        repoName: patch.repoName,
        branchName: patch.branchName,
        patches: [patch.patch],
        // current page number is one more than "how many items in paginatedPatches already
        // have repoName as their repo name.
        pageNumber,
      })
    }
    else {
      paginatedPatches[index].patches.push(patch.patch)
    }
  })

  return paginatedPatches
}