https://github.com/macarie/trancio
Lazily split an array into chunks, just like slices of pizza π
https://github.com/macarie/trancio
array array-helper chunk chunker iterable iterator javascript npm-module typescript
Last synced: 2 months ago
JSON representation
Lazily split an array into chunks, just like slices of pizza π
- Host: GitHub
- URL: https://github.com/macarie/trancio
- Owner: macarie
- License: mit
- Created: 2020-07-06T18:31:49.000Z (over 5 years ago)
- Default Branch: next
- Last Pushed: 2023-01-06T11:49:31.000Z (about 3 years ago)
- Last Synced: 2025-10-02T05:36:12.566Z (6 months ago)
- Topics: array, array-helper, chunk, chunker, iterable, iterator, javascript, npm-module, typescript
- Language: TypeScript
- Homepage:
- Size: 1.16 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 13
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# π [trancio](https://github.com/macarie/trancio) [](https://www.npmjs.com/package/trancio)
> Trancio lazily splits an array into chunks, providing both a functional and an iterable interface
  [](https://github.com/macarie/trancio/blob/next/license)
## Install
```console
$ npm install trancio
```
Or if you prefer using Yarn:
```console
$ yarn add trancio
```
## Usage
```javascript
import { trancio } from "trancio"
const array = [1, 2, 3, 4, 5, 6, 7, 8]
const tranci = [...trancio(array, 3)]
// => Array [[1, 2, 3], [4, 5, 6], [7, 8]]
const trancia = trancio(array, 3)
trancia()
// => [1, 2, 3]
trancia()
// => [4, 5, 6]
trancia()
// => [7, 8]
```
If you're using TypeScript, you can also directly import the `.ts` file, just like this:
```typescript
import { trancio } from "trancio/ts"
```
By doing that your bundler should, hopefully, be able to compile it by following your project's `tsconfig.json` file.
## API
### trancio(input, size)
Create a _slicer_ that also implements the `IterableIterator` interface, that way you can use the spread operator, the `for...of` statement, and call `next()` over it.
Calling the _slicer_ will return a chunk of the array with `size` elements. If `input` can't be split evenly, the final chunk will contain the remaining elements.
#### input
Type: `unknown[]`
#### size
Type: `number`
The length of each chunk.
## More Examples
Using `next()`:
```typescript
import { trancio } from "trancio"
const array = [1, 2, 3, 4, 5, 6, 7, 8]
const trancia = trancio(array, 3)
const fetta = trancia.next()
// fetta => { value: [1, 2, 3], done: false }
trancia.next()
// fetta => { value: [4, 5, 6], done: false }
trancia.next()
// fetta => { value: [7, 8], done: false }
trancia.next()
// fetta => { value: undefined, done: true }
```
Using `for...of`:
```typescript
import { trancio } from "trancio"
const array = [1, 2, 3, 4, 5, 6, 7, 8]
for (const fetta of trancio(array, 3)) {
console.log(fetta)
// 1st time => [1, 2, 3]
// 2nd time => [4, 5, 6]
// 3rd time => [7, 8]
}
```
## FAQ
### What does _trancio_ mean?
Pronounced [`/ΛtrantΚo/`](https://github.com/macarie/trancio/blob/master/media/pronunciation.m4a?raw=true), trancio is an Italian word that means "slice" or "piece". Usually, the term is used for food, e.g. _"un trancio di pizza"_, which means _"a slice of pizza"_, hence the pizza emoji at the top.