@rbxts/jsnatives
toCamelCase
Converts a string to camelCase format.
Signature
function toCamelCase(str: string): string
Description
The toCamelCase
function converts a string to camelCase format. It removes spaces, hyphens, underscores, and other separators, and transforms the string into a format where the first word is lowercase and subsequent words start with a capital letter, with no spaces or separators between them.
Parameters
str
: The string to convert to camelCase.
Return value
- A new string in camelCase format.
Examples
Basic usage
// Converting different formats to camelCaseconst space = String.toCamelCase("hello world");print(space); // Outputs: "helloWorld"
const kebab = String.toCamelCase("hello-world");print(kebab); // Outputs: "helloWorld"
const snake = String.toCamelCase("hello_world");print(snake); // Outputs: "helloWorld"
const pascal = String.toCamelCase("HelloWorld");print(pascal); // Outputs: "helloWorld"
Multiple words
// Converting multi-word stringsconst multiple = String.toCamelCase("hello beautiful world");print(multiple); // Outputs: "helloBeautifulWorld"
const mixedSeparators = String.toCamelCase("hello_beautiful-world");print(mixedSeparators); // Outputs: "helloBeautifulWorld"
Handling edge cases
// Empty stringconst empty = String.toCamelCase("");print(empty); // Outputs: ""
// Single word uppercase, each next letter will be considered new wordconst single = String.toCamelCase("HELLO");print(single); // Outputs: "hELLO"
// Already in camelCaseconst alreadyCamel = String.toCamelCase("helloWorld");print(alreadyCamel); // Outputs: "helloWorld"
Converting from other cases
// From sentence caseconst sentence = String.toCamelCase("This is a sentence");print(sentence); // Outputs: "thisIsASentence"
// From title caseconst title = String.toCamelCase("This Is A Title");print(title); // Outputs: "thisIsATitle"
// From snake_caseconst snake = String.toCamelCase("user_first_name");print(snake); // Outputs: "userFirstName"
// From kebab-caseconst kebab = String.toCamelCase("api-response-code");print(kebab); // Outputs: "apiResponseCode"
// From PascalCaseconst pascal = String.toCamelCase("UserLoginRequest");print(pascal); // Outputs: "userLoginRequest"