Created
January 19, 2021 17:04
-
-
Save tmcnab/8c19374eedbcf44dc8600080b604a529 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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