├── .gitignore ├── test └── index.test.js ├── tea.yaml ├── LICENSE ├── package.json ├── src └── pagination.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Directories 2 | node_modules/ 3 | dist/ 4 | coverage/ 5 | .cache/ 6 | 7 | # Files 8 | .env 9 | .DS_Store -------------------------------------------------------------------------------- /test/index.test.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'); 2 | const generatePagination = require('../src/pagination'); 3 | 4 | describe('Pagination Component', () => { 5 | it('should generate correct pagination array', () => { 6 | const pagination = generatePagination(5, 10, 5); 7 | assert.deepStrictEqual(pagination, [3, 4, 5, 6, 7]); 8 | }); 9 | 10 | it('should handle edge cases', () => { 11 | const pagination1 = generatePagination(1, 5, 5); 12 | assert.deepStrictEqual(pagination1, [1, 2, 3, 4, 5]); 13 | 14 | const pagination2 = generatePagination(10, 10, 5); 15 | assert.deepStrictEqual(pagination2, [6, 7, 8, 9, 10]); 16 | }); 17 | }); 18 | -------------------------------------------------------------------------------- /tea.yaml: -------------------------------------------------------------------------------- 1 | # https://tea.xyz/what-is-this-file 2 | --- 3 | version: 1.0.0 4 | codeOwners: 5 | - '0x9BAac40A197bbb110ae5B2f31342D8d15D734832' 6 | - '0x8Ca738Dd8a2527A9f05C8da66C77E852d4C7F48f' 7 | - '0x4701cC6D8F3B1720Df17AA0F41DFcF4a31991535' 8 | - '0x5230F2b5468ad7b6135A2348ffcA35377819280D' 9 | - '0x67DcA3fcb27b12623Ceca72358831B0F1ec331db' 10 | - '0x350D5Baa2291F8eAA6198775af6b7896BDe34309' 11 | - '0xfC790a0249e8C56522965c1Bfb22693a6844A83a' 12 | - '0x00Ec93b1332C2e2eb5f91BE8CD85bf8c6158386c' 13 | - '0xf9C60A75cBf404E52256f83d40ce1dBf78DA788e' 14 | - '0x56aBed26aAb3f3396Fe0AF5dABDd4319486042DF' 15 | - '0xd6A983395faD5D29c256B4Af7f31D8e86800e7CF' 16 | - '0x7fbF8be9f66C5251643425Bb279AAbDcAD0C2C85' 17 | - '0x303F8E3e470bBb0F37557e9309faC2ADA53DfCc8' 18 | - '0xDb0B0798a69ab23f86D4eac88e84272987c2798B' 19 | - '0xd24433D894D739785651F21CB93E586fbFfeB551' 20 | quorum: 1 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 ONE DIONYS 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "onedionys-pagination-component", 3 | "version": "5.0.0", 4 | "description": "One Dionys (Pagination Component) - A component to divide content into multiple pages, making it easier for users to navigate.", 5 | "main": "src/pagination.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "scripts": { 10 | "test": "mocha" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/onedionys/onedionys-pagination-component.git" 15 | }, 16 | "keywords": [ 17 | "onedionys", 18 | "tea", 19 | "package-manager", 20 | "pagination-component" 21 | ], 22 | "author": "One Dionys", 23 | "license": "ISC", 24 | "bugs": { 25 | "url": "https://github.com/onedionys/onedionys-pagination-component/issues" 26 | }, 27 | "homepage": "https://github.com/onedionys/onedionys-pagination-component#readme", 28 | "dependencies": { 29 | "@types/chai": "^4.3.12", 30 | "@types/mocha": "^10.0.6", 31 | "chai": "^5.1.0", 32 | "mocha": "^10.3.0" 33 | }, 34 | "devDependencies": { 35 | "chai": "^5.1.0", 36 | "mocha": "^10.3.0" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/pagination.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Pagination Component 3 | * @param {number} currentPage - The current page number. 4 | * @param {number} totalPages - The total number of pages. 5 | * @param {number} visiblePages - The number of visible page links. 6 | * @returns {Array} - An array of page numbers to be displayed. 7 | */ 8 | function generatePagination(currentPage, totalPages, visiblePages) { 9 | let startPage, endPage; 10 | 11 | if (totalPages <= visiblePages) { 12 | startPage = 1; 13 | endPage = totalPages; 14 | } else { 15 | const halfVisible = Math.floor(visiblePages / 2); 16 | if (currentPage <= halfVisible) { 17 | startPage = 1; 18 | endPage = visiblePages; 19 | } else if (currentPage + halfVisible >= totalPages) { 20 | startPage = totalPages - visiblePages + 1; 21 | endPage = totalPages; 22 | } else { 23 | startPage = currentPage - halfVisible; 24 | endPage = currentPage + halfVisible; 25 | } 26 | } 27 | 28 | return Array.from({ length: endPage - startPage + 1 }, (_, i) => startPage + i); 29 | } 30 | 31 | module.exports = generatePagination; 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

Welcome to One Dionys - Pagination Component! 👋

2 | 3 |

A component to divide content into multiple pages, making it easier for users to navigate. 💖

4 | 5 |

6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |

14 | 15 | ## 💾 Requirements 16 | 17 | * `Web Browser` - Can be used as an emulator to build applications. Example [Chrome, Firefox, Safari & Opera]. 18 | * `Internet` - Because many use CDN and to make it easier to find solutions to all problems. 19 | 20 | ## 🎯 How To Use 21 | 22 | #### Example Syntax 23 | 24 | ```javascript 25 | const generatePagination = require('pagination-component'); 26 | 27 | const currentPage = 5; 28 | const totalPages = 10; 29 | const visiblePages = 5; 30 | 31 | const pagination = generatePagination(currentPage, totalPages, visiblePages); 32 | console.log(pagination); // Output: [3, 4, 5, 6, 7] 33 | ``` 34 | 35 | #### Explanation 36 | 37 | The `generatePagination` function takes three parameters: 38 | * `currentPage`: The current page number. 39 | * `totalPages`: The total number of pages. 40 | * `visiblePages`: The number of visible page links. 41 | It returns an array of page numbers to be displayed based on the current page, total pages, and visible pages. 42 | 43 | #### Return Value 44 | 45 | * The function returns an array of page numbers to be displayed. 46 | 47 | ## 📆 Release Date 48 | 49 | * v1.0.0 : 07 March 2024 50 | * v1.0.1 : 11 March 2024 51 | * v4.0.0 : 11 March 2024 52 | * v4.0.1 : 13 March 2024 53 | * v4.0.2 : 18 March 2024 54 | * v5.0.0 : 31 March 2024 55 | 56 | ## 🧑 Author 57 | 58 | * Facebook : Oned Ionys 59 | * Instagram : @onedionys 60 | * Twitter : @onedionys 61 | * LinkedIn : @onedionys 62 | 63 | ## 📝 License 64 | 65 | * Copyright © 2024 One Dionys 66 | * **One Dionys - Pagination Component is an open source project licensed under the MIT license** 67 | 68 | ## ☕️ Suppport & Donation 69 | 70 | Love One Dionys - Pagination Component? Support this project by donating or sharing with others in need. 71 | 72 | 73 | 74 | **Made with ❤️ One Dionys** 75 | --------------------------------------------------------------------------------