array-batcher
A Node.JS module that provides utility functions to execute synchronous or asynchronous operations on or mapping conversions of JavaScript Arrays in predetermined batches. Batches can be of a fixed size, created from a desired number of batches, or constructed by iterating through the array and testing a predicate.
Installation
npm install array-batcher --save
Usage
Javascript
; // In this case, an odd number of elements yields a best approximation of the even-numbered batch size const batched = "this" "is" "not" "a" "test";console;
Output should be:
TypeScript
; // helper function to track elapsed time;// helper function to simulate waiting for the given duration, i.e. in a request, etc.; /** * This example demonstrates one potential use case for the asynchronous batching * capabilities of the module, while showing the differences between patient * and strict interval batching. * * While the example uses a hard coded array of animal objects below, imagine that the input array * has been dynamically generated, and thus has an unknown number of unknown animals at the time * of execution. * * The goal is to upload these images to a server via a series of HTTP POST requests to a REST API. * In this example, let's say you've looked at the API's documentation and found to our dismay that the server can * process only up to a certain number of bytes in any one request. How can you, in one function call, * dynamically divide this unknown list of images into appropriately sized groups that can be sent to the API? * * Here's where the dynamic, asynchronous batching comes in. Continue reading inline below... */ ExecuteUploadSimulations
Where ~ denotes approximately, output should be: PATIENT Patient test with a 1 megabyte threshold!Dispatching upload
Test
npm run test