{"id":15704114,"url":"https://github.com/nam-hle/chunk-arr","last_synced_at":"2025-05-12T16:34:29.790Z","repository":{"id":48163417,"uuid":"354246744","full_name":"nam-hle/chunk-arr","owner":"nam-hle","description":"A collection of utility functions to split an array into chunks by size or certain condition.","archived":false,"fork":false,"pushed_at":"2021-05-04T00:45:54.000Z","size":321,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-02T18:54:43.863Z","etag":null,"topics":["array","array-manipulations","array-methods","chunk","chunks","split","typescript","utility"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nam-hle.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-04-03T09:09:07.000Z","updated_at":"2023-07-10T01:28:48.000Z","dependencies_parsed_at":"2022-09-26T18:01:32.683Z","dependency_job_id":null,"html_url":"https://github.com/nam-hle/chunk-arr","commit_stats":null,"previous_names":["nam288/chunk-arr"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nam-hle%2Fchunk-arr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nam-hle%2Fchunk-arr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nam-hle%2Fchunk-arr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nam-hle%2Fchunk-arr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nam-hle","download_url":"https://codeload.github.com/nam-hle/chunk-arr/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253777518,"owners_count":21962701,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["array","array-manipulations","array-methods","chunk","chunks","split","typescript","utility"],"created_at":"2024-10-03T20:10:23.323Z","updated_at":"2025-05-12T16:34:29.771Z","avatar_url":"https://github.com/nam-hle.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# chunk-arr\n[![Version](https://img.shields.io/npm/v/chunk-arr.svg)](https://www.npmjs.com/package/chunk-arr)\n[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo)\n[![codecov](https://codecov.io/gh/nam288/chunk-arr/branch/main/graph/badge.svg?token=fbqIQ8uk7t)](https://codecov.io/gh/nam288/chunk-arr)\n![build](https://github.com/nam288/chunk-arr/actions/workflows/main.yml/badge.svg)\n\n\u003e Split array into chunks by size or certain condition.\n\n## Why?\n\n* Minimal APIs\n* Written in TypeScript\n* Actively maintained\n* Fully coverage tests\n* Inspired from [Ruby chunk](https://ruby-doc.org/core-3.0.0/Enumerable.html#method-i-chunk)\n\n## Install\n\n```sh\n$ npm install chunk-arr\n```\n\n## Usage\n\n\n```js\nconst { chunkBy } = require('chunk-arr');\n\nchunkBy([3, 1, 4, 1, 5, 9, 2, 6], n =\u003e n % 2);\n// [ [3, 1], [4], [1, 5, 9], [2, 6] ]\n\n```\n\n## API\n\n### `chunk(array, [size = 1])`\n\nLike lodash/chunk, split the `array` into chunks of `size`. If array can not be split evenly, the final chunk will be the remaining elements.\n\n#### array\n\n* Require: `true`\n* Type: `any[]`\n\nThe array to process\n\n#### size\n\n* Require: `false`\n* Type: `number`\n* Default: `1`\n\nThe size of each chunk. Return empty array if `size` smaller than `1`.\n\n**Note:** The `size` will be converted to the largest number that smaller than `size` by `Math.floor`.\n\n**Example:**\n```js\nconst { chunk } = require('chunk-arr');\n// or\n// const chunk = require('chunk-arr').default;\n\nconst chars = ['a', 'b', 'c', 'd', 'e'];\n\nconsole.log(chunk(chars));\n// Output: [ ['a'], ['b'], ['c'], ['d'], ['e'] ]\n\nconsole.log(chunk(chars, 2.7));\n// Output: [ ['a', 'b'], ['c', 'd'], ['e'] ]\n```\n\n### `chunkBy(array, func)`\n\nIterate over `array` elements, chunking them together based on the returned value of `func`.\n\n#### array\n\n* Require: `true`\n* Type: `T[]`\n\nThe array to process\n\n#### func\n\n* Require: `true`\n* Type: `(element: T, index: number): U`\n\nThe function used to chunk `array` based on its return value. It takes two arguments: the current element is iterating and its index\n\n**Example:**\n* Split based on element types:\n\n```js\nconst { chunkBy } = require('chunk-arr');\n\nconst arr = [true, -3, 1, 'a', 'b', 'c'];\n\nconsole.log(chunkBy(arr, e =\u003e typeof e));\n// Output: [ [true], [-3, 1], ['a', 'b', 'c'] ]\n```\n\n* Split strings by their sizes:\n```js\nconst { chunkBy } = require('chunk-arr');\n\nconst arr = ['Lorem', 'Ipsum', 'is', 'simply', 'dummy', 'text'];\n\nconsole.log(chunkBy(arr, s =\u003e s.length));\n// Output: [ ['Lorem', 'Ipsum'], ['is'], ['simply', 'dummy'], ['text'] ]\n```\n\n### `chunkWhile(array, func)`\n\nThis method splits the `array` between adjacent elements if the `func` receives those elements returns `false`.\n\n#### array\n\n* Require: `true`\n* Type: `Array\u003cT\u003e`\n\nThe array to process\n\n#### func\n\n* Require: `true`\n* Type: `(previous: T, current: T): boolean`\n\nThe function, which receives the two adjacent elements alternatively, splits array between them if it returns `false`.\n\n**Example:**\n\n* Split array into non-decreasing chunks:\n\n```js\nconst { chunkWhile } = require('chunk-arr');\n\nconst nums = [0, 9, 2, 2, 3, 2, 7, 5, 9, 5];\n\nconsole.log(chunkWhile(nums, (prev, curr) =\u003e prev \u003c= curr));\n// Output: [[0, 9], [2, 2, 3], [2, 7], [5, 9], [5]]\n```\n\n* Convert increasing numbers into the compact string:\n\n```js\nconst { chunkWhile } = require('chunk-arr');\n\nconst nums = [1, 2, 4, 9, 10, 11, 12, 15, 16, 19, 20, 21];\n\nconsole.log(chunkWhile(nums, (prev, curr) =\u003e prev + 1 === curr)\n\t\t    .map((chunk) =\u003e chunk.length \u003e 2 ? `${chunk[0]}-${chunk[chunk.length - 1]}` : chunk)\n\t\t    .join(',')\n);\n// Output: '1,2,4,9-12,15,16,19-21'\n```\n\n* Split Markdown file into sections:\n\n```js\nconst Fs = require('fs');\nconst { chunkWhile } = require('chunk-arr');\n\ntry {\n\tconst data = Fs.readFileSync('./README.md', 'utf8');\n\tconsole.log(chunkWhile(data.split('\\n'), (_, current) =\u003e !current.startsWith('#')));\n} catch (error) {\n\tconsole.error(error);\n}\n\n// Output:\n// [\n//   ['# chunk-arr', ...],\n//   ['## Why?, ...],\n//   ...\n//   ['## Author', ...]\n// ]\n```\n\n## Author\n\n* **Nam Hoang Le**\n\nGive a ⭐️ if this package helped you!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnam-hle%2Fchunk-arr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnam-hle%2Fchunk-arr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnam-hle%2Fchunk-arr/lists"}