-
Notifications
You must be signed in to change notification settings - Fork 36
/
chunkifier.js
43 lines (36 loc) · 1.16 KB
/
chunkifier.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
const chunkifyArray = (messageLines, maxLength, delimiter) => {
delimiter = delimiter || '\n'
let message = ''
const resultLines = []
messageLines.forEach(line => {
// the next chunk is made up of the (next) line and the delimiter
let nextChunk = line + delimiter
// if the message plus the next chunk puts us over the maxLength limit...
if ((message + nextChunk).length > maxLength) {
// ...we add the message to the result array...
resultLines.push(message.trimEnd())
// ...and "reset" the message with the next chunk.
message = nextChunk
} else {
// if not, we add the next chunk to the message
message += nextChunk
}
})
// handle the case where we have a trailing message
if (message.length > 0) {
resultLines.push(message.trimEnd())
}
return resultLines
}
const chunkifyString = (messageText, maxLength, delimiter) => {
if (messageText.length <= maxLength) {
return [messageText]
}
delimiter = delimiter || '\n'
const messageLines = messageText.split(delimiter)
return chunkifyArray(messageLines, maxLength, delimiter)
}
module.exports = {
chunkifyArray,
chunkifyString
}