utilyx
Version:
Modern utility helper library for cleaner, faster TypeScript/JavaScript development 🚀🔧
112 lines (111 loc) • 4.45 kB
TypeScript
/**
* Converts a string to title case (capitalizes first letter of each word)
* @param str - Input string
* @param lowercaseRest - Whether to lowercase the rest of each word (default: true)
* @returns String in title case
* @example
* toTitleCase("hello world") → "Hello World"
* toTitleCase("hElLo wOrLd", false) → "HElLo WOrLd"
*/
export declare function toTitleCase(str: string, lowercaseRest?: boolean): string;
/**
* Converts camelCase to snake_case
* @param str - Input string in camelCase
* @param uppercase - Whether to convert to UPPER_SNAKE_CASE (default: false)
* @returns String in snake_case
* @example
* camelToSnake("myVarName") → "my_var_name"
* camelToSnake("myVarName", true) → "MY_VAR_NAME"
*/
export declare function camelToSnake(str: string, uppercase?: boolean): string;
/**
* Converts snake_case to camelCase
* @param str - Input string in snake_case
* @param pascal - Whether to convert to PascalCase (default: false)
* @returns String in camelCase or PascalCase
* @example
* snakeToCamel("my_var_name") → "myVarName"
* snakeToCamel("my_var_name", true) → "MyVarName"
*/
export declare function snakeToCamel(str: string, pascal?: boolean): string;
/**
* Converts a string to a URL-friendly slug
* @param str - Input string
* @param separator - Character to use as separator (default: '-')
* @param preserveCase - Whether to preserve original case (default: false)
* @returns URL-friendly slug
* @example
* slugify("Hello World!") → "hello-world"
* slugify("Hello World!", '_', true) → "Hello_World"
*/
export declare function slugify(str: string, separator?: string, preserveCase?: boolean): string;
/**
* Truncates text with ellipsis
* @param str - Input string
* @param maxLength - Maximum length before truncation
* @param ellipsis - Custom ellipsis string (default: '...')
* @param preserveWords - Whether to preserve whole words (default: true)
* @returns Truncated string
* @example
* truncateText("Hello world", 8) → "Hello..."
* truncateText("Hello world", 8, '..', false) → "Hello wo.."
*/
export declare function truncateText(str: string, maxLength: number, ellipsis?: string, preserveWords?: boolean): string;
/**
* Reverses a string.
* @param str - Input string.
* @returns Reversed string.
*/
export declare function reverseString(str: string): string;
/**
* Counts words in a string (split by spaces).
* @param str - Input string.
* @returns Number of words.
*/
export declare function countWords(str: string): number;
/**
* Checks if a string is a palindrome
* @param str - Input string
* @param caseSensitive - Whether comparison should be case-sensitive (default: false)
* @param ignoreSpaces - Whether to ignore spaces (default: true)
* @returns true if the string is a palindrome
* @example
* isPalindrome("Madam") → true
* isPalindrome("A man a plan a canal Panama") → true
* isPalindrome("Racecar", true) → false
*/
export declare function isPalindrome(str: string, caseSensitive?: boolean, ignoreSpaces?: boolean): boolean;
export declare function isEmail(str: string): boolean;
export declare function isURL(str: string): boolean;
/**
* Trims extra spaces between words (e.g., "Hello world" → "Hello world").
* @param str - Input string.
* @returns String with normalized spaces.
*/
export declare function trimExtraSpaces(str: string): string;
/**
* Masks the local part of an email (e.g., "user@domain.com" → "us**@domain.com").
* @param email - Email address.
* @returns Masked email.
* @throws Error if input is not a valid email.
*/
export declare function maskEmail(email: string): string;
/**
* Generates a random string
* @param length - Length of the string (default: 10)
* @param options - Configuration options
* @param options.includeNumbers - Whether to include numbers (default: true)
* @param options.includeUppercase - Whether to include uppercase letters (default: true)
* @param options.includeLowercase - Whether to include lowercase letters (default: true)
* @param options.customChars - Custom character set to use
* @returns Random string
* @example
* generateRandomString(8) → "A3b7GhK9"
* generateRandomString(6, { includeNumbers: false }) → "aBcDef"
*/
export declare function generateRandomString(length?: number, options?: {
includeNumbers?: boolean;
includeUppercase?: boolean;
includeLowercase?: boolean;
customChars?: string;
}): string;