The StringUtils class provides a collection of utility methods for working with strings, including casing conversions, truncation, palindrome checks, and placeholder replacement.
import { StringUtils } from '@brmorillo/utils';
// Capitalize the first letter
const capitalized = StringUtils.capitalizeFirstLetter({ input: 'hello' });
console.log(capitalized); // "Hello"
// Convert to kebab-case
const kebab = StringUtils.toKebabCase({ input: 'Hello World' });
console.log(kebab); // "hello-world"Capitalizes the first letter of a string. Only the first character is upper-cased; the rest of the string is left untouched. Throws a ValidationError if input is not a string.
StringUtils.capitalizeFirstLetter({ input: 'hello' }); // "Hello"
StringUtils.capitalizeFirstLetter({ input: 'iPhone' }); // "IPhone"Reverses a string.
StringUtils.reverse({ input: 'hello' }); // "olleh"Checks if a string is a palindrome (ignoring non-alphanumeric characters and case).
StringUtils.isPalindrome({ input: 'racecar' }); // true
StringUtils.isPalindrome({ input: 'hello' }); // falseTruncates a string to a maximum length, adding an ellipsis if necessary.
StringUtils.truncate({ input: 'This is a long string', maxLength: 10 }); // "This is..."Converts a string to kebab-case.
StringUtils.toKebabCase({ input: 'Hello World' }); // "hello-world"
StringUtils.toKebabCase({ input: 'camelCaseString' }); // "camel-case-string"Converts a string to snake_case.
StringUtils.toSnakeCase({ input: 'Hello World' }); // "hello_world"
StringUtils.toSnakeCase({ input: 'camelCaseString' }); // "camel_case_string"Converts a string to camelCase.
StringUtils.toCamelCase({ input: 'Hello World' }); // "helloWorld"
StringUtils.toCamelCase({ input: 'snake_case_string' }); // "snakeCaseString"Converts a string to Title Case.
StringUtils.toTitleCase({ input: 'hello world' }); // "Hello World"Counts the occurrences of a substring in a string.
StringUtils.countOccurrences({ input: 'hello world hello', substring: 'hello' }); // 2
StringUtils.countOccurrences({ input: 'abc abc abc', substring: 'abc' }); // 3Replaces all occurrences of a substring in a string.
StringUtils.replaceAll({
input: 'hello world hello',
substring: 'hello',
replacement: 'hi'
}); // "hi world hi"Replaces the first occurrences occurrences of a substring in a string.
StringUtils.replaceOccurrences({
input: 'hello world hello',
substring: 'hello',
replacement: 'hi',
occurrences: 1
}); // "hi world hello"Replaces {key} placeholders in a template string with values from the replacements map. Unknown placeholders are left untouched.
StringUtils.replacePlaceholders({
template: 'Hello, {name}! You have {count} new messages.',
replacements: { name: 'John', count: '5' }
}); // "Hello, John! You have 5 new messages."import { StringUtils } from '@brmorillo/utils';
const title = 'the quick brown fox';
console.log(StringUtils.toTitleCase({ input: title })); // "The Quick Brown Fox"
console.log(StringUtils.toKebabCase({ input: title })); // "the-quick-brown-fox"
console.log(StringUtils.toCamelCase({ input: title })); // "theQuickBrownFox"
console.log(StringUtils.truncate({ input: title, maxLength: 12 })); // "the quick..."