Skip to main content
A collection of string manipulation utilities focused on case conversions and string splitting operations.

Installation

npm install @temelj/string

Case conversion

Convert strings between different casing conventions with intelligent word boundary detection.

toCamelCase

Converts a string to camelCase.
function toCamelCase(s: string, options?: ConvertCaseOptions): string
import { toCamelCase } from "@temelj/string";

toCamelCase("Hello World");    // "helloWorld"
toCamelCase("user_name");      // "userName"
toCamelCase("get-user-id");    // "getUserId"
toCamelCase("XMLHttpRequest"); // "xmlHttpRequest"

toSnakeCase

Converts a string to snake_case.
function toSnakeCase(s: string, options?: ConvertCaseOptions): string
import { toSnakeCase } from "@temelj/string";

toSnakeCase("Hello World");  // "hello_world"
toSnakeCase("userName");     // "user_name"
toSnakeCase("getUserID");    // "get_user_id"
toSnakeCase("A b");          // "a_b"
toSnakeCase("Ab42");         // "ab42"

toPascalCase

Converts a string to PascalCase.
function toPascalCase(s: string, options?: ConvertCaseOptions): string
import { toPascalCase } from "@temelj/string";

toPascalCase("Hello World");   // "HelloWorld"
toPascalCase("user_name");     // "UserName"
toPascalCase("data-type-id");  // "DataTypeId"
toPascalCase("get_User");      // "GetUser"

toKebabCase

Converts a string to kebab-case.
function toKebabCase(s: string, options?: ConvertCaseOptions): string
import { toKebabCase } from "@temelj/string";

toKebabCase("Hello World");  // "hello-world"
toKebabCase("userName");     // "user-name"
toKebabCase("getUserID");    // "get-user-id"

toTitleCase

Converts a string to Title Case.
function toTitleCase(s: string, options?: ConvertCaseOptions): string
import { toTitleCase } from "@temelj/string";

toTitleCase("hello world");    // "Hello World"
toTitleCase("user_name");      // "User Name"
toTitleCase("data-type-id");   // "Data Type Id"

Utility functions

capitalize

Capitalizes the first letter of a string.
function capitalize(s: string): string
import { capitalize } from "@temelj/string";

capitalize("hello");    // "Hello"
capitalize("world");    // "World"
capitalize("UPPERCASE"); // "Uppercase"

caseSplit

Splits a string into parts based on case changes and delimiters.
function caseSplit(s: string, options?: ConvertCaseOptions): string[]
import { caseSplit } from "@temelj/string";

caseSplit("HelloWorld");        // ["hello", "world"]
caseSplit("user_name");         // ["user", "name"]
caseSplit("get-user-id");       // ["get", "user", "id"]
caseSplit("XMLHttpRequest");    // ["xml", "http", "request"]
The caseSplit function recognizes multiple delimiters including spaces, dots, underscores, and hyphens, as well as case changes.

Types

ConvertCaseOptions

Options for customizing case conversion behavior.
interface ConvertCaseOptions {
  /**
   * A function to split a string into parts.
   */
  split?: (s: string) => string[];
}

Conversion behavior

All case conversion functions handle the following automatically:
  • Spaces ( )
  • Dots (.)
  • Underscores (_)
  • Hyphens (-)
  • Case changes (e.g., camelCase or PascalCase)
Numbers are preserved as part of words:
toCamelCase("user123Name");  // "user123Name"
toSnakeCase("get42Items");   // "get42_items"
All functions return an empty string when given an empty string:
toCamelCase("");   // ""
toSnakeCase("");   // ""
toPascalCase("");  // ""

Examples

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

// Convert API response from snake_case to camelCase
function convertApiResponse(data: Record<string, any>) {
  const result: Record<string, any> = {};
  for (const [key, value] of Object.entries(data)) {
    result[toCamelCase(key)] = value;
  }
  return result;
}

const apiData = {
  user_name: "john",
  user_email: "[email protected]",
  created_at: "2024-01-01"
};

convertApiResponse(apiData);
// {
//   userName: "john",
//   userEmail: "[email protected]",
//   createdAt: "2024-01-01"
// }
Case conversions are lossy operations. Converting back and forth may not preserve the original formatting:
const original = "XMLHttpRequest";
const snake = toSnakeCase(original);    // "xml_http_request"
const camel = toCamelCase(snake);       // "xmlHttpRequest"
// original !== camel

Build docs developers (and LLMs) love