Helps to convert "per page" and "page number" to skip/take values

Helper functions to create paginated REST APIs in node.js. Took some ideas from the github api v3.

I took the name from Dexter Morgan boat because I like the show and paginate is already used :)

npm install slice-of-life

This is useful for cases when you want to ask the consumer just the page number and maybe the per page amount of objects, but you have to convert this information to the classical skip, take, limit.

var sliceOfLife = require('slice-of-life');
var pageData = sliceOfLife.convert({
  per_page: 100, //perPage also works 
  page: 5
// will print {skip: 500, limit: 100} as simple as that. 

There is also an express middleware to do the convert

var sliceOfLife = require('slice-of-life');
app.use(sliceOfLife.convertMiddleware({defaultPerPage: 100}));
//then in a route: 
app.get('/customers', function (reqres) {
  //will print {skip: xx, limit: xx}; 

SliceOfLife has also a linker class to create links to other pages.

var Linker = require('slice-of-life').Linker,
    linker = new Linker('$per_page&page=$page');
var links = linker.createLinks({
 per_page: 10,
 total: 100,
 page: 5
/* It will return an object like this:
    'first': '',
    'prev': '',
    'next': '',
    'last': ''

The create links method needs these options:

  • per_page: the amount of objects to retrieve in each page
  • total: the total amount of objects in the object set
  • page: the current page index (base 0)

The createLinksHeader method of the linker does the same thing than createLinks but it returns an string with the Links header format.

var links = linker.createLinksHeader({
 per_page: 10,
 total: 100,
 page: 5
It will print something like:
<>; rel="first",
  <>; rel="prev",
  <>; rel="next",
  <>; rel="last"
(without line breaks)

I know how hard and well-engineered this thing seems but I don't want to be billionary with this module, so this is MIT licensed :)