mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-14 23:21:08 +02:00
43 lines
932 B
TypeScript
43 lines
932 B
TypeScript
/**
|
|
* String utility functions
|
|
*/
|
|
|
|
/**
|
|
* Truncate a string to a maximum length with ellipsis
|
|
*/
|
|
export function truncate(str: string, maxLength: number): string {
|
|
if (str.length <= maxLength) return str;
|
|
return str.slice(0, maxLength - 3) + '...';
|
|
}
|
|
|
|
/**
|
|
* Capitalize the first letter of a string
|
|
*/
|
|
export function capitalize(str: string): string {
|
|
if (!str) return str;
|
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
}
|
|
|
|
/**
|
|
* Generate a random string ID
|
|
*/
|
|
export function generateId(length = 8): string {
|
|
const chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
|
let result = '';
|
|
for (let i = 0; i < length; i++) {
|
|
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Slugify a string for URLs
|
|
*/
|
|
export function slugify(str: string): string {
|
|
return str
|
|
.toLowerCase()
|
|
.trim()
|
|
.replace(/[^\w\s-]/g, '')
|
|
.replace(/[\s_-]+/g, '-')
|
|
.replace(/^-+|-+$/g, '');
|
|
}
|