byu-paginate

Generate pagination options for BYU APIs

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
byu-paginate
0.1.86 years ago6 years agoMinified + gzip package size for byu-paginate in KB

Readme

paginate
Simple pagination tool for API implementation.
Installation
Install with npm: ``npm i byu-paginate``
Usage
Calculates all pagination starting locations for first, last, prev, and next page.
Parameters:
  • data - expects an array of the items that you want to paginate.
  • pageStart (optional) - expects a number for the start location greater than zero.
  • pageSize (optional) - expects a number for how many items should be returned.
  • myDefaults (optional) - an object to replace the defaults ({ start: 1, size: 50, maxSize: 1000 })
Returns:
An Object with the following properties:
  • start - the location of the first item in the current page.
  • end - the location of the last item in the current page.
  • size - number of elements returned in the current selection.
  • next - the location of the next page's starting element.
  • prev - the location of the previous page's starting element.
  • last - the location of the starting element in the last page of the collection.
  • collectionSize - the number of items in the collection.
  • defaults:
- start - default start location.
- size - default page size.
- maxSize - default max page size.
  • result - an array of the current page's items.

Usage examples:
let paginate = require('byu-paginate')

const months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec' ]

let page = paginate(months, 0, 3)

return {
  links: {
    info: {
      rel: 'self',
      href: `https://my-domain/my-path?page_start=${page.start}&page_size=${page.size}`,
      method: 'GET'
    },
    ...page.prev && { prev: {
      rel: 'prev',
      href: `https://my-domain/my-path?page_start=${page.prev}&page_size=${page.size}`,
      method: 'GET'
    } },
    ...page.next && { next: {
      rel: 'next',
      href: `https://my-domain/my-path?page_start=${page.next}&page_size={page.size}`,
      method: 'GET'
    } }
  },
  metadata: {
    collection_size: page.collectionSize,
    page_start: page.start,
    page_end: page.end,
    page_size: page.size,
    default_page_size: page.defaults.size,
    max_page_size: page.defaults.maxSize
  },
  values: page.results
}

The above return value would look like:
{
  links: {
    info: {
      rel: 'self',
      href: `https://my-domain/my-path?page_start=1&page_size=3`,
      method: 'GET'
    },
    next: {
      rel: 'next',
      href: `https://my-domain/my-path?page_start=4&page_size=3`,
      method: 'GET'
    }
  },
  metadata: {
    collection_size: 12,
    page_start: 1,
    page_end: 3,
    page_size: 3,
    default_page_size: 50,
    max_page_size: 1000
  },
  values: [
    'Jan',
    'Feb',
    'Mar'
  ]
}