Skip to content

objectenvy / toCamelCase

Function: toCamelCase()

ts
function toCamelCase(str): string;

Defined in: utils.ts:35

Convert a SCREAMING_SNAKE_CASE string to camelCase.

Parameters

ParameterTypeDescription
strstringA string in SCREAMING_SNAKE_CASE or snake_case form.

Returns

string

The camelCase equivalent.

Remarks

Lowercases the entire string, then capitalises the first letter of every segment that follows an underscore. Leading and trailing underscores are preserved as empty string collapses (the regex only matches _ followed by a letter). This is a simple, non-Unicode-aware transformation; non-ASCII letters are not affected.

Use When

  • You need to convert a raw environment variable key to a JavaScript property name.
  • You're normalising keys before building a config object.

Avoid When

  • Input may contain non-ASCII letters — the regex captures only [a-z] after the underscore.
  • You need PascalCase output — capitalise the first character of the result separately.

Pitfalls

  • NEVER assume toCamelCase(toSnakeCase(x)) === x for all inputs — BECAUSE acronym boundaries (e.g., apiURLAPI_URLapiUrl) collapse consecutive capitals, so the round-trip is lossy for strings with adjacent uppercase letters.

Example

ts
import { toCamelCase } from 'objectenvy';
toCamelCase('PORT_NUMBER');           // 'portNumber'
toCamelCase('LOG_LEVEL');             // 'logLevel'
toCamelCase('DATABASE_HOST');         // 'databaseHost'

See

toSnakeCase for the inverse operation

Released under the MIT License.