Skip to main content

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​


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, otherwise false.

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