npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

slice-of-life

v0.0.7

Published

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

Downloads

4

Readme

Build Status

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 :)

Installation

npm install slice-of-life

The convert method

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
});

console.log(pageData);

// will print {skip: 500, limit: 100} as simple as that.

The convert middleware

There is also an express middleware to do the convert

var sliceOfLife = require('slice-of-life');

//setup
app.use(sliceOfLife.convertMiddleware({defaultPerPage: 100}));

//then in a route:

app.get('/customers', function (req, res) {
  console.log(req.pageData);
  //will print {skip: xx, limit: xx};
});

The Linker

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

var Linker = require('slice-of-life').Linker,
	linker = new Linker('http://awesome.com/products?per_page=$per_page&page=$page');

var links = linker.createLinks({
 per_page: 10,
 total: 100,
 page: 5
});

console.log(links);

/* It will return an object like this:
  {
    'first': 'http://awesome.com/products?per_page=30&page=10',
	'prev': 'http://awesome.com/products?per_page=10&page=0',
	'next': 'http://awesome.com/products?per_page=30&page=2',
	'last': 'http://awesome.com/products?per_page=30&page=10'
  }
*/

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)

linker.createLinksHeader

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
});

console.log(links);

/*
It will print something like:

<http://awesome.com/customers?per_page=10&page=0>; rel="first",
  <http://awesome.com/customers?per_page=10&page=1>; rel="prev",
  <http://awesome.com/customers?per_page=10&page=3>; rel="next",
  <http://awesome.com/customers?per_page=10&page=10>; rel="last"

(without line breaks)
*/

License

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 :)