@rbxts/jsnatives
toSnakeCase
Converts a string to snake_case format.
Signature
function toSnakeCase(str: string): string
Description
The toSnakeCase
function converts a string to snake_case format. It replaces spaces, hyphens, and other separators with underscores, and transforms the string into a format where all letters are lowercase with words separated by underscores.
Parameters
str
: The string to convert to snake_case.
Return value
- A new string in snake_case format.
Examples
Basic usage
// Converting different formats to snake_caseconst space = String.toSnakeCase("hello world");print(space); // Outputs: "hello_world"
const camel = String.toSnakeCase("helloWorld");print(camel); // Outputs: "hello_world"
const pascal = String.toSnakeCase("HelloWorld");print(pascal); // Outputs: "hello_world"
const kebab = String.toSnakeCase("hello-world");print(kebab); // Outputs: "hello_world"
Multiple words
// Converting multi-word stringsconst multiple = String.toSnakeCase("hello beautiful world");print(multiple); // Outputs: "hello_beautiful_world"
const mixedSeparators = String.toSnakeCase("hello_beautiful-world");print(mixedSeparators); // Outputs: "hello_beautiful_world"
Handling edge cases
// Empty stringconst empty = String.toSnakeCase("");print(empty); // Outputs: ""
// Single wordconst single = String.toSnakeCase("hello");print(single); // Outputs: "hello"
// Already in snake_caseconst alreadySnake = String.toSnakeCase("hello_world");print(alreadySnake); // Outputs: "hello_world"
// Uppercaseconst uppercase = String.toSnakeCase("HELLO WORLD");print(uppercase); // Outputs: "h_e_l_l_o_w_o_r_l_d" (each uppercase is treated as new word)
Converting from other cases
// From sentence caseconst sentence = String.toSnakeCase("This is a sentence");print(sentence); // Outputs: "this_is_a_sentence"
// From title caseconst title = String.toSnakeCase("This Is A Title");print(title); // Outputs: "this_is_a_title"
// From camelCaseconst camel = String.toSnakeCase("userFirstName");print(camel); // Outputs: "user_first_name"
// From PascalCaseconst pascal = String.toSnakeCase("UserLoginRequest");print(pascal); // Outputs: "user_login_request"
// From kebab-caseconst kebab = String.toSnakeCase("api-response-code");print(kebab); // Outputs: "api_response_code"
Practical applications
// Create database column namesfunction createColumnName(name: string): string { return String.toSnakeCase(name);}
print(createColumnName("UserFirstName")); // Outputs: "user_first_name"print(createColumnName("Created At")); // Outputs: "created_at"
// Normalize configuration keysfunction normalizeConfigKey(key: string): string { return String.toSnakeCase(key);}
const configKeys = [ "apiKey", "api-url", "Max Items", "DatabaseConnection"];
const normalizedKeys = configKeys.map(normalizeConfigKey);print(normalizedKeys);// Outputs: ["api_key", "api_url", "max_items", "database_connection"]