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

chunk-arr

v2.1.2

Published

A collection of utility functions to split an array into chunks by size or certain condition

Downloads

1,310

Readme

chunk-arr

Version XO code style codecov build

A collection of utility functions to split an array into chunks by size or certain condition.

Why?

  • Minimal APIs
  • Written in TypeScript
  • Actively maintained
  • Fully coverage tests
  • Inspired from Ruby chunk

Install

$ npm install chunk-arr

Usage

const { chunkBy } = require('chunk-arr');

chunkBy([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5], n => n % 2);
// [ [3, 1], [4], [1, 5, 9], [2, 6], [5, 3, 5] ]

API

chunk(array, [size = 1])

Like lodash/chunk, split the array into chunks of size. If array can not be split evenly, the final chunk will be the remaining elements.

array

  • Require: true
  • Type: any[]

The array to process

size

  • Require: false
  • Type: number
  • Default: 1

The size of each chunk. Return empty array if size smaller than 1.

Note: The size will be converted to the largest number that smaller than size by Math.floor.

Example:

const { chunk } = require('chunk-arr');
// or
// const chunk = require('chunk-arr');

const chars = ['a', 'b', 'c', 'd', 'e'];

console.log(chunk(chars));
// Output: [ ['a'], ['b'], ['c'], ['d'], ['e'] ]

console.log(chunk(chars, 2.4));
// Output: [ ['a', 'b'], ['c', 'd'], ['e'] ]

chunkBy(array, func)

Iterate over array elements, chunking them together based on the return value of the block.

Consecutive elements which return the same block value are chunked together.

array

  • Require: true
  • Type: T[]

The array to process

func

  • Require: true
  • Type: (element: T, index: number): U

The function used to chunk array based on its return value. It takes two arguments: the current element is iterating and its index

Example: Split based on element types

const { chunkBy } = require('chunk-arr');

const arr = [true, -3, 1, 'a', 'b', 'c'];

console.log(chunkBy(arr, e => typeof e));
// Output: [ [true], [-3, 1], ['a', 'b', 'c'] ]

chunkWhile(array, func)

This method splits the array between previous and current element if the function receives those elements return false.

array

  • Require: true
  • Type: Array<T>

The array to process

func

  • Require: true
  • Type: (previous: T, current: T): boolean

The function, which receives the previous and current element, uses to split array between them if it returns false

Example:

  • Split array into non-decreasing chunks
const { chunkWhile } = require('chunk-arr');

const nums = [0, 9, 2, 2, 3, 2, 7, 5, 9, 5];

console.log(chunkWhile(nums, (prev, curr) => prev <= curr));
// Output: [[0, 9], [2, 2, 3], [2, 7], [5, 9], [5]]
  • Convert increasing numbers into the compact string
const { chunkWhile } = require('chunk-arr');

const nums = [1, 2, 4, 9, 10, 11, 12, 15, 16, 19, 20, 21];

console.log(chunkWhile(nums, (prev, curr) => prev + 1 === curr)
		    .map((chunk) => chunk.length > 2 ? `${chunk[0]}-${chunk[chunk.length - 1]}` : chunk)
		    .join(',')
);
// Output: '1,2,4,9-12,15,16,19-21'
  • Split Markdown file by sections
const Fs = require('fs');
const { chunkWhile } = require('chunk-arr');

try {
	const data = Fs.readFileSync('./README.md', 'utf8');
	console.log(chunkWhile(data.split('\n'), (_, current) => !current.startsWith('#')));
} catch (error) {
	console.error(error);
}

// Output:
// [
//   ['# chunk-arr', ...],
//   ['## Why?, ...],
//   ...
//   ['## Author', ...]
// ]

Author

  • Nam Hoang Le

Give a ⭐️ if this package helped you!