Skip to content

Instantly share code, notes, and snippets.

@tmcnab
Created January 19, 2021 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tmcnab/8c19374eedbcf44dc8600080b604a529 to your computer and use it in GitHub Desktop.
Save tmcnab/8c19374eedbcf44dc8600080b604a529 to your computer and use it in GitHub Desktop.
function transverseChunk (array, size = 1) {
if (!(array instanceof Array)) throw new TypeError(`argument 'array' must be type array, received '${typeof array}'`)
if (typeof size !== 'number') throw new TypeError(`argument 'size' must be type number, received '${typeof size}'`)
if (size === 1 || array.length < 1) {
return [...array]
}
const chunks = new Array(n)
chunks.fill([])
let ptr = 0, idx = 0
while (true) {
chunks[ptr] = array[idx]
idx += 1
ptr += 1
if (ptr >= size) {
ptr = 0
}
if (idx >= array.length) {
break;
}
}
return chunks
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment