Elixir's Enum.chunk_every in TypeScript

How to use

Install via yarn add locutus and import: import { chunk_every } from 'locutus/elixir/Enum/chunk_every'.

Or with CommonJS: const { chunk_every } = require('locutus/elixir/Enum/chunk_every')

Use a bundler that supports tree-shaking so you only ship the functions you actually use. Vite, webpack, Rollup, and Parcel all handle this. For server-side use this is less of a concern.

Examples

These examples are extracted from test cases that automatically verify our functions against their native counterparts.

#codeexpected result
1chunk_every([1, 2, 3, 4, 5], 2)[[1, 2], [3, 4], [5]]
2chunk_every([1, 2, 3, 4, 5], 2, 2, [])[[1, 2], [3, 4], [5]]
3chunk_every([1, 2, 3, 4, 5], 3, 3, [0, 0])[[1, 2, 3], [4, 5, 0]]

Notes

  • Mirrors Elixir Enum.chunk_every/4 semantics with count, step and optional leftover padding.

Here's what our current TypeScript equivalent to Elixir's Enum.chunk_every looks like.

export function chunk_every<T>(values: T[] | unknown, count: number, step: number = count, leftover: T[] = []): T[][] {
// discuss at: https://locutus.io/elixir/chunk_every/
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Mirrors Elixir Enum.chunk_every/4 semantics with count, step and optional leftover padding.
// example 1: chunk_every([1, 2, 3, 4, 5], 2)
// returns 1: [[1, 2], [3, 4], [5]]
// example 2: chunk_every([1, 2, 3, 4, 5], 2, 2, [])
// returns 2: [[1, 2], [3, 4], [5]]
// example 3: chunk_every([1, 2, 3, 4, 5], 3, 3, [0, 0])
// returns 3: [[1, 2, 3], [4, 5, 0]]

if (!Array.isArray(values)) {
return []
}

const width = Math.trunc(Number(count))
const stride = Math.trunc(Number(step))
if (!Number.isFinite(width) || width <= 0) {
throw new RangeError('chunk_every(): count must be a positive integer')
}
if (!Number.isFinite(stride) || stride <= 0) {
throw new RangeError('chunk_every(): step must be a positive integer')
}

const pad = Array.isArray(leftover) ? leftover : []
const out: T[][] = []

for (let i = 0; i < values.length; i += stride) {
const chunk = values.slice(i, i + width)
if (chunk.length === width) {
out.push(chunk)
continue
}

if (chunk.length === 0) {
continue
}

const padded = [...chunk, ...pad].slice(0, width)
if (padded.length > 0) {
out.push(padded)
}
}

return out
}

Improve this function

Locutus is a community effort following The McDonald's Theory: we ship first iterations, hoping others will improve them. If you see something that could be better, we'd love your contribution.

View on GitHub · Edit on GitHub · View Raw


We have 11 Elixir functions so far - help us add more

Got a rainy Sunday afternoon and a taste for a porting puzzle?

  • Get inspiration from the Elixir documentation.
  • Click "New file" in the appropriate folder on GitHub. This will fork the project to your account, directly add the file to it, and send a Pull Request to us.

We will then review it. If it's useful to the project and in line with our contributing guidelines your work will become part of Locutus and you'll be automatically credited in the authors section accordingly.

« More Elixir Enum functions


Star