Discord.js: Let SplitOptions#char accept an array

Created on 9 Aug 2018  Â·  6Comments  Â·  Source: discordjs/discord.js

Is your feature request related to a problem? Please describe.
If you want to split a message, you have to pick one specific character to split on. I suggest enhancing the split function to allow you to specify more than one character and split based on all of them in descending order of priority

Describe the solution you'd like
Letting SplitOptions#char be an array as well as a string. If it's an array, we first try to split the message using the first element in that array, if we fail to construct a series of messages with only that split, we retry splitting the messages that are too long with the second element, etc. If we fail to split the message at all, we throw an error just as we do now.

Describe alternatives you've considered
Uhm, the alternative would be to not implement this? It's just a suggestion, and I'm mainly posting here to gather responses, if it's something we want I'll implement it myself and make a PR

Additional context
imagine this array being put in:
['\n', ' ', '']

That would result in the message being split on first new lines, and if we fail to split the message using only new lines, we try to split using spaces, and if even that fails, we split EVERYTHING, and forcibly split the message wherever is needed. This array would make it so the split never fails. That's useful if, say, we want to make sure that this message being sent no matter what.

I feel this would also make a good default value, but that's probably more debatable than the feature as a whole

low utility discussion enhancement

Most helpful comment

Why not a regex like String#split?

All 6 comments

Why not a regex like String#split?

Ah, yes, that wouldake even more sense!

On Fri, Aug 10, 2018, 10:49 1Computer1 notifications@github.com wrote:

Why not a regex like String#split?

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/discordjs/discord.js/issues/2721#issuecomment-412019614,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABVGdDBaDdl5LgbIGT6M_9SqxYdJdXyuks5uPUkAgaJpZM4V1lEw
.

Actually, hmm... I'm unsure if you can get the prioritizing of splitting I was talking about without a very complex regex, which would make the point of having an easy to use split function moot to begin with. I agree we should accept regex-splitting as well, but not to replace the priority array.

Simply specifying /\n| /g as a regex would not prioritize splitting on newlines, it would split on both newlines and spaces at once and remove this whole "use next index as fallback" philosophy behind the array

Why not both? Perhaps allow it to take string | regexp | Array<string | regexp>.

Yeah, I might have been unclear but that was my intention from the beginning when I said I agreed we should be supporting regex in combination with the priority array.

Uhm, doesn't String.split(); work with multiple arguments? Isn't that the behaviour you are looking for?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Dmitry221060 picture Dmitry221060  Â·  3Comments

Lombra picture Lombra  Â·  3Comments

kvn1351 picture kvn1351  Â·  3Comments

Blumlaut picture Blumlaut  Â·  3Comments

tiritto picture tiritto  Â·  3Comments