String
Helpers for manipulating and formatting strings. Includes casing conversions, masking, slugifying, truncating, reversing, counting, and more. All methods are fully typed.
API Summary
capitalize(str: string)- Capitalizes the first character of a string.toKebabCase(str: string)- Converts a string to kebab-case.toCamelCase(str: string)- Converts a string to camelCase.slugify(str: string)- Converts a string to a URL-friendly slug.truncate(str: string, len: number)- Truncates a string to a specific length, appending '...' if truncated.toPascalCase(str: string)- Converts a string to PascalCase.toSnakeCase(str: string)- Converts a string to snake_case.mask(str: string, visibleStart = 0, visibleEnd = 0, maskChar = '*')- Masks a string by replacing characters with a mask character.stripHtml(str: string)- Removes all HTML tags from a string.equalsIgnoreCase(a: string, b: string)- Performs case-insensitive string comparison.reverse(str: string)- Reverses a string.countOccurrences(str: string, substring: string, caseSensitive = true)- Counts occurrences of a substring within a string.toTitleCase(str: string)- Converts a string to Title Case.escapeRegex(str: string)- Escapes special regex characters in a string.unescapeHtml(str: string)- Unescapes HTML entities in a string.isBlank(str: string)- Checks if a string is blank (empty or only whitespace).ellipsis(str: string, maxLength: number, suffix = '...')- Truncates a string with an ellipsis, ensuring word boundaries.
Function Documentation & Usage Examples
capitalize()
Capitalizes the first character of a string.
Method Signature:
function capitalize(str: string): string;
Parameters:
str: The string to capitalize.
Returns:
- The capitalized string.
Example:
import { capitalize } from '@catbee/utils/string';
capitalize('hello world'); // "Hello world"
toKebabCase()
Converts a string to kebab-case.
Method Signature:
function toKebabCase(str: string): string;
Parameters:
str: The string to convert.
Returns:
- The kebab-case string.
Example:
import { toKebabCase } from '@catbee/utils/string';
toKebabCase('FooBar test'); // "foo-bar-test"
toCamelCase()
Converts a string to camelCase.
Method Signature:
function toCamelCase(str: string): string;
Parameters:
str: The string to convert.
Returns:
- The camelCase string.
Example:
import { toCamelCase } from '@catbee/utils/string';
toCamelCase('foo-bar_test'); // "fooBarTest"
slugify()
Converts a string to a URL-friendly slug.
Method Signature:
function slugify(str: string): string;
Parameters:
str: The string to convert.
Returns:
- The URL-friendly slug.
Example:
import { slugify } from '@catbee/utils/string';
slugify('Hello World!'); // "hello-world"
truncate()
Truncates a string to a specific length, appending '...' if truncated.
Method Signature:
function truncate(str: string, len: number): string;
Parameters:
str: The string to truncate.len: The maximum length of the truncated string.
Returns:
- The truncated string.
Example:
import { truncate } from '@catbee/utils/string';
truncate('This is a long sentence.', 10); // "This is a ..."
toPascalCase()
Converts a string to PascalCase.
Method Signature:
function toPascalCase(str: string): string;
Parameters:
str: The string to convert.
Returns:
- The PascalCase string.
Example:
import { toPascalCase } from '@catbee/utils/string';
toPascalCase('foo-bar'); // "FooBar"
toSnakeCase()
Converts a string to snake_case.
Method Signature:
function toSnakeCase(str: string): string;
Parameters:
str: The string to convert.
Returns:
- The snake_case string.
Example:
import { toSnakeCase } from '@catbee/utils/string';
toSnakeCase('FooBar test'); // "foo_bar_test"
mask()
Masks a string by replacing characters with a mask character.
Method Signature:
function mask(str: string, visibleStart?: number, visibleEnd?: number, maskChar?: string): string;
Parameters:
str: The string to mask.visibleStart: Number of characters to leave visible at the start (default: 0).visibleEnd: Number of characters to leave visible at the end (default: 0).maskChar: The character to use for masking (default: '*').
Returns:
- The masked string.
Example:
import { mask } from '@catbee/utils/string';
mask('hello world', 2, 5, '*'); // "he***world"
stripHtml()
Removes all HTML tags from a string.
Method Signature:
function stripHtml(str: string): string;
Parameters:
str: The string to process.
Returns:
- The string without HTML tags.
Example:
import { stripHtml } from '@catbee/utils/string';
stripHtml('<div>Hello <b>world</b></div>'); // "Hello world"
equalsIgnoreCase()
Performs case-insensitive string comparison.
Method Signature:
function equalsIgnoreCase(a: string, b: string): boolean;
Parameters:
a: The first string to compare.b: The second string to compare.
Returns:
trueif the strings are equal ignoring case, otherwisefalse.
Example:
import { equalsIgnoreCase } from '@catbee/utils/string';
equalsIgnoreCase('Hello', 'hello'); // true
reverse()
Reverses a string.
Method Signature:
function reverse(str: string): string;
Parameters:
str: The string to reverse.
Returns:
- The reversed string.
Example:
import { reverse } from '@catbee/utils/string';
reverse('abc'); // "cba"
countOccurrences()
Counts occurrences of a substring within a string.
Method Signature:
function countOccurrences(str: string, substring: string, caseSensitive: boolean = true): number;
Parameters:
str: The string to search within.substring: The substring to count.caseSensitive: Whether the search should be case-sensitive (default: true).
Returns:
- The number of occurrences of the substring.
Example:
import { countOccurrences } from '@catbee/utils/string';
countOccurrences('banana', 'an'); // 2 (case-sensitive)
countOccurrences('Banana', 'an'); // 1 (case-sensitive, only lowercase 'an')
countOccurrences('Banana', 'an', false); // 2 (case-insensitive)
toTitleCase()
Converts a string to Title Case (each word capitalized).
Method Signature:
function toTitleCase(str: string): string;
Parameters:
str: The string to convert.
Returns:
- The Title Case string.
Example:
import { toTitleCase } from '@catbee/utils/string';
toTitleCase('hello world'); // "Hello World"
toTitleCase('the quick brown fox'); // "The Quick Brown Fox"
escapeRegex()
Escapes special regex characters in a string.
Method Signature:
function escapeRegex(str: string): string;
Parameters:
str: The string to escape.
Returns:
- The string with regex characters escaped.
Example:
import { escapeRegex } from '@catbee/utils/string';
escapeRegex('Hello (world)'); // "Hello \\(world\\)"
escapeRegex('$100.00'); // "\\$100\\.00"
unescapeHtml()
Unescapes HTML entities in a string.
Method Signature:
function unescapeHtml(str: string): string;
Parameters:
str: The HTML string with entities.
Returns:
- The string with HTML entities unescaped.
Example:
import { unescapeHtml } from '@catbee/utils/string';
unescapeHtml('<div>Hello</div>'); // "<div>Hello</div>"
unescapeHtml('"Hello"'); // '"Hello"'
isBlank()
Checks if a string is blank (empty or only whitespace).
Method Signature:
function isBlank(str: string): boolean;
Parameters:
str: The string to check.
Returns:
trueif the string is blank, otherwisefalse.
Example:
import { isBlank } from '@catbee/utils/string';
isBlank(' '); // true
isBlank(''); // true
isBlank('hello'); // false
ellipsis()
Truncates a string with an ellipsis, ensuring word boundaries.
Method Signature:
function ellipsis(str: string, maxLength: number, suffix: string = '...'): string;
Parameters:
str: The string to truncate.maxLength: Maximum length of the result.suffix: Suffix to append when truncated (default: '...').
Returns:
- The truncated string with suffix.
Example:
import { ellipsis } from '@catbee/utils/string';
ellipsis('The quick brown fox', 10); // "The quick..."
ellipsis('Short', 10); // "Short"
ellipsis('The quick brown fox', 15, '…'); // "The quick…"