{"id":11196,"library":"jw-paginate","title":"JW Paginate","description":"jw-paginate provides a standalone, framework-agnostic utility for calculating pagination logic, implemented in both JavaScript and TypeScript. It offers a pure function that, given parameters like total items, current page, page size, and maximum pages to display, returns a comprehensive object containing all necessary pagination details. This includes total pages, start and end item indices, start and end page numbers, and an array of page numbers to render in a UI. The current stable version is 1.0.4. Due to its minimalist 'pure logic' design and comprehensive feature set for its specific domain, the library has a very infrequent release cadence, with its last significant update occurring in 2018. Its primary differentiator is its complete lack of external dependencies and framework agnosticism, allowing seamless integration into any web project without introducing framework-specific couplings or unnecessary bloat. It also ships with first-class TypeScript type definitions, providing an enhanced development experience for TypeScript users.","status":"maintenance","version":"1.0.4","language":"javascript","source_language":"en","source_url":"https://github.com/cornflourblue/jw-paginate","tags":["javascript","typescript"],"install":[{"cmd":"npm install jw-paginate","lang":"bash","label":"npm"},{"cmd":"yarn add jw-paginate","lang":"bash","label":"yarn"},{"cmd":"pnpm add jw-paginate","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The main pagination function is exported as the default export. For TypeScript users, type definitions are included.","wrong":"import { paginate } from 'jw-paginate'; // paginate is a default export\nconst paginate = require('jw-paginate'); // CommonJS import pattern","symbol":"paginate","correct":"import paginate from 'jw-paginate';"},{"note":"While `PaginateResult` is a type, it's a common pattern to import types alongside the default export for clarity in TypeScript. It represents the shape of the object returned by the `paginate` function.","symbol":"PaginateResult","correct":"import paginate, { PaginateResult } from 'jw-paginate';"}],"quickstart":{"code":"import paginate from 'jw-paginate';\n\ninterface Item {\n  id: number;\n  name: string;\n}\n\nconst allItems: Item[] = Array.from({ length: 150 }, (_, i) => ({\n  id: i + 1,\n  name: `Item ${i + 1}`\n}));\n\n// Simulate a request for page 3 with 10 items per page\nconst currentPage = 3;\nconst pageSize = 10;\nconst maxPagesToShow = 7;\n\nconst page = paginate(\n  allItems.length, \n  currentPage, \n  pageSize, \n  maxPagesToShow\n);\n\nconst pageOfItems = allItems.slice(page.startIndex, page.endIndex + 1);\n\nconsole.log(`Total Items: ${page.totalItems}`);\nconsole.log(`Total Pages: ${page.totalPages}`);\nconsole.log(`Current Page: ${page.currentPage}`);\nconsole.log(`Page Numbers to Display: ${page.pages}`);\nconsole.log(`Showing items ${page.startIndex + 1} to ${page.endIndex + 1}:`);\npageOfItems.forEach(item => console.log(` - ${item.name}`));\n\n// Example: Pagination for a different scenario\nconst anotherPage = paginate(50, 1, 5, 5);\nconsole.log('\\n--- Another Pagination Example ---');\nconsole.log(`Total Pages: ${anotherPage.totalPages}`);\nconsole.log(`Page Numbers: ${anotherPage.pages}`);","lang":"typescript","description":"Demonstrates how to use the `paginate` function to calculate page numbers and slice a data array for a specific page. It initializes an array of items and then uses the `jw-paginate` function to determine the pagination details for a given page, logging the results."},"warnings":[{"fix":"Always add `+ 1` to the `endIndex` when using `Array.prototype.slice()`: `yourArray.slice(page.startIndex, page.endIndex + 1)`.","message":"The `paginate` function returns a `startIndex` and `endIndex` which are zero-based. When using these indices to slice an array in JavaScript, `array.slice(startIndex, endIndex + 1)` is typically required to include the `endIndex` element.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Review the source code to ensure it meets your project's standards for modern practices if relying on a long-term unmaintained library. For pure logic, this is often less of a concern.","message":"The library has seen no significant updates since 2018. While its 'pure logic' nature means it's less susceptible to breaking changes or security vulnerabilities than libraries with active dependencies, it also implies no new features or active maintenance for modern JavaScript patterns or framework integrations.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always explicitly pass all parameters to `paginate(totalItems, currentPage, pageSize, maxPages)` to ensure predictable behavior, even if some values match the defaults.","message":"The `paginate` function uses default parameters for `currentPage`, `pageSize`, and `maxPages`. While convenient, omitting these can lead to unexpected pagination results if you intend different values from the defaults (1, 10, 10 respectively).","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change your import statement to `import paginate from 'jw-paginate';` or `const paginate = require('jw-paginate').default;` for CommonJS.","cause":"Attempting to import `paginate` as a named export when it is a default export.","error":"TypeError: paginate is not a function"},{"fix":"Ensure `totalItems` is a positive integer representing the total count of all items. Verify `maxPages` is set to a reasonable number of page links you want to display, not the total number of pages.","cause":"Incorrect `totalItems` value passed to the `paginate` function, often 0 or a non-positive number, or misinterpretation of `maxPages`.","error":"console.log(page.pages.length) shows 0 or incorrect number of pages"},{"fix":"Ensure all numerical parameters passed to `paginate` are valid numbers. If values come from URL query parameters or user input, parse them to integers (e.g., `parseInt(value, 10)`) and handle potential `NaN` results before passing to the function.","cause":"Non-numeric or invalid values passed for `currentPage`, `pageSize`, or `maxPages`.","error":"Page numbers are showing up as `NaN` or unexpected values in my UI"}],"ecosystem":"npm"}