Skip to main content

Overview

The string utilities module provides functions for converting between different naming conventions and manipulating text.

Case conversion

toCamelCase

Converts a string to camelCase.
function toCamelCase(s: string, options?: ConvertCaseOptions): string
s
string
required
The string to convert
options
ConvertCaseOptions
Optional configuration for custom splitting behavior
result
string
The string converted to camelCase
Example:
import { toCamelCase } from "@temelj/string";

console.log(toCamelCase("hello world"));        // "helloWorld"
console.log(toCamelCase("user_first_name"));    // "userFirstName"
console.log(toCamelCase("API-Response-Data")); // "apiResponseData"

toSnakeCase

Converts a string to snake_case.
function toSnakeCase(s: string, options?: ConvertCaseOptions): string
s
string
required
The string to convert
options
ConvertCaseOptions
Optional configuration for custom splitting behavior
result
string
The string converted to snake_case
Example:
import { toSnakeCase } from "@temelj/string";

console.log(toSnakeCase("helloWorld"));        // "hello_world"
console.log(toSnakeCase("UserFirstName"));     // "user_first_name"
console.log(toSnakeCase("API Response Data")); // "api_response_data"

toPascalCase

Converts a string to PascalCase.
function toPascalCase(s: string, options?: ConvertCaseOptions): string
s
string
required
The string to convert
options
ConvertCaseOptions
Optional configuration for custom splitting behavior
result
string
The string converted to PascalCase
Example:
import { toPascalCase } from "@temelj/string";

console.log(toPascalCase("hello world"));      // "HelloWorld"
console.log(toPascalCase("user_first_name"));  // "UserFirstName"
console.log(toPascalCase("api-response-data"));// "ApiResponseData"

toKebabCase

Converts a string to kebab-case.
function toKebabCase(s: string, options?: ConvertCaseOptions): string
s
string
required
The string to convert
options
ConvertCaseOptions
Optional configuration for custom splitting behavior
result
string
The string converted to kebab-case
Example:
import { toKebabCase } from "@temelj/string";

console.log(toKebabCase("helloWorld"));        // "hello-world"
console.log(toKebabCase("UserFirstName"));     // "user-first-name"
console.log(toKebabCase("API Response Data")); // "api-response-data"

toTitleCase

Converts a string to Title Case.
function toTitleCase(s: string, options?: ConvertCaseOptions): string
s
string
required
The string to convert
options
ConvertCaseOptions
Optional configuration for custom splitting behavior
result
string
The string converted to Title Case with spaces
Example:
import { toTitleCase } from "@temelj/string";

console.log(toTitleCase("helloWorld"));        // "Hello World"
console.log(toTitleCase("user_first_name"));   // "User First Name"
console.log(toTitleCase("api-response-data")); // "Api Response Data"

Text manipulation

capitalize

Capitalizes the first letter of a string and lowercases the rest.
function capitalize(s: string): string
s
string
required
The string to capitalize
result
string
The capitalized string
Example:
import { capitalize } from "@temelj/string";

console.log(capitalize("hello"));   // "Hello"
console.log(capitalize("WORLD"));   // "World"
console.log(capitalize("aPi"));     // "Api"
console.log(capitalize(""));        // ""

caseSplit

Splits a string into parts based on case changes and delimiters.
function caseSplit(s: string, options?: ConvertCaseOptions): string[]
s
string
required
The string to split
options
ConvertCaseOptions
Optional configuration with custom split function
result
string[]
Array of lowercase word parts
Splitting rules:
  • Splits on spaces, dots, underscores, and hyphens
  • Splits on case changes (camelCase to parts)
  • Preserves numbers as separate parts
Example:
import { caseSplit } from "@temelj/string";

console.log(caseSplit("helloWorld"));       // ["hello", "world"]
console.log(caseSplit("user_first_name"));  // ["user", "first", "name"]
console.log(caseSplit("API-Response"));     // ["api", "response"]
console.log(caseSplit("version2Update"));   // ["version", "2", "update"]

Configuration

ConvertCaseOptions

Options for customizing case conversion behavior.
interface ConvertCaseOptions {
  split?: (s: string) => string[];
}
split
(s: string) => string[]
Custom function to split a string into parts before conversion
Example with custom splitter:
import { toCamelCase } from "@temelj/string";

// Split only on slashes
const result = toCamelCase("user/api/endpoint", {
  split: (s) => s.split("/")
});
console.log(result); // "userApiEndpoint"

Common use cases

API key transformation

import { toCamelCase, toSnakeCase } from "@temelj/string";

// Convert API response keys from snake_case to camelCase
function transformKeys(obj: Record<string, any>): Record<string, any> {
  return Object.fromEntries(
    Object.entries(obj).map(([key, value]) => [
      toCamelCase(key),
      value
    ])
  );
}

// Convert to database column names
function toDatabaseKeys(obj: Record<string, any>): Record<string, any> {
  return Object.fromEntries(
    Object.entries(obj).map(([key, value]) => [
      toSnakeCase(key),
      value
    ])
  );
}

CSS class name generation

import { toKebabCase } from "@temelj/string";

function generateClassName(componentName: string, modifier?: string): string {
  const base = toKebabCase(componentName);
  return modifier ? `${base}--${toKebabCase(modifier)}` : base;
}

console.log(generateClassName("UserProfile"));           // "user-profile"
console.log(generateClassName("UserProfile", "Active")); // "user-profile--active"

Type name generation

import { toPascalCase } from "@temelj/string";

function generateTypeName(tableName: string): string {
  return `${toPascalCase(tableName)}Model`;
}

console.log(generateTypeName("user_profiles")); // "UserProfilesModel"

Build docs developers (and LLMs) love