Skip to content

Chrome DevTools for coding agents

chore: use string utilities from devtools frontend. (#2549)

yulunz0dbc8c12dfa6
diff --git a/src/telemetry/flagUtils.ts b/src/telemetry/flagUtils.ts
index a585e119..b4adb94e 100644
--- a/src/telemetry/flagUtils.ts
+++ b/src/telemetry/flagUtils.ts
@@ -5,11 +5,13 @@
*/
import type {cliOptions} from '../bin/chrome-devtools-mcp-cli-options.js';
-import {toSnakeCase} from '../utils/string.js';
+import {DevTools} from '../third_party/index.js';
import {stripUnderscoreBeforeNumber} from './transformation.js';
import type {FlagUsage} from './types.js';
+const {StringUtilities} = DevTools.Platform;
+
type CliOptions = typeof cliOptions;
/**
@@ -44,7 +46,9 @@ export function computeFlagUsage(
for (const [flagName, config] of Object.entries(options)) {
const value = args[flagName];
- const snakeCaseName = stripUnderscoreBeforeNumber(toSnakeCase(flagName));
+ const snakeCaseName = stripUnderscoreBeforeNumber(
+ StringUtilities.toSnakeCase(flagName),
+ );
// If there isn't a default value provided for the flag,
// we're going to log whether it's present on the args user
@@ -85,7 +89,9 @@ export function getPossibleFlagMetrics(options: CliOptions): FlagMetric[] {
const metrics: FlagMetric[] = [];
for (const [flagName, config] of Object.entries(options)) {
- const snakeCaseName = stripUnderscoreBeforeNumber(toSnakeCase(flagName));
+ const snakeCaseName = stripUnderscoreBeforeNumber(
+ StringUtilities.toSnakeCase(flagName),
+ );
// _present is always a possible metric
metrics.push({
diff --git a/src/utils/string.ts b/src/utils/string.ts
deleted file mode 100644
index dee2c36d..00000000
--- a/src/utils/string.ts
+++ /dev/null
@@ -1,38 +0,0 @@
-/**
- * @license
- * Copyright 2026 Google LLC
- * SPDX-License-Identifier: Apache-2.0
- */
-
-/**
- * Converts a given string to snake_case.
- * This function handles camelCase, PascalCase, and acronyms, including transitions between letters and numbers.
- * It uses Unicode-aware regular expressions (`\p{L}`, `\p{N}`, `\p{Lu}`, `\p{Ll}` with the `u` flag)
- * to correctly process letters and numbers from various languages.
- *
- * @param text The input string to convert to snake_case.
- * @returns The snake_case version of the input string.
- */
-export function toSnakeCase(text: string): string {
- if (!text) {
- return '';
- }
- // First, handle case-based transformations to insert underscores correctly.
- // 1. Add underscore between a letter and a number.
- // e.g., "version2" -> "version_2"
- // 2. Add underscore between an uppercase letter sequence and a following uppercase+lowercase sequence.
- // e.g., "APIFlags" -> "API_Flags"
- // 3. Add underscore between a lowercase/number and an uppercase letter.
- // e.g., "lastName" -> "last_Name", "version_2Update" -> "version_2_Update"
- // 4. Replace sequences of non-alphanumeric with a single underscore
- // 5. Remove any leading or trailing underscores.
- const result = text
- .replace(/(\p{L})(\p{N})/gu, '$1_$2') // 1
- .replace(/(\p{Lu}+)(\p{Lu}\p{Ll})/gu, '$1_$2') // 2
- .replace(/(\p{Ll}|\p{N})(\p{Lu})/gu, '$1_$2') // 3
- .toLowerCase()
- .replace(/[^\p{L}\p{N}]+/gu, '_') // 4
- .replace(/^_|_$/g, ''); // 5
-
- return result;
-}
diff --git a/tests/utils/string.test.ts b/tests/utils/string.test.ts
deleted file mode 100644
index 033107fa..00000000
--- a/tests/utils/string.test.ts
+++ /dev/null
@@ -1,74 +0,0 @@
-/**
- * @license
- * Copyright 2026 Google LLC
- * SPDX-License-Identifier: Apache-2.0
- */
-
-import assert from 'node:assert';
-import {describe, it} from 'node:test';
-
-import {toSnakeCase} from '../../src/utils/string.js';
-
-describe('toSnakeCase', () => {
- it('should convert camelCase to snake_case', () => {
- assert.strictEqual(toSnakeCase('lastName'), 'last_name');
- assert.strictEqual(toSnakeCase('browserUrl'), 'browser_url');
- assert.strictEqual(
- toSnakeCase('acceptInsecureCerts'),
- 'accept_insecure_certs',
- );
- });
-
- it('should convert PascalCase to snake_case', () => {
- assert.strictEqual(toSnakeCase('LastName'), 'last_name');
- assert.strictEqual(toSnakeCase('XMLHttpRequest'), 'xml_http_request');
- });
-
- it('should handle acronym runs', () => {
- assert.strictEqual(toSnakeCase('APIFlags'), 'api_flags');
- assert.strictEqual(toSnakeCase('HTMLParser'), 'html_parser');
- assert.strictEqual(toSnakeCase('parseHTML'), 'parse_html');
- assert.strictEqual(toSnakeCase('fooBAR'), 'foo_bar');
- assert.strictEqual(toSnakeCase('ABC'), 'abc');
- });
-
- it('should insert underscores at letter-to-digit transitions', () => {
- assert.strictEqual(toSnakeCase('version2'), 'version_2');
- assert.strictEqual(toSnakeCase('v2'), 'v_2');
- assert.strictEqual(toSnakeCase('myVar123Name'), 'my_var_123_name');
- assert.strictEqual(toSnakeCase('getHTTP2Response'), 'get_http_2_response');
- });
-
- it('should not insert underscores at digit-to-lowercase transitions', () => {
- assert.strictEqual(toSnakeCase('2fast'), '2fast');
- assert.strictEqual(toSnakeCase('a1B2c3'), 'a_1_b_2c_3');
- });
-
- it('should keep already snake_case input unchanged', () => {
- assert.strictEqual(toSnakeCase('already_snake'), 'already_snake');
- assert.strictEqual(toSnakeCase('snake_case_2'), 'snake_case_2');
- });
-
- it('should collapse non-alphanumeric sequences into a single underscore', () => {
- assert.strictEqual(toSnakeCase('foo-bar baz'), 'foo_bar_baz');
- assert.strictEqual(toSnakeCase('foo.bar/baz'), 'foo_bar_baz');
- });
-
- it('should strip leading and trailing underscores', () => {
- assert.strictEqual(toSnakeCase('__leading__'), 'leading');
- assert.strictEqual(toSnakeCase(' spaced '), 'spaced');
- });
-
- it('should handle non-ASCII letters', () => {
- assert.strictEqual(toSnakeCase('снегУпал'), 'снег_упал');
- });
-
- it('should return an empty string for an empty string', () => {
- assert.strictEqual(toSnakeCase(''), '');
- });
-
- it('should lowercase a single character', () => {
- assert.strictEqual(toSnakeCase('A'), 'a');
- assert.strictEqual(toSnakeCase('a'), 'a');
- });
-});