# page-numbers

> Stateless page number generator

Latest version **0.0.1** (published 2021-08-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install page-numbers
pnpm add page-numbers
yarn add page-numbers
bun add page-numbers
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2021-08-24 |
| First published | 2021-08-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 52.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Rouslan Placella |
| Maintainers | roccivic |
| Keywords | page-numbers, page-range, pager, pagination, paginate, paginator, paging, navigation |

## Links

- npm: https://www.npmjs.com/package/page-numbers
- Repository: https://github.com/roccivic/page-numbers
- Homepage: https://github.com/roccivic/page-numbers#readme
- Issues: https://github.com/roccivic/page-numbers/issues
- npm.io page: https://npm.io/package/page-numbers

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 0.0.1 (latest) — 2021-08-24

## README

# page-numbers

[![npm version](https://badge.fury.io/js/page-numbers.svg)](https://badge.fury.io/js/page-numbers) [![Build and test](https://github.com/roccivic/page-numbers/actions/workflows/build.yml/badge.svg)](https://github.com/roccivic/page-numbers/actions/workflows/build.yml)

This is a stateless page number generator.

The purpose of this package is only to generate a list
of items to be displayed in a pagination control.

Maintaining state, rendering of the actual control and presentation to the user is out of scope.


# Get it

```sh
yarn add page-numbers
```

# Use it
## Hello world

<img src="images/hello.gif" alt="hello" width="626" height="163" />

```ts
import pager from 'page-numbers';

const total = 5;
const current = 2; // page 3 (zero-indexed)

const pages = pager(total, current);

// 1,2,[3],4,5
console.log(pages.toString())

// [
//   {"value":0,"key":"1","isCurrent":false},
//   {"value":1,"key":"2","isCurrent":false},
//   {"value":2,"key":"3","isCurrent":true},
//   {"value":3,"key":"4","isCurrent":false},
//   {"value":4,"key":"5","isCurrent":false}
// ]
console.log(JSON.stringify(pages))
```

## With default options

<img src="images/defaults.gif" alt="defaults" width="626" height="163" />

```ts
const options = {
   // number of items the pager should return
  target: 15,
  neighbours: {
    // number of pages to display
    // near the first and last pages
    edge: 1,
    // number of pages to display
    // on either side of the current page
    current: 2,
  },
  // a gap region to focus on.
  // more on this in the next example
  focus: undefined,
}

const pages = pager(100, 19, options);

// The horizontal ellipsis here surrond a gap region in the page numbers
// 1,2,…3-17…,18,19,[20],21,22,…23-47…,48,…49-72…,73,…74-98…,99,100
console.log(pages.toString())

// [
//   {"value":0,"key":"1","isCurrent":false},
//   {"value":1,"key":"2","isCurrent":false},
//   {"from":2,"to":16,"key":"3-17","isCurrent":false},
//   {"value":17,"key":"18","isCurrent":false},
//   {"value":18,"key":"19","isCurrent":false},
//   {"value":19,"key":"20","isCurrent":true},
//   {"value":20,"key":"21","isCurrent":false},
//   {"value":21,"key":"22","isCurrent":false},
//   {"from":22,"to":46,"key":"23-47","isCurrent":false},
//   {"value":47,"key":"48","isCurrent":false},
//   {"from":48,"to":71,"key":"49-72","isCurrent":false},
//   {"value":72,"key":"73","isCurrent":false},
//   {"from":73,"to":97,"key":"74-98","isCurrent":false},
//   {"value":98,"key":"99","isCurrent":false},
//   {"value":99,"key":"100","isCurrent":false}
// ]
console.log(JSON.stringify(pages))
```

## With a focus region

If a gap region from the previous example is passed to the pager method
in the `options.focus` property, it will include more items in that region.

<img src="images/focus.gif" alt="focus" width="626" height="163" />

```ts
const pages = pager(100, 19);
const gap = pages[2];
const focusedPages = pager(100, 19, { focus: gap })

// Note the large gap between pages 22 and 99
// 1,2,3,…4-7…,8,…9-11…,12,…13-16…,17,18,19,[20],21,22,…23-98…,99,100
console.log(pages.toString())

// [
//   {"value":0,"key":"1","isCurrent":false},
//   {"value":1,"key":"2","isCurrent":false},
//   {"value":2,"key":"3","isCurrent":false},
//   {"from":3,"to":6,"key":"4-7","isCurrent":false},
//   {"value":7,"key":"8","isCurrent":false},
//   {"from":8,"to":10,"key":"9-11","isCurrent":false},
//   {"value":11,"key":"12","isCurrent":false},
//   {"from":12,"to":15,"key":"13-16","isCurrent":false},
//   {"value":16,"key":"17","isCurrent":false},
//   {"value":17,"key":"18","isCurrent":false},
//   {"value":18,"key":"19","isCurrent":false},
//   {"value":19,"key":"20","isCurrent":true},
//   {"value":20,"key":"21","isCurrent":false},
//   {"value":21,"key":"22","isCurrent":false},
//   {"from":22,"to":97,"key":"23-98","isCurrent":false},
//   {"value":98,"key":"99","isCurrent":false},
//   {"value":99,"key":"100","isCurrent":false}
// ]
console.log(JSON.stringify(pages))
```

## API
### Imports
```ts
// Interfaces
import { Item, Options, Neighbours } from 'page-numbers';

// Classes (implementations of Item)
import { GapItem, NumberItem } from 'page-numbers';

// The pager method
import pager from 'page-numbers';
```

### Method signature
```ts
Item[] pager(
  total : number,
  current : number,
  options : Options
)
```

### Types
All public types are [here](src/models/public.ts).

The types are bundled with the project, so you can import
the module directly into your TypeScript project.

# Develop it
```sh
yarn # install
yarn build
yarn test
yarn coverage
```

---
_Source: https://npm.io/package/page-numbers · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
