
Product
Introducing Custom Tabs for Org Alerts
Create and share saved alert views with custom tabs on the org alerts page, making it easier for teams to return to consistent, named filter sets.
An API to prevent long-running scripts from blocking.
The idea is to do work in synchronous chunks, periodically letting go of the thread.
Here it is in the browser, integrated with Angular.js.
We'll use our TypeScript declarations for documentation.
In API methods, an options literal can be passed:
declare interface IChunkifyOptions {
// The number of synchronous calls to make before yielding.
// Default value is 10; must be positive.
chunk: number;
// The number of milliseconds to wait until calling again.
// Default value is 50; must be non-negative.
delay: number;
// The context on which to invoke calls on.
// Default value is null; must not be a Number, Boolean, or undefined.
scope?: Object;
}
export var generator: (
start: number,
final: number,
options?: IChunkifyOptions
) => IterableIterator<number|IPause>;
declare interface IPause {
resume: (fn: () => void) => IPause;
}
Returns the core Generator used to power everything else.
It yields integers from start and final. Values are yielded synchronously within intervals of length chunk. At every chunkth call to .next(), the generator yields a promise-like IPause, with a single method .resume.
An error will be thrown if the generator is advanced before the IPause has .resume'd. The pause resumes after delay milliseconds, which is given in options or using its default value.
For chunking up functional work on arrays.
export var each: <T>(
tArray: T[],
tConsumer: (tItem: T, index: number) => void,
options?: IChunkifyOptions
) => Promise<void>
// This Promise resolves with the mapped array.
export var map: <T>(
tArray: T[],
tMapper: (tItem: T, index: number) => T,
options?: IChunkifyOptions
) => Promise<T[]>;
// Virtually identical to the native reduce on Array.prototype.
// This Promise resolves with the reduction result.
export var reduce: <T, U>(
tArray: T[],
tReducer: (current: U, tItem: T, index: number, tArray: T[]) => U,
options?: IChunkifyOptions,
memo?: U
) => Promise<U>;
To catch any Error thrown during processing, attach a .catch(...) to the returned Promise.
The error object in .catch(...) has the shape
{error: Error, item: T, index: number}
where error is the original error, index is the index where it happened, and item is tArray[index].
Processing stops when this happens.
For chunking up work done in loops.
export var interval: (
indexConsumer: (index: number) => void,
start: number,
final: number,
options?: IChunkifyOptions
) => Promise<void>;
// This is the same as chunkify.interval
// with the start parameter set to 0.
export var range: (
indexConsumer: (index: number) => void,
length: number,
options?: IChunkifyOptions
) => Promise<void>;
Just like catching errors in the Arrays API, except an item key isn't part of the error object.
Development is in TypeScript/ES6.
Get the source:
$ git clone git@github.com:username/chunkify
Install dependencies:
$ npm i && npm i babel-polyfill && node_modules/.bin/tsd install
Compile:
$ npm run-script compile
Develop:
$ npm run-script dev
MIT © 2016, Victor Alvarez
FAQs
Split an iterable into evenly sized chunks
The npm package chunkify receives a total of 7,246 weekly downloads. As such, chunkify popularity was classified as popular.
We found that chunkify demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Create and share saved alert views with custom tabs on the org alerts page, making it easier for teams to return to consistent, named filter sets.

Product
Socket’s Rust and Cargo support is now generally available, providing dependency analysis and supply chain visibility for Rust projects.

Security News
Chrome 144 introduces the Temporal API, a modern approach to date and time handling designed to fix long-standing issues with JavaScript’s Date object.