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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@noravel/pagination

v1.2.0

Published

This is a support library for my personal projects.

Readme

Noravel pagination

This is a support library for Nam's projects.

Content

Installation

npm install @noravel/pagination

Usage

const { LengthAwarePaginator } = require('@noravel/pagination');
const items = [
  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
  61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
  81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
];

const paginator = new LengthAwarePaginator(items, 100, 5, 1);

console.log(paginator.jsonSerialize());

/*
{
    current_page: 1,
    data: [1, 2, 3, 4, 5],
    first_page_url: '/?page=1',
    from: 1,
    last_page: 20,
    last_page_url: '/?page=20',
    links: [
      { page: '1', url: '/?page=1', active: true },
      { page: '2', url: '/?page=2', active: false },
      { page: '3', url: '/?page=3', active: false },
      { page: '4', url: '/?page=4', active: false },
      { page: '...', url: null, active: false } ,
      { page: '19', url: '/?page=19', active: false },
      { page: '20', url: '/?page=20', active: false },
    ],
    next_page_url: '/?page=2',
    path: '/',
    per_page: 5,
    prev_page_url: null,
    to: 5,
    total: 100,
}
*/

Sometimes you may wish to create a pagination instance manually, passing it an array of items that you already have in memory. You may do so by creating either an SimplePaginator or LengthAwarePaginator depending on your needs.

SimplePaginator

The SimplePaginator class does not need to know the total number of items in the result set; however, because of this, this class does not have methods for retrieving the index of the last page.

new SimplePaginator<T>(items: T[], perPage: number = 10, currentPage: number = 1)

Description:

  • items: The items to set.
  • perPage: The number of items per page.
  • currentPage: The current page number.
const { SimplePaginator } = require('@noravel/pagination');
const items = [
  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
  61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
  81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
];

const paginator = new SimplePaginator(items, 5, 1);

console.log(paginator.jsonSerialize());

/*
{
  current_page: 1,
  data: [1, 2, 3, 4, 5],
  first_page_url: '/?page=1',
  from: 1,
  next_page_url: '/?page=2',
  path: '/',
  per_page: 5,
  prev_page_url: null,
  to: 5
}
*/

LengthAwarePaginator

The LengthAwarePaginator accepts almost the same arguments as the SimplePaginator; however, it requires a count of the total number of items in the result set.

new LengthAwarePaginator<T>(items: T[], total: number, perPage: number = 10, currentPage: number = 1)

Description:

  • items: The items to set.
  • total: The total number of items in the result set.
  • perPage: The number of items per page.
  • currentPage: The current page number.
const { LengthAwarePaginator } = require('@noravel/pagination');
const items = [
  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
  61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
  81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
];

const paginator = new LengthAwarePaginator(items, 100, 5, 7);

console.log(paginator.jsonSerialize());

/*
{
    current_page: 7,
    data: [31, 32, 33, 34, 35],
    first_page_url: '/?page=1',
    from: 31,
    last_page: 20,
    last_page_url: '/?page=20',
    links: [
      { page: '1', url: '/?page=1', active: false },
      { page: '2', url: '/?page=2', active: false },
      { page: '...', url: null, active: false },
      { page: '5', url: '/?page=5', active: false },
      { page: '6', url: '/?page=6', active: false },
      { page: '7', url: '/?page=7', active: true },
      { page: '8', url: '/?page=8', active: false },
      { page: '9', url: '/?page=9', active: false },
      { page: '...', url: null, active: false },
      { page: '19', url: '/?page=19', active: false },
      { page: '20', url: '/?page=20', active: false },
    ],
    next_page_url: '/?page=8',
    path: '/',
    per_page: 5,
    prev_page_url: '/?page=6',
    to: 35,
    total: 100,
}
*/

Adjusting the Pagination Link Window

When the paginator displays pagination links, the current page number is displayed as well as links for the three pages before and after the current page. Using the onEachSide method, you may control how many additional links are displayed on each side of the current page within the middle, sliding window of links generated by the paginator.

const { LengthAwarePaginator } = require('@noravel/pagination');
const items = [
  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
  61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
  81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
];

const paginator = new LengthAwarePaginator(items, 100, 5, 7);
paginator.onEachSide(1);

console.log(paginator.jsonSerialize());

/*
{
    current_page: 7,
    data: [31, 32, 33, 34, 35],
    first_page_url: '/?page=1',
    from: 31,
    last_page: 20,
    last_page_url: '/?page=20',
    links: [
      { page: '1', url: '/?page=1', active: false },
      { page: '2', url: '/?page=2', active: false },
      { page: '...', url: null, active: false },
      { page: '6', url: '/?page=6', active: false },
      { page: '7', url: '/?page=7', active: true },
      { page: '8', url: '/?page=8', active: false },
      { page: '...', url: null, active: false },
      { page: '19', url: '/?page=19', active: false },
      { page: '20', url: '/?page=20', active: false },
    ],
    next_page_url: '/?page=8',
    path: '/',
    per_page: 5,
    prev_page_url: '/?page=6',
    to: 35,
    total: 100,
}
*/

Configuration

You can set the base URL by using the setBaseUrl method.

Paginator.setBaseUrl('https://your-application.example');

You can get the base URL by using the getBaseUrl method.

const baseUrl = Paginator.getBaseUrl();

The options for the paginator are:

export type PaginatorOptions = {
  path?: string;
  query?: Record<string, unknown>;
  fragment?: any;
  pageName?: string;
};

Description:

  • path: The path to the paginator. Ex: /users
  • query: The query parameters to the paginator. Ex: { foo: 'bar' }
  • fragment: The fragment to the paginator. Ex: #foo
  • pageName: The name of the page parameter. Ex: p
const { Paginator, SimplePaginator } = require('@noravel/pagination');
Paginator.setBaseUrl('https://your-application.example');
const paginator = new SimplePaginator([1, 2, 3, 4, 5]);
paginator.setOptions({
  path: '/users',
  query: { q: 'test' },
  fragment: 'foo',
  pageName: 'p',
});
console.log(paginator.url(2)); // https://your-application.example/users?q=test&p=2#foo