split-to-chunks
v1.0.3
Published
Splits an array or a string to an array of chunks of the given size
Downloads
5
Readme
split-to-chunks
Splits an array or a string to an array of chunks of the given size
basic usage
const splitToChunks = require('split-to-chunks');
const output = splitToChunks('AABBCC', 2);
console.log(output);
// >> [['A', 'A'], ['B', 'B'], ['C', 'C']]
Example: reverse the pixels from a 3*1 bitmap
// Basic hex data for a 3*1px 24bit image going red, then green and blue
const img = [0xFF, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF]
// Reverse the image so it goes blue then green and red
const reversed = splitToChunks(img, 3).reverse()
const output = reversed.reduce((a, c) => {
c.forEach(piece => a.push(piece))
return a
}, [])
console.log(output)
// >> [0, 0, 255, 0, 255, 0, 255, 0, 0]