# @writetome51/batchinator

> TypeScript/JavaScript class intended to help a separate Paginator class paginate data that can only be saved in-memory one batch at-a-time

Latest version **2.0.2** (published 2019-04-19) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @writetome51/batchinator
pnpm add @writetome51/batchinator
yarn add @writetome51/batchinator
bun add @writetome51/batchinator
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2019-04-19 |
| First published | 2019-01-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 25.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Steve Thompson |
| Maintainers | writetome51 |
| Keywords | array, display, data, app, batch, paginator, pagination, typescript, class |

## Links

- npm: https://www.npmjs.com/package/@writetome51/batchinator
- Repository: https://github.com/writetome51/batchinator
- Homepage: https://github.com/writetome51/batchinator#readme
- Issues: https://github.com/writetome51/batchinator/issues
- npm.io page: https://npm.io/package/@writetome51/batchinator

## Dependencies (5)

- [@writetome51/not](https://npm.io/package/@writetome51/not.md) ~1.0.5
- [error-if-not-integer](https://npm.io/package/error-if-not-integer.md) ~1.0.0
- [@writetome51/in-range](https://npm.io/package/@writetome51/in-range.md) ~1.0.4
- [@writetome51/base-class](https://npm.io/package/@writetome51/base-class.md) ~0.0.3
- [@writetome51/get-rounded-up-down](https://npm.io/package/@writetome51/get-rounded-up-down.md) ~1.0.10

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 2.0.2 (latest) — 2019-04-19
- 2.0.1 — 2019-03-05
- 2.0.0 — 2019-01-30
- 1.0.0 — 2019-01-29

## README

# Batchinator

A TypeScript/JavaScript class intended to help a separate Paginator class paginate data  
that can only be saved in-memory one batch at-a-time, where each batch is taken from a  
much bigger data set that can't be completely fetched all at once.  
A single batch is measured by the number of items it has.  
A batch is also defined as the total number of items the Paginator can handle all at once.

An example: if the user is clicking thru pagination controls and clicks to page 10, it's  
this class' job to figure out which batch page 10 is in, tell the data-fetching tool what  
batch to fetch, and tell the Paginator what page to show.



## Constructor

```
constructor()
```

## Properties
```
// The first 3 properties must be set before doing anything else:

totalDataCount: number;
    // number of items in entire data set.

itemsPerBatch: number;
    
itemsPerPage: number;

currentBatchNumber: number (read-only);
    // This is set by calling this.set_currentBatchNumber_basedOnPage(pageNumber) .
    // Its initial value is 1.

pagesPerBatch: number (read-only);

totalBatches: number (read-only);

totalPages: number (read-only);

className : string (read-only)
    // Not important.  Inherited from BaseClass.
```

## Methods
```
set_currentBatchNumber_basedOnPage(pageNumber): void
    // Figures out the batch number that contains pageNumber, and
    // assigns it to this.currentBatchNumber .

currentBatchContainsPage(pageNumber): boolean
    // Useful if you need to find out if the batch containing pageNumber 
    // is already the currently loaded batch.

getBatchNumberContainingPage(pageNumber): number

getCurrentPageNumberForPaginator(pageNumber): number
    // We assume the Paginator doesn't know it's handling batches of a larger data set, 
    // so this function translates the passed pageNumber into a different number, 
    // which it returns. This is the page number the Paginator needs to show.
    // Example:  say this.pagesPerBatch is 10. Say the user requests page 11. That page would be 
    // page 1 of batch 2.  You call this.set_currentBatchNumber_basedOnPage(11), fetch the 
    // batch, assign it to the Paginator, and call this.getCurrentPageNumberForPaginator(11). 
    // It will return 1. That's the page number Paginator must show.
```
The methods below are not important to know about in order to use this  
class.  They're inherited from [BaseClass](https://github.com/writetome51/typescript-base-class#baseclass) .
```	
protected   _createGetterAndOrSetterForEach(
                  propertyNames: string[],
                  configuration: IGetterSetterConfiguration
            ) : void
     /*********************
     Use this method when you have a bunch of properties that need getter and/or 
     setter functions that all do the same thing. You pass in an array of string 
     names of those properties, and the method attaches the same getter and/or 
     setter function to each property.
     IGetterSetterConfiguration is this object:
     {
         get_setterFunction?: (
             propertyName: string, index?: number, propertyNames?: string[]
         ) => Function,
             // get_setterFunction takes the property name as first argument and 
             // returns the setter function.  The setter function must take one 
             // parameter and return void.
     
         get_getterFunction?: (
             propertyName: string, index?: number, propertyNames?: string[]
         ) => Function
             // get_getterFunction takes the property name as first argument and 
             // returns the getter function.  The getter function must return something.
     }
     *********************/ 
   
   
protected   _returnThis_after(voidExpression: any) : this
    // voidExpression is executed, then function returns this.
    // Even if voidExpression returns something, the returned data isn't used.

protected   _runMethod_and_returnThis(
    callingObject, 
    method: Function, 
    methodArgs: any[], 
    additionalAction?: Function // takes the result returned by method as an argument.
) : this
```   

## Usage Example

```
export class PaginationDataController {

    constructor(
        private __batchinator: Batchinator,
        private __paginator: { data: any[], itemsPerPage: number, currentPageNumber: number },
        private __dataService: DataService // as of now, an imaginary interface
    ) { 
        // Set properties 'totalDataCount', 'itemsPerPage', and 'itemsPerBatch' before doing 
        // anything else with the batchinator:
        this.__batchinator.totalDataCount = this.__dataService.getTotalDataCount();
        this.__batchinator.itemsPerPage = this.__paginator.itemsPerPage;
        this.__batchinator.itemsPerBatch = 500;

        this.__loadBatchAndPage(1);
    }


    showPage(pageNumber) {
        if (this.__batchinator.currentBatchContainsPage(pageNumber)) {
            this.__showPageInCurrentBatch(pageNumber);
        }
        else this.__loadBatchAndPage(pageNumber);
    }


    private __showPageInCurrentBatch(pageNumber){
        this.__paginator.currentPageNumber = 
            this.__batchinator.getCurrentPageNumberForPaginator(pageNumber);
    }


    private __loadBatchAndPage(pageNumber){
        this.__loadBatchContainingPage(pageNumber);
        this.__showPageInCurrentBatch(pageNumber);
    }


    private __loadBatchContainingPage(pageNumber){
        this.__batchinator.set_currentBatchNumber_basedOnPage(pageNumber);

        this.__paginator.data = this.__dataService.getData(
            this.__batchinator.currentBatchNumber,
            this.__batchinator.itemsPerBatch
        );
    }


}
```

## Inheritance Chain

Batchinator<--[BaseClass](https://github.com/writetome51/typescript-base-class#baseclass)

## Installation

You must have npm installed first.  Then, in the command line:

```bash
npm install @writetome51/batchinator
```

## Loading

```
// If using TypeScript:
import { Batchinator } from '@writetome51/batchinator';
// If using ES5 JavaScript:
var Batchinator = require('@writetome51/batchinator').Batchinator;
```   


## License
[MIT](https://choosealicense.com/licenses/mit/)

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