Skip to content

Helper functions to create and apply chunks. It's a wrapper around lodash chunk that provides additional logging and a way to process the chunks.

License

Notifications You must be signed in to change notification settings

koaninc/koan-chunked

Repository files navigation

koan-chunked

Work in progress - please don't mind the momentary mess

Helper functions to create and apply chunks. It's a wrapper around lodash chunk that provides additional logging and a way to process the chunks.

Install

Using npm

npm i koan-chunked

Add to the top of the file

import * as chunked from "koan-chunked"

Usage

from => Chunk items in an array (can be any valid TypeScript type)

/*
* @param ts {array}
* @param chunkSize {number}
*/
const original: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const groupedByTwo = chunked.from(original, 2);

/* groupedByTwo = {
      "total": 10,
      "chunks": [
        [1, 2],
        [3, 4],
        [5, 6],
        [7, 8],
        [9, 10]
      ]
    }
*/

apply => Unwind chunked items

type Chunked<T> = { total: number; chunks: T[][] };

/*
* @param chunked {Chunked<T>}
* @param applyChunk {function that returns a Promise}
*/
  await chunked.apply(groupedByTwo, (chunk) =>
    Promise.all(chunk.map((item) => console.log(item)),
  ));

  // This will console.log each item in groupedByTwo

About

Helper functions to create and apply chunks. It's a wrapper around lodash chunk that provides additional logging and a way to process the chunks.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published