45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
/**
|
|
* Shared utilities for channel adapters.
|
|
*/
|
|
|
|
/**
|
|
* Split a long message into chunks that respect a platform's character limit.
|
|
* Prefers splitting at newlines, then spaces, then hard-cuts.
|
|
*/
|
|
export function splitMessage(text: string, maxLength: number): string[] {
|
|
const chunks: string[] = [];
|
|
let remaining = text;
|
|
|
|
while (remaining.length > 0) {
|
|
if (remaining.length <= maxLength) {
|
|
chunks.push(remaining);
|
|
break;
|
|
}
|
|
|
|
// Try to split at a newline within the allowed window
|
|
let splitIndex = remaining.lastIndexOf('\n', maxLength);
|
|
if (splitIndex === -1 || splitIndex < maxLength / 2) {
|
|
splitIndex = remaining.lastIndexOf(' ', maxLength);
|
|
}
|
|
if (splitIndex === -1 || splitIndex < maxLength / 2) {
|
|
splitIndex = maxLength;
|
|
}
|
|
|
|
chunks.push(remaining.slice(0, splitIndex));
|
|
remaining = remaining.slice(splitIndex).trimStart();
|
|
}
|
|
|
|
return chunks;
|
|
}
|
|
|
|
/**
|
|
* Normalize reset command variants to a canonical text form.
|
|
* Returns '!reset' for recognized variants, otherwise returns the original text.
|
|
*/
|
|
export function normalizeResetCommandText(text: string): string {
|
|
if (text === '!reset' || text === 'reset') {
|
|
return '!reset';
|
|
}
|
|
return text;
|
|
}
|