Skip to content

Commit

Permalink
feat/fix(Utils): splitMessage regex + optional length(#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsian03 authored Jun 25, 2020
1 parent 05cbb05 commit b611ca9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
11 changes: 7 additions & 4 deletions src/Utility/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const ROLE_MENTION = /<@&([0-9]+)>$/;
const CHANNEL_MENTION = /<#([0-9]+)>$/;
const ID = /^[0-9]+$/;
const HEX_CODE = /^#?([0-9A-Fa-f]{6})$/;
const STRING_SPLIT = /[\s\S]{1,1950}([\n\r]|$)/g;

/**
* General Utility Class for AxonCore
Expand Down Expand Up @@ -49,6 +50,7 @@ class Utils {
this.roleMention = ROLE_MENTION;
this.channelMention = CHANNEL_MENTION;
this.id = ID;
this.stringSplit = STRING_SPLIT;

this.hexCode = HEX_CODE;
}
Expand Down Expand Up @@ -150,14 +152,15 @@ class Utils {

/**
* Split the given content (String), according to correct linebreaks.
* Split at 1900 characters.
* Split at default 1900 characters.
*
* @param {String} content
* @returns {Array<String>|String} The array of content string splitted or the original String
* @param {Number} [length]
* @returns {Array<String>} The array of content string splitted
* @memberof Utils
*/
splitMessage(content) {
return content.match(/[\s\S]{1,1900}[\n\r]/g) || [];
splitMessage(content, length) {
return content.match(typeof length === 'number' && length > 0 ? new RegExp(`[\\s\\S]{1,${length}}([\\n\\r]|$)`, 'g') : this.stringSplit);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions types/Utility/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ export declare class Utils {
readonly library: LibraryInterface;
/**
* Split the given content (String), according to correct linebreaks.
* Split at 1900 characters.
* Split at default 1900 characters.
*
* @returns The array of content string splitted or the original String
* @returns The array of content string splitted
* @memberof Utils
*/
public splitMessage(content: string): string[] | string;
public splitMessage(content: string, length?: number): string[];
/**
* Returns the guild prefix of the given msg.
*
Expand Down

0 comments on commit b611ca9

Please sign in to comment.