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

paginate-arrays-js

v1.2.2

Published

A library to paginate arrays in javascript

Downloads

69

Readme

paginate-arrays-js

paginate-arrays-js is a simple and easy-to-use NPM package that allows you to paginate your array data in JavaScript/TypeScript projects. This package is flexible and can be used with any data structure or framework.

Installation

npm install paginate-arrays-js

Usage Paginate Default

Import the paginate function from the paginate-arrays-js package:

import { paginate } from "paginate-arrays-js";

Parameters

The paginate function accepts an object with the following properties:

  • data: The array of data to be paginated.
  • page: The current page number.
  • perPage: The number of items to be displayed per page.
  • url: The base URL for the paginated resource.

Return Value

The paginate function returns an object with the following properties:

  • data: The paginated data.
  • pagination: The pagination information.

Example

import { paginate } from "paginate-arrays-js";

const data = [
  { id: 1, name: "John" },
  { id: 2, name: "Jane" },
  { id: 3, name: "Doe" },
  // ...
];

const currentPage = 1;
const itemsPerPage = 10;
const url = "/api/users";

const paginatedData = paginate({
  data,
  page: currentPage,
  perPage: itemsPerPage,
  url,
});

console.log(paginatedData);

This will return an object with paginated data and pagination information, like so:

{
  data: [
    { id: 1, name: 'John' },
    { id: 2, name: 'Jane' },
    // ...
  ],
  pagination: {
    totalPage: 5,
    nextPage: 2,
    prevPage: null,
    firstPage: 1,
    lastPage: 5,
    from: 1,
    to: 10,
    perPage: 10,
    total: 50,
    currentPage: 1,
    hasPrevPage: false,
    hasNextPage: true,
    url: '/api/users?page=1'
  }
}

You can then use the paginated data and the pagination object to display paginated content and navigation in your application.

Usage Cursor Pagination

Import the cursorPaginate function from the paginate-arrays-js package:

import { cursorPaginate } from "paginate-arrays-js";

Parameters

The cursorPaginate function accepts an object with the following properties:

  • data: The array of data to be paginated.
  • identifier: The unique identifier for the cursor pagination.
  • perPage: The number of items to be displayed per page.
  • after: The cursor for the next page (optional).
  • before: The cursor for the previous page (optional).

Return Value

The cursorPaginate function returns an object with the following properties:

  • data: The paginated data.
  • pagination: The pagination information.

Example

import { cursorPaginate } from "paginate-arrays-js";

const data = [
  { id: 1, name: "John" },
  { id: 2, name: "Jane" },
  { id: 3, name: "Doe" },
  // ...
];

const itemsPerPage = 10;
const identifier = "id";
const after = 1;

const paginatedData = cursorPaginate({
  data,
  identifier,
  perPage: itemsPerPage,
  after,
});

console.log(paginatedData);

This will return an object with paginated data and pagination information, like so:

{
  data: [
    { id: 2, name: 'Jane' },
    // ...
  ],
  pagination: {
    totalPages: 5,
    hasPrevPage: true,
    hasNextPage: true,
    startCursor: 2,
    endCursor: 11,
  }
}

You can then use the paginated data and the pagination object to display paginated content and navigation in your application.

Contributing

If you'd like to contribute to the development of paginate-arrays-js, please feel free to submit pull requests or report issues on the repository.

License

paginate-arrays-js is open-sourced software licensed under the MIT license.