npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

compute-chunkify

v0.0.0

Published

Segments an array into chunks.

Downloads

6

Readme

Chunkify

NPM version Build Status Coverage Status Dependencies

Segments an array into chunks.

Installation

$ npm install compute-chunkify

For use in the browser, use browserify.

Usage

To use the module,

var chunkify = require( 'compute-chunkify' );

chunkify( arr, n[, opts] )

Segments an array into chunks of length n.

var data = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];

var out = chunkify( data, 3 );
/* returns
	[
		[1,2,3],
		[4,5,6],
		[7,8,9],
		[10,null,null]
	]
*/

The function is configurable and has the following options...

start

Integer specifying the index from which to begin chunking. Default: 0.

var opts = {
	'start': 2
};

var out = chunkify( data, 3, opts );
/* returns
	[
		[2,3,4],
		[5,6,7],
		[8,9,10]
	]
*/
truncate

Boolean specifying whether the last chunk must contain only array values and no padded values. Default: false.

var opts = {
	'truncate': true
};

var out = chunkify( data, 3, opts );
/* returns
	[
		[1,2,3],
		[4,5,6],
		[7,8,9]
	]
*/
padding

By default, if the array length is not evenly divisible by n, the last chunk is padded with null values. Default: true.

To turn off padding, set this option to false.

var opts = {
	'padding': false
};

var out = chunkify( data, 3, opts );
/* returns
	[
		[1,2,3],
		[4,5,6],
		[7,8,9],
		[10]
	]
*/
padding_value

The default padding value is null. Set this option to pad with a value other than null.

var opts = {
	'padding_value': 0
};

var out = chunkify( data, 3, opts );
/* returns
	[
		[1,2,3],
		[4,5,6],
		[7,8,9],
		[10,0,0]
	]
*/
delay

By default, an array is chunked beginning with the first array element. Default: 0.

Set this option to pad the first chunk, where 0 < delay < n.

var opts = {
	'delay': 2
};

var out = chunkify( data, 3, opts );
/* returns 
	[
		[null,null,1],
		[2,3,4],
		[5,6,7],
		[8,9,10]
	]
*/
overlap

By default, the array is segmented into adjacent chunks (no overlap and no underlap). Default: 0.

To create overlapping chunks, set the overlap option such that 0 < overlap < n,

var opts = {
	'overlap': 2
};

var out = chunkify( data, 3, opts );
/* returns 
	[
		[1,2,3],
		[2,3,4],
		[3,4,5],
		[4,5,6],
		[5,6,7],
		[6,7,8],
		[7,8,9],
		[8,9,10],
		[9,10,null]
	]
*/

To create non-adjacent chunks (underlap), set the overlap such that overlap < 0 , where |overlap| corresponds to the number of elements to skip between new chunks.

var opts = {
	'overlap': -4
};

var out = chunkify( data, 3, opts );
/* returns 
	[
		[1,2,3],
		[8,9,10]
	]
*/

Examples

var chunkify = require( 'compute-chunkify' );

To run the example code from the top-level application directory,

$ node ./examples/index.js

Tests

Unit

Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:

$ make test

All new feature development should have corresponding unit tests to validate correct functionality.

Test Coverage

This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:

$ make test-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,

$ make view-cov

License

MIT license.


Copyright

Copyright © 2014. Athan Reines.