MCPcopy Create free account
hub / github.com/ChromeDevTools/chrome-devtools-mcp / toSnakeCase

Function toSnakeCase

src/utils/string.ts:16–38  ·  view source on GitHub ↗
(text: string)

Source from the content-addressed store, hash-verified

14 * @returns The snake_case version of the input string.
15 */
16export function toSnakeCase(text: string): string {
17 if (!text) {
18 return '';
19 }
20 // First, handle case-based transformations to insert underscores correctly.
21 // 1. Add underscore between a letter and a number.
22 // e.g., "version2" -> "version_2"
23 // 2. Add underscore between an uppercase letter sequence and a following uppercase+lowercase sequence.
24 // e.g., "APIFlags" -> "API_Flags"
25 // 3. Add underscore between a lowercase/number and an uppercase letter.
26 // e.g., "lastName" -> "last_Name", "version_2Update" -> "version_2_Update"
27 // 4. Replace sequences of non-alphanumeric with a single underscore
28 // 5. Remove any leading or trailing underscores.
29 const result = text
30 .replace(/(\p{L})(\p{N})/gu, '$1_$2') // 1
31 .replace(/(\p{Lu}+)(\p{Lu}\p{Ll})/gu, '$1_$2') // 2
32 .replace(/(\p{Ll}|\p{N})(\p{Lu})/gu, '$1_$2') // 3
33 .toLowerCase()
34 .replace(/[^\p{L}\p{N}]+/gu, '_') // 4
35 .replace(/^_|_$/g, ''); // 5
36
37 return result;
38}

Callers 2

computeFlagUsageFunction · 0.85
getPossibleFlagMetricsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected