forked from shelfio/array-chunk-by-size
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
51 lines (43 loc) · 1.25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
require('fast-text-encoding');
/**
* Chunk array of objects by their size when stringified into JSON
* @param {Object[]} input Array of objects to chunk
* @param {Number} bytesSize Amount of bytes each chunk can have at max
* @return {Object[][]} Array of arrays - chunked array by size
*/
module.exports.chunkArray = function({input, bytesSize = Number.MAX_SAFE_INTEGER}) {
const output = [];
let outputSize = 0;
let outputFreeIndex = 0;
if (!input || input.length === 0 || bytesSize <= 0) {
return output;
}
for (let obj of input) {
const objSize = getObjectSize(obj);
const fitsIntoLastChunk = (outputSize + objSize) <= bytesSize;
if (fitsIntoLastChunk) {
if (!Array.isArray(output[outputFreeIndex])) {
output[outputFreeIndex] = [];
}
output[outputFreeIndex].push(obj);
outputSize += objSize;
} else {
if (output[outputFreeIndex]) {
outputFreeIndex++;
outputSize = 0;
}
output[outputFreeIndex] = [];
output[outputFreeIndex].push(obj);
outputSize += objSize;
}
}
return output;
};
function getObjectSize(obj) {
try {
const str = JSON.stringify(obj);
return new TextEncoder().encode(str).length;
} catch (error) {
return 0;
}
}