import { notEmpty } from 'e'; // eslint-disable-next-line @typescript-eslint/no-var-requires const emojiRegex = require('emoji-regex'); const rawEmojiRegex = emojiRegex(); export function stripEmojis(str: string) { return str.replace(rawEmojiRegex, ''); } export function cleanString(str: string) { return str.replace(/[^0-9a-zA-Z+]/gi, '').toUpperCase(); } export function stringMatches(str: string | number = '', str2: string | number = '') { return cleanString(str.toString()) === cleanString(str2.toString()); } export function roboChimpCLRankQuery(userID: bigint) { return `SELECT COUNT(*)::int FROM public.user WHERE ((osb_cl_percent + bso_cl_percent) / 2) >= ( SELECT (((COALESCE(osb_cl_percent, 0)) + (COALESCE(bso_cl_percent, 0))) / 2) FROM public.user WHERE id = ${userID} );`; } const englishOrdinalRules = new Intl.PluralRules('en', { type: 'ordinal' }); const suffixes: { [key: string]: string } = { one: 'st', two: 'nd', few: 'rd', other: 'th' }; export function formatOrdinal(number: number): string { const suffix = suffixes[englishOrdinalRules.select(number)]; return `${number}${suffix}`; } export function toTitleCase(str: string) { const splitStr = str.toLowerCase().split(' '); for (let i = 0; i < splitStr.length; i++) { splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1); } return splitStr.join(' '); } const validChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; export function miniID(length: number): string { let id = ''; for (let i = 0; i < length; i++) { const randomChar = validChars[Math.floor(Math.random() * validChars.length)]; id += randomChar; } return id; } export function truncateString(str: string, maxLen: number) { if (str.length < maxLen) return str; return `${str.slice(0, maxLen - 3)}...`; } export function splitMessage(text: string, { maxLength = 2000, char = '\n', prepend = '', append = '' } = {}) { if (text.length <= maxLength) return [text]; let splitText: string[] = [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)).filter(notEmpty); } 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 + char + chunk + append).length > maxLength) { messages.push(msg + append); msg = prepend; } msg += (msg !== prepend ? char : '') + chunk; } return messages.concat(msg).filter(m => m); }