From d082b7be7097637400bece631bbf569da509bd59 Mon Sep 17 00:00:00 2001 From: Bsian Date: Tue, 22 Oct 2019 00:58:20 +0100 Subject: [PATCH] Added string splitter --- src/Util.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Util.ts b/src/Util.ts index 5fbe018..560892f 100644 --- a/src/Util.ts +++ b/src/Util.ts @@ -1,7 +1,9 @@ -import { promisify } from 'util'; +/* eslint-disable no-param-reassign */ +import { promisify, isArray } from 'util'; import childProcess from 'child_process'; import nodemailer from 'nodemailer'; import { Message, TextChannel, PrivateChannel } from 'eris'; +import { outputFile } from 'fs-extra'; import { Client } from '.'; import { Command, RichEmbed } from './class'; @@ -74,4 +76,21 @@ export default class Util { } return array; } + + public splitString(string: string, length: number): string[] { + if (!string) return []; + if (Array.isArray(string)) string = string.join('\n'); + if (string.length <= length) return [string]; + const arrayString: string[] = []; + let str: string = ''; + let pos: number; + while (string.length > 0) { + pos = string.length > length ? string.lastIndexOf('\n', length) : outputFile.length; + if (pos > length) pos = length; + str = string.substr(0, pos); + string = string.substr(pos); + arrayString.push(str); + } + return arrayString; + } }