2.3.3 • Published 2 years ago

paging-util v2.3.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

:arrow_down: Install package

Install with yarn:

$ yarn add paging-util

:file_folder: Basic Usage

  • Using (import/export) ESM with TypeScript:
import { paginate, offsetBased, range } from 'paging-util';

const offset = offsetBased(2, 10); // => output: 10

const paging = paginate({ recods: 100, setRange: true });

{
  offset: 0,
  pagination: {
    records: 100,
    totalPages: 10,
    currentPage: 1,
    firstPage: 1,
    limit: 10,
    next: 2,
    previous: null,
    hasNext: true,
    hasPrevious: false,
    isLastPage: false,
    firstIndex: 0,
    lastIndex: 9,
    length: 10,
    range: [
      1, 2, 3, 4, 5,
      6, 7, 8, 9, 10
    ], // array: range/total pages
  },
  constants: {
    MIN_LIMIT: 10,
    MAX_LIMIT: 20,
  }
}

range(1, 5); // output: [1, 2, 3, 4, 5]

range(5, 1); // output: [5, 4, 3, 2, 1]

range(-5, 1);  // output: [-5, -4, -3, -2, -1,  0,  1]
  • Using (require/node) CommonJS:
const { paginate } = require('paging-util');

const { range } = paginate({ records: 100, setRange: true });

const odds = range.filter(value => !(value % 2));

console.log(odds); // output: [2, 4, 6, 8, 10]

:wrench: API

  • paginate({ records: 100, ...options }):

Options

Property nameRequiredDefaultDescription
recordstrue-Resources/records
pagefalse1Current page
limitfalse10Total (resources) to show per page
setRangefalsefalseSet array of pages
setConstantsfalsetrueGet constants
minfalse10Min limit
maxfalse20Max limit

Output:

  • Object:
    • records - Total records/resources to paging.
    • totalPages - Total pages
    • currentPage - Current page, default: 1
    • firstPage - First page, default: 1
    • limit - Total items to show per page, default: 10
    • next - Next page
    • previous - Previous page,
    • hasNext - true or false
    • hasPrevious - true or false
    • firstIndex, lastIndex - First and last result (index).
    • length - Results length.
    • offSet - Offset-based pagination.
    • constants - Fixed values.
    • range - Array of pages, default: null

  • range(start, end?, step?): Array of pages.

  • Offset-based pagination algorithm/logic:

import logic from 'node/logic';

logic.explode(); // 😄

/**
 * Output:
 *  - Offset-based pagination:
 *    ((1 - 1) * 10) = 0 * 10 = 0
 *    ((2 - 1) * 10) = 1 * 10 = 10
 *
 *    Position 1: 0 - 9
 *    Position 2: 10 - 19
 *    Position 3: 20 - 29
 */

:open_hands: Contributing

Contributions, issues and feature requests are welcome!Feel free to check issues page.

📝 License

Copyright © 2021 José Lucas. This project is MIT licensed.

2.3.2

2 years ago

2.2.2

2 years ago

2.3.3

2 years ago

2.1.2

2 years ago

2.0.2

2 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.2

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago