| name | string-ts-usage |
| description | Generate code examples and usage patterns for the string-ts library. Use when writing examples, documentation, demos, answering questions about string-ts API, or helping users understand how to use string-ts for type-safe string manipulation, case conversion, object key transformation, environment variables, and payload transformation. |
string-ts Usage Guide
What is string-ts?
A strongly-typed string manipulation library for TypeScript. Every function works at both runtime AND type level, preserving literal string types through transformations — something native JS string methods cannot do.
const str = 'hello-world'
const result = str.replace('-', ' ')
import { replace } from 'string-ts'
const result = replace('hello-world', '-', ' ')
Key constraints: ASCII-only (no international characters or emojis). Requires TypeScript 5+.
Installation
npm install string-ts
Use Case 1: Environment Variables
Transform SCREAMING_SNAKE_CASE environment variables into type-safe camelCase config objects.
Basic env config
import { deepCamelKeys } from 'string-ts'
const env = {
DATABASE_URL: process.env.DATABASE_URL,
DATABASE_PORT: process.env.DATABASE_PORT,
API_BASE_URL: process.env.API_BASE_URL,
MAX_RETRY_COUNT: process.env.MAX_RETRY_COUNT,
} as const
const config = deepCamelKeys(env)
config.databaseUrl
config.dataBaseUrl
Nested env config
import { deepCamelKeys } from 'string-ts'
const rawConfig = {
APP_NAME: 'my-app',
SERVER: {
HOST_NAME: 'localhost',
PORT_NUMBER: '3000',
},
AUTH: {
JWT_SECRET: 'secret',
TOKEN_EXPIRY: '3600',
},
} as const
const config = deepCamelKeys(rawConfig)
Use Case 2: API Payload Transformation
Transform between snake_case (common in Ruby/Python APIs) and camelCase (JS convention).
Incoming response (snake_case → camelCase)
import { deepCamelKeys } from 'string-ts'
import type { DeepCamelKeys } from 'string-ts'
type ApiResponse = {
user_id: number
first_name: string
last_name: string
email_address: string
created_at: string
address_info: {
street_name: string
zip_code: string
}
}
function fetchUser(): Promise<DeepCamelKeys<ApiResponse>> {
return fetch('/api/user')
.then(res => res.json())
.then(data => deepCamelKeys(data))
}
const user = await fetchUser()
user.firstName
user.addressInfo.streetName
Outgoing request (camelCase → snake_case)
import { deepSnakeKeys } from 'string-ts'
const payload = {
firstName: 'John',
lastName: 'Doe',
emailAddress: 'john@example.com',
shippingAddress: {
streetName: '123 Main St',
zipCode: '12345',
},
} as const
const body = deepSnakeKeys(payload)
Use Case 3: Type-Safe Routing and Path Building
import { join, split, replace } from 'string-ts'
const segments = split('/api/v1/users', '/')
const path = join(['api', 'v1', 'users'], '/')
const endpoint = replace('/users/:id/posts/:postId', ':id', '42')
Use Case 4: CSS Class and HTML Attribute Generation
import { kebabCase } from 'string-ts'
const componentName = 'UserProfileCard' as const
const className = kebabCase(componentName)
import { camelCase } from 'string-ts'
const dataAttr = 'data-user-id' as const
const propName = camelCase(dataAttr)
Most Common Functions
Case Conversions
import {
camelCase,
pascalCase,
kebabCase,
snakeCase,
constantCase,
titleCase,
delimiterCase,
capitalize,
uncapitalize,
} from 'string-ts'
camelCase('hello-world')
pascalCase('hello-world')
kebabCase('helloWorld')
snakeCase('helloWorld')
constantCase('helloWorld')
titleCase('helloWorld')
delimiterCase('helloWorld', '.')
capitalize('hello')
uncapitalize('Hello')
Strongly-Typed Native Methods
import {
replace,
replaceAll,
split,
join,
trim,
slice,
includes,
startsWith,
endsWith,
repeat,
length,
charAt,
} from 'string-ts'
replace('hello world', 'world', 'ts')
replaceAll('ababa', 'a', 'o')
split('a-b-c', '-')
join(['a', 'b', 'c'], '-')
trim(' hello ')
slice('hello', 1, 4)
includes('hello', 'ell')
startsWith('hello', 'hel')
endsWith('hello', 'llo')
repeat('ab', 3)
length('hello')
charAt('hello', 1)
Object Key Transformations
import {
camelKeys,
deepCamelKeys,
snakeKeys,
deepSnakeKeys,
kebabKeys,
deepKebabKeys,
pascalKeys,
deepPascalKeys,
constantKeys,
deepConstantKeys,
delimiterKeys,
deepDelimiterKeys,
} from 'string-ts'
camelKeys({ 'foo-bar': { 'baz-qux': 1 } })
deepCamelKeys({ 'foo-bar': { 'baz-qux': 1 } })
Utility Functions
import { words, reverse, truncate } from 'string-ts'
words('helloWorld')
words('hello-world')
reverse('hello')
truncate('Hello, World!', 8)
truncate('Hello, World!', 8, '…')
Type-Level Usage
All functions have corresponding types for use in generics and type utilities:
import type {
CamelCase,
SnakeCase,
KebabCase,
PascalCase,
Split,
Join,
Replace,
DeepCamelKeys,
DeepSnakeKeys,
} from 'string-ts'
type A = CamelCase<'hello-world'>
type B = SnakeCase<'helloWorld'>
type C = Split<'a-b-c', '-'>
type D = Join<['a', 'b'], '.'>
type E = Replace<'hello', 'l', 'r'>
Character type guards (type-level only)
import type { IsDigit, IsLetter, IsUpper, IsLower, IsSeparator } from 'string-ts'
type A = IsDigit<'5'>
type B = IsLetter<'a'>
type C = IsUpper<'A'>
type D = IsLower<'a'>
type E = IsSeparator<'-'>
Native String Method Augmentation
Opt-in to enhance native string methods with strong types:
import 'string-ts/native'
const str = 'hello-world' as const
str.split('-')
str.replace('-', '_')
str.toUpperCase()
str.trim()
Tips
- Always use
as const on string literals to get the best type inference
- Every runtime function has a matching type export (e.g.,
camelCase → CamelCase)
- Use
deep*Keys functions for nested objects, *Keys for flat/shallow transforms
- Regex patterns in
replace/replaceAll fall back to string return type
- The library is fully tree-shakeable — import only what is needed