split-to-chunks

1.0.3 • Public • Published

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]

Dependencies (0)

    Dev Dependencies (2)

    Package Sidebar

    Install

    npm i split-to-chunks

    Weekly Downloads

    1

    Version

    1.0.3

    License

    MIT

    Unpacked Size

    40.9 kB

    Total Files

    6

    Last publish

    Collaborators

    • timbeard