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 * @param {SplitOptions} [options] Options controlling the behaviour of the split
* @returns {string|string[]} * @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; if (text.length <= maxLength) return text;
const splitText = text.split(char); const splitText = text.split(char);
if (splitText.length === 1) throw new RangeError('SPLIT_MAX_LEN'); if (splitText.length === 1) throw new RangeError('SPLIT_MAX_LEN');
const messages = ['']; const messages = [];
let msg = 0; let msg = '';
for (let i = 0; i < splitText.length; i++) { for (const chunk of splitText) {
if (messages[msg].length + splitText[i].length + 1 > maxLength) { if (msg && (msg + char + chunk + append).length > maxLength) {
messages[msg] += append; messages.push(msg + append);
messages.push(prepend); msg = prepend;
msg++;
} }
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);
} }
/** /**