Skip to content

Commit

Permalink
refactor(Util): remove splitting
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiralite committed Apr 12, 2022
1 parent 01a423d commit 6af3513
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 53 deletions.
45 changes: 0 additions & 45 deletions packages/discord.js/src/util/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,51 +52,6 @@ class Util extends null {
return out;
}

/**
* Options for splitting a message.
* @typedef {Object} SplitOptions
* @property {number} [maxLength=2000] Maximum character length per message piece
* @property {string|string[]|RegExp|RegExp[]} [char='\n'] Character(s) or Regex(es) to split the message with,
* an array can be used to split multiple times
* @property {string} [prepend=''] Text to prepend to every piece except the first
* @property {string} [append=''] Text to append to every piece except the last
*/

/**
* Splits a string into multiple chunks at a designated character that do not exceed a specific length.
* @param {string} text Content to split
* @param {SplitOptions} [options] Options controlling the behavior of the split
* @returns {string[]}
*/
static splitMessage(text, { maxLength = 2_000, char = '\n', prepend = '', append = '' } = {}) {
text = Util.verifyString(text);
if (text.length <= maxLength) return [text];
let splitText = [text];
if (Array.isArray(char)) {
while (char.length > 0 && splitText.some(elem => elem.length > maxLength)) {
const currentChar = char.shift();
if (currentChar instanceof RegExp) {
splitText = splitText.flatMap(chunk => chunk.match(currentChar));
} else {
splitText = splitText.flatMap(chunk => chunk.split(currentChar));
}
}
} else {
splitText = text.split(char);
}
if (splitText.some(elem => elem.length > maxLength)) throw new RangeError('SPLIT_MAX_LEN');
const messages = [];
let msg = '';
for (const chunk of splitText) {
if (msg && (msg + char + chunk + append).length > maxLength) {
messages.push(msg + append);
msg = prepend;
}
msg += (msg && msg !== prepend ? char : '') + chunk;
}
return messages.concat(msg).filter(m => m);
}

/**
* Options used to escape markdown.
* @typedef {Object} EscapeMarkdownOptions
Expand Down
8 changes: 0 additions & 8 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2548,7 +2548,6 @@ export class Util extends null {
route: string,
reason?: string,
): Promise<{ id: Snowflake; position: number }[]>;
public static splitMessage(text: string, options?: SplitOptions): string[];
}

export class Components extends null {
Expand Down Expand Up @@ -5077,13 +5076,6 @@ export interface ShardingManagerOptions {

export { Snowflake };

export interface SplitOptions {
maxLength?: number;
char?: string | string[] | RegExp | RegExp[];
prepend?: string;
append?: string;
}

export type StageInstanceResolvable = StageInstance | Snowflake;

export interface StartThreadOptions {
Expand Down

0 comments on commit 6af3513

Please sign in to comment.