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

paginator-builder

v0.0.2

Published

Calculate pagination array for easier building of pagination markup.

Downloads

3

Readme

pagination-builder

Travis npm version npm downloads

Pagination builder is the simple function that generates pagination array by passed current page and total pages. It performs all the logic for you and you just have to draw the result.

Installation

For install stable version:

npm install --save pagination-builder

This assumes you are using npm as your package manager.
You can use this function as CommonJS module. This module is what you get when you import paginationBuilder in a Webpack, Browserify, or a Node environment.

If you don’t use a module bundler, it’s also fine. The paginationBuilder npm package includes precompiled production and development UMD builds in the dist folder. They can be used directly without a bundler and are thus compatible with many popular JavaScript module loaders and environments. For example, you can drop a UMD build as a <script>path to /dist/pagination-builder.js</script> on the page. The UMD build make paginationBuilder available as a window.paginationBuilder.default global variable.

The source code is written in ES2015 but we precompile both CommonJS and UMD builds to ES5 so they work in any modern browser. You don’t need to use Babel or a module bundler.

Example of usage

import paginationBuilder from 'pagination-builder';

const paginationStructure = paginationBuilder(5, 10);
const listItems = paginationStructure.map(item => {
    let className = 'pagination__item';

    if (item.isActive) {
        className += ' pagination__item_active';
    }

    if (item.isDisabled) {
        className += ' pagination__item_disabled';
    }

    const innerElement = item.isDisabled
        ? `<span class="pagination__element">${item.text}</span>`
        : `<a class="pagination__link" href="?page${item.page}">${item.text}</a>`;

    return `<li class="${className}">${innerElement}</li>`;
});

console.log(['<ul class="pagination">', ...listItems, '</ul>'].join(''));

Will print to console:

<ul class="pagination">
    <li class="pagination__item"><a class="pagination__link" href="?page4">Prev</a></li>
    <li class="pagination__item"><a class="pagination__link" href="?page1">1</a></li>
    <li class="pagination__item"><a class="pagination__link" href="?page2">2</a></li>
    <li class="pagination__item"><a class="pagination__link" href="?page3">3</a></li>
    <li class="pagination__item"><a class="pagination__link" href="?page4">4</a></li>
    <li class="pagination__item pagination__item_active"><a class="pagination__link" href="?page5">5</a></li>
    <li class="pagination__item"><a class="pagination__link" href="?page6">6</a></li>
    <li class="pagination__item"><a class="pagination__link" href="?page7">7</a></li>
    <li class="pagination__item pagination__item_disabled"><span class="pagination__element">...</span></li>
    <li class="pagination__item"><a class="pagination__link" href="?page10">10</a></li>
    <li class="pagination__item"><a class="pagination__link" href="?page6">Next</a></li>
</ul>

API

Module has one default exported function: paginationBuilder(currentPage, totalPages[, options={Object}])

Options object params

|Param|Default value|Description| |-----|-------------|-----------| |prevNextLinks|true|Generate items with prev and next text| |dots|'...'|Generate dots between items, if false it won't generated| |prevText|'Prev'|Text that will be used for previous item, if prevNextLinks is true| |nextText|'Next'|Text that will be used for next item, if prevNextLinks is true| |afterFirst|2|Count of items that will be returned after first page, before dots| |beforeLast|2|Count of items that will be returned before last page, after dots| |beforeCurrent|2|Count of items that will be returned before current page, after dots| |afterCurrent|2|Count of items that will be returned after current page, before dots|

Example

Result of call: paginationBuilder(10, 22, { afterCurrent: 3 }).map(item => item.text).join(' ');

Prev 1 2 3 ... 8 9 10 11 12 13 ... 20 21 22 Next
 ^    |---|   |---|  |--------|   |-----|     ^
 |      ^       ^        ^           ^        |
 |      |       |        |           |        |
 |      |       |   afterCurrent     |        |
 |      | beforeCurrent         beforeLast    |
 | afterFirst                                 |
 prevText                                 nextText