String Functions
A collection of utility functions for working with strings.
truncate
Truncates a string if it’s longer than the maximum length.
const longText = "This is a very long text that needs to be truncated";const truncated = truncate(longText, 20);console.log(truncated); // "This is a very long..."/** * Truncates a string if it's longer than the maximum length * @param str - The string to truncate * @param maxLength - Maximum length before truncation * @param ellipsis - String to use as ellipsis * @returns Truncated string */function truncate( str: string, maxLength: number, ellipsis: string = '...'): string { if (str.length <= maxLength) { return str; }
return str.slice(0, maxLength - ellipsis.length) + ellipsis;}slugify
Converts a string to a URL-friendly slug.
const title = "Hello World! This is a Test";const slug = slugify(title);console.log(slug); // "hello-world-this-is-a-test"/** * Converts a string to a URL-friendly slug * @param str - The string to convert * @returns URL-friendly slug */function slugify(str: string): string { return str .toLowerCase() .replace(/[^\w\s-]/g, '') // Remove non-word chars .replace(/[\s_-]+/g, '-') // Replace spaces and underscores with hyphens .replace(/^-+|-+$/g, ''); // Remove leading/trailing hyphens}capitalize
Capitalizes the first letter of a string.
const text = "hello world";const capitalized = capitalize(text);console.log(capitalized); // "Hello world"/** * Capitalizes the first letter of a string * @param str - The string to capitalize * @returns Capitalized string */function capitalize(str: string): string { if (!str.length) { return str; }
return str.charAt(0).toUpperCase() + str.slice(1);}