String Utilities
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?: number, visibleEnd?: number, maskChar?: string)
- 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?: boolean)
- Counts occurrences of a substring within a string.
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';
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';
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';
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';
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';
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';
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';
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 is 0).visibleEnd
: Number of characters to leave visible at the end (default is 0).maskChar
: The character to use for masking (default is '*').
Returns:
- The masked string.
Example:
import { mask } from '@catbee/utils';
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';
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:
true
if the strings are equal ignoring case, otherwisefalse
.
Example:
import { equalsIgnoreCase } from '@catbee/utils';
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';
reverse("abc"); // "cba"
countOccurrences()
​
Counts occurrences of a substring within a string.
Method Signature:
function countOccurrences(str: string, substring: string, caseSensitive?: boolean): number;
Parameters:
str
: The string to search within.substring
: The substring to count.caseSensitive
: Whether the search should be case-sensitive (default is false).
Returns:
- The number of occurrences of the substring.
Example:
import { countOccurrences } from '@catbee/utils';
countOccurrences("banana", "an"); // 2
countOccurrences("Banana", "an", false); // 2