63 lines
1.6 KiB
TypeScript
63 lines
1.6 KiB
TypeScript
export function truncateText(input: string, length: number = 1000): string {
|
|
return input.length > length ? input.substring(0, length) + '...' : input;
|
|
}
|
|
|
|
export function formatNumberToShortForm(number: number, locale: string = 'en-uS') {
|
|
let suffix = '';
|
|
let value = number;
|
|
|
|
if (Math.abs(number) >= 1e9) {
|
|
value = number / 1e9;
|
|
suffix = 'b';
|
|
} else if (Math.abs(number) >= 1e6) {
|
|
value = number / 1e6;
|
|
suffix = 'M';
|
|
} else if (Math.abs(number) >= 1e3) {
|
|
value = number / 1e3;
|
|
suffix = 'k';
|
|
}
|
|
|
|
// Format the number to have up to 4 significant digits
|
|
const formattedValue = new Intl.NumberFormat(locale, {
|
|
maximumSignificantDigits: 4,
|
|
minimumSignificantDigits: 3,
|
|
minimumFractionDigits: 0,
|
|
maximumFractionDigits: 2,
|
|
}).format(value);
|
|
|
|
return `${formattedValue}${suffix}`;
|
|
}
|
|
|
|
export function normalize(value: string) {
|
|
return value
|
|
.toString()
|
|
.normalize('NFD')
|
|
.replace(/[\u0300-\u036f]/g, '')
|
|
.replace(/[^a-zA-Z\s\d]/g, '')
|
|
.trim();
|
|
}
|
|
|
|
export function toTitleCase(value: string) {
|
|
return value.replace(/\w\S*/g, (txt) => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase());
|
|
}
|
|
|
|
export function escapeMarkdown(text: string) {
|
|
return text.replace(/([\\_*~`|])/g, '\\$1');
|
|
}
|
|
|
|
export function escapeCodeBlock(text: string) {
|
|
return text.replace(/```/g, '`\u200b``');
|
|
}
|
|
|
|
export function escapeInlineCode(text: string) {
|
|
return text.replace(/`/g, '\u200b`');
|
|
}
|
|
|
|
export function escapeSpoiler(text: string) {
|
|
return text.replace(/\|\|/g, '|\u200b|');
|
|
}
|
|
|
|
export function escapeAll(text: string) {
|
|
return escapeMarkdown(escapeCodeBlock(escapeInlineCode(escapeSpoiler(text))));
|
|
}
|