Make Util#splitMessage handle edge cases properly (#2212)

* Make Util#splitMessage handle edge cases properly

* Restart Travis

* Set maxLength to 2000 + small tweak
This commit is contained in:
Alex
2018-01-25 07:41:50 +02:00
committed by Isabella
parent 58d85282b4
commit e58ff642f5

View File

@@ -18,21 +18,20 @@ class Util {
* @param {SplitOptions} [options] Options controlling the behaviour of the split
* @returns {string|string[]}
*/
static splitMessage(text, { maxLength = 1950, char = '\n', prepend = '', append = '' } = {}) {
static splitMessage(text, { maxLength = 2000, char = '\n', prepend = '', append = '' } = {}) {
if (text.length <= maxLength) return text;
const splitText = text.split(char);
if (splitText.length === 1) throw new RangeError('SPLIT_MAX_LEN');
const messages = [''];
let msg = 0;
for (let i = 0; i < splitText.length; i++) {
if (messages[msg].length + splitText[i].length + 1 > maxLength) {
messages[msg] += append;
messages.push(prepend);
msg++;
const messages = [];
let msg = '';
for (const chunk of splitText) {
if (msg && (msg + char + chunk + append).length > maxLength) {
messages.push(msg + append);
msg = prepend;
}
messages[msg] += (messages[msg].length > 0 && messages[msg] !== prepend ? char : '') + splitText[i];
msg += (msg && msg !== prepend ? char : '') + chunk;
}
return messages.filter(m => m);
return messages.concat(msg).filter(m => m);
}
/**