Last active
September 19, 2018 17:50
-
-
Save szanata/4299564215c9867d0b3efca13a10af40 to your computer and use it in GitHub Desktop.
Batch splitter
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
const sizeLimit = 1000000; | |
const lengthLimit = 500; | |
const getSize = obj => Buffer.byteLength( JSON.stringify( obj ) ); | |
const isUnderLimits = batch => batch.length <= lengthLimit && getSize( batch ) <= sizeLimit; | |
module.exports = records => records.reduce( ( batches, record ) => { | |
if ( isUnderLimits( [].concat( batches[0], record ) ) ) { | |
batches[0].push( record ); | |
} else { | |
batches.unshift( [ record ] ); | |
} | |
return batches; | |
}, [] ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment