Split messages into chunks

This commit is contained in:
Mark Ceter
2023-05-21 10:41:18 +00:00
parent 3dcff11354
commit 5c7e14c287
2 changed files with 66 additions and 17 deletions

View File

@ -231,4 +231,35 @@ export function countOccurrences(string, character) {
export function isOdd(number) {
return number % 2 !== 0;
}
/** Split string to parts no more than length in size */
export function splitRecursive(input, length, delimitiers = ['\n\n', '\n', ' ', '']) {
const delim = delimitiers[0] ?? '';
const parts = input.split(delim);
const flatParts = parts.flatMap(p => {
if (p.length < length) return p;
return splitRecursive(input, length, delimitiers.slice(1));
});
// Merge short chunks
const result = [];
let currentChunk = '';
for (let i = 0; i < flatParts.length;) {
currentChunk = flatParts[i];
let j = i + 1;
while (j < flatParts.length) {
const nextChunk = flatParts[j];
if (currentChunk.length + nextChunk.length + delim.length <= length) {
currentChunk += delim + nextChunk;
} else {
break;
}
j++;
}
i = j;
result.push(currentChunk);
}
return result;
}