Comprehensive string manipulation utilities including case conversion, trimming, padding, escaping, and text processing functions. All functions are optimized for performance and handle Unicode characters correctly.
Functions for converting strings between different case formats commonly used in programming and text processing.
/**
* Converts string to camelCase
* @param str - String to convert
* @returns String in camelCase format
*/
function camelCase(str: string): string;
/**
* Converts string to snake_case
* @param str - String to convert
* @returns String in snake_case format
*/
function snakeCase(str: string): string;
/**
* Converts string to kebab-case
* @param str - String to convert
* @returns String in kebab-case format
*/
function kebabCase(str: string): string;
/**
* Converts string to PascalCase
* @param str - String to convert
* @returns String in PascalCase format
*/
function pascalCase(str: string): string;
/**
* Converts string to CONSTANT_CASE
* @param str - String to convert
* @returns String in CONSTANT_CASE format
*/
function constantCase(str: string): string;
/**
* Converts string to lower case with spaces
* @param str - String to convert
* @returns String in lower case format
*/
function lowerCase(str: string): string;
/**
* Converts string to UPPER CASE with spaces
* @param str - String to convert
* @returns String in UPPER CASE format
*/
function upperCase(str: string): string;
/**
* Converts string to Start Case (Title Case)
* @param str - String to convert
* @returns String in Start Case format
*/
function startCase(str: string): string;Usage Examples:
import {
camelCase,
snakeCase,
kebabCase,
pascalCase,
constantCase,
startCase
} from 'es-toolkit/string';
const text = 'hello world example';
console.log(camelCase(text)); // 'helloWorldExample'
console.log(snakeCase(text)); // 'hello_world_example'
console.log(kebabCase(text)); // 'hello-world-example'
console.log(pascalCase(text)); // 'HelloWorldExample'
console.log(constantCase(text)); // 'HELLO_WORLD_EXAMPLE'
console.log(startCase(text)); // 'Hello World Example'
// Works with various input formats
const mixedInput = 'XMLHttpRequest';
console.log(camelCase(mixedInput)); // 'xmlHttpRequest'
console.log(snakeCase(mixedInput)); // 'xml_http_request'
console.log(kebabCase(mixedInput)); // 'xml-http-request'
// Handles special characters and numbers
const complex = 'user-ID_123 name';
console.log(camelCase(complex)); // 'userId123Name'
console.log(pascalCase(complex)); // 'UserId123Name'Functions for modifying the case of specific characters within strings.
/**
* Capitalizes the first character of string
* @param str - String to capitalize
* @returns String with first character capitalized
*/
function capitalize(str: string): string;
/**
* Converts first character to lowercase
* @param str - String to modify
* @returns String with first character lowercased
*/
function lowerFirst(str: string): string;
/**
* Converts first character to uppercase
* @param str - String to modify
* @returns String with first character uppercased
*/
function upperFirst(str: string): string;Usage Examples:
import { capitalize, lowerFirst, upperFirst } from 'es-toolkit/string';
console.log(capitalize('hello world')); // 'Hello world'
console.log(lowerFirst('HELLO WORLD')); // 'hELLO WORLD'
console.log(upperFirst('hello world')); // 'Hello world'
// Works with empty strings and single characters
console.log(capitalize('')); // ''
console.log(capitalize('a')); // 'A'
console.log(lowerFirst('A')); // 'a'Functions for adding or removing characters from strings to achieve desired formatting.
/**
* Pads string to specified length with given characters
* @param str - String to pad
* @param length - Target length of result string
* @param chars - Characters to use for padding (default: ' ')
* @returns Padded string
*/
function pad(str: string, length: number, chars?: string): string;
/**
* Removes whitespace or specified characters from both ends
* @param str - String to trim
* @param chars - Characters to remove (default: whitespace)
* @returns Trimmed string
*/
function trim(str: string, chars?: string): string;
/**
* Removes whitespace or specified characters from start
* @param str - String to trim
* @param chars - Characters to remove (default: whitespace)
* @returns String with leading characters removed
*/
function trimStart(str: string, chars?: string): string;
/**
* Removes whitespace or specified characters from end
* @param str - String to trim
* @param chars - Characters to remove (default: whitespace)
* @returns String with trailing characters removed
*/
function trimEnd(str: string, chars?: string): string;Usage Examples:
import { pad, trim, trimStart, trimEnd } from 'es-toolkit/string';
// Padding examples
console.log(pad('hello', 10)); // ' hello ' (centered)
console.log(pad('hello', 10, '-')); // '--hello---'
console.log(pad('hello', 8, '0')); // '0hello00'
// Trimming whitespace
console.log(trim(' hello world ')); // 'hello world'
console.log(trimStart(' hello')); // 'hello'
console.log(trimEnd('hello ')); // 'hello'
// Trimming custom characters
console.log(trim('--hello--', '-')); // 'hello'
console.log(trimStart('...text', '.')); // 'text'
console.log(trimEnd('text!!!', '!')); // 'text'
// Multiple character trimming
console.log(trim('_-hello-_', '_-')); // 'hello'Functions for analyzing and manipulating text content including word extraction and string reversal.
/**
* Splits string into array of words
* @param str - String to split
* @returns Array of words
*/
function words(str: string): string[];
/**
* Reverses the characters in a string
* @param str - String to reverse
* @returns Reversed string
*/
function reverseString(str: string): string;
/**
* Removes diacritical marks from string
* @param str - String to deburr
* @returns String with diacritical marks removed
*/
function deburr(str: string): string;Usage Examples:
import { words, reverseString, deburr } from 'es-toolkit/string';
// Word extraction
console.log(words('hello world')); // ['hello', 'world']
console.log(words('camelCaseWord')); // ['camel', 'Case', 'Word']
console.log(words('snake_case_word')); // ['snake', 'case', 'word']
console.log(words('kebab-case-word')); // ['kebab', 'case', 'word']
console.log(words('words with spaces')); // ['words', 'with', 'spaces']
// String reversal
console.log(reverseString('hello')); // 'olleh'
console.log(reverseString('JavaScript')); // 'tpircSavaJ'
console.log(reverseString('')); // ''
// Diacritical mark removal
console.log(deburr('déjà vu')); // 'deja vu'
console.log(deburr('café')); // 'cafe'
console.log(deburr('naïve')); // 'naive'
console.log(deburr('résumé')); // 'resume'Functions for safely escaping and unescaping strings for various contexts including HTML and regular expressions.
/**
* Escapes HTML characters in string
* @param str - String to escape
* @returns String with HTML characters escaped
*/
function escape(str: string): string;
/**
* Unescapes HTML entities in string
* @param str - String to unescape
* @returns String with HTML entities converted back to characters
*/
function unescape(str: string): string;
/**
* Escapes RegExp special characters in string
* @param str - String to escape for use in RegExp
* @returns String with RegExp special characters escaped
*/
function escapeRegExp(str: string): string;Usage Examples:
import { escape, unescape, escapeRegExp } from 'es-toolkit/string';
// HTML escaping
const htmlContent = '<div>Hello & "goodbye"</div>';
const escaped = escape(htmlContent);
console.log(escaped); // '<div>Hello & "goodbye"</div>'
const unescaped = unescape(escaped);
console.log(unescaped); // '<div>Hello & "goodbye"</div>'
// RegExp escaping for safe pattern creation
const userInput = 'How much is $5.99?';
const escapedPattern = escapeRegExp(userInput);
console.log(escapedPattern); // 'How much is \\$5\\.99\\?'
// Use escaped string in RegExp
const regex = new RegExp(escapedPattern);
console.log("The price is $5.99".match(regex)); // Matches the literal string
// Common RegExp special characters that get escaped
console.log(escapeRegExp('.+*?^${}()|[]\\'));
// Output: '\\.\\+\\*\\?\\^\\$\\{\\}\\(\\)\\|\\[\\]\\\\'String functions can be chained together for complex transformations:
import { trim, capitalize, camelCase } from 'es-toolkit/string';
function processUserInput(input: string): string {
return capitalize(camelCase(trim(input)));
}
console.log(processUserInput(' hello_WORLD ')); // 'HelloWorld'Combine string functions for template and data processing:
import { snakeCase, upperCase, trim } from 'es-toolkit/string';
function generateConstantName(description: string): string {
return snakeCase(trim(description)).toUpperCase();
}
console.log(generateConstantName(' API Base URL ')); // 'API_BASE_URL'
function generateCSSClass(componentName: string): string {
return kebabCase(trim(componentName));
}
console.log(generateCSSClass('UserProfileCard')); // 'user-profile-card'Use string functions for consistent data formatting:
import { trim, lowerCase } from 'es-toolkit/string';
function normalizeEmail(email: string): string {
return trim(email).toLowerCase();
}
function normalizeSearchTerm(term: string): string {
return trim(lowerCase(term));
}
console.log(normalizeEmail(' User@Example.COM ')); // 'user@example.com'
console.log(normalizeSearchTerm(' HELLO World ')); // 'hello world'All string functions properly handle Unicode characters including:
import { capitalize, words, deburr } from 'es-toolkit/string';
// Emoji support
console.log(capitalize('🚀 rocket launch')); // '🚀 rocket launch'
console.log(words('hello 🌟 world')); // ['hello', '🌟', 'world']
// Diacritics and international characters
console.log(deburr('Björk')); // 'Bjork'
console.log(capitalize('måneskin')); // 'Måneskin'