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
API field conversion
CSS class generation
Database naming
Environment variables
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"
// }
import { toKebabCase } from "@temelj/string";
function generateClassName(componentName: string, modifier?: string) {
const base = toKebabCase(componentName);
if (modifier) {
return `${base}--${toKebabCase(modifier)}`;
}
return base;
}
generateClassName("UserProfile");
// "user-profile"
generateClassName("UserProfile", "IsActive");
// "user-profile--is-active"
import { toSnakeCase, toPascalCase } from "@temelj/string";
// Convert TypeScript class names to database table names
function toTableName(className: string): string {
return toSnakeCase(className);
}
toTableName("UserProfile"); // "user_profile"
toTableName("BlogPost"); // "blog_post"
toTableName("OrderLineItem"); // "order_line_item"
import { toSnakeCase } from "@temelj/string";
function toEnvVarName(key: string): string {
return toSnakeCase(key).toUpperCase();
}
toEnvVarName("apiKey"); // "API_KEY"
toEnvVarName("databaseUrl"); // "DATABASE_URL"
toEnvVarName("maxConnections"); // "MAX_CONNECTIONS"
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