diff --git a/src/pkg/utils/utils.test.ts b/src/pkg/utils/utils.test.ts index 55027dfe1..3289b6dc4 100644 --- a/src/pkg/utils/utils.test.ts +++ b/src/pkg/utils/utils.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it, beforeAll } from "vitest"; -import { checkSilenceUpdate, cleanFileName, stringMatching } from "./utils"; +import { checkSilenceUpdate, cleanFileName, stringMatching, toCamelCase } from "./utils"; import { ltever, versionCompare } from "@App/pkg/utils/semver"; import { nextTime } from "./cron"; import dayjs from "dayjs"; @@ -331,3 +331,21 @@ describe.concurrent("stringMatching", () => { }); }); }); + +describe.concurrent("toCamelCase", () => { + it.concurrent("应当将蛇形命名转换为驼峰命名", () => { + expect(toCamelCase("cloud_sync")).toBe("CloudSync"); + expect(toCamelCase("cat_file_storage")).toBe("CatFileStorage"); + expect(toCamelCase("enable_eslint")).toBe("EnableEslint"); + expect(toCamelCase("eslint_config")).toBe("EslintConfig"); + }); + + it.concurrent("应当正确处理单词配置键", () => { + expect(toCamelCase("language")).toBe("Language"); + }); + + it.concurrent("应当正确处理多下划线配置键", () => { + expect(toCamelCase("editor_type_definition")).toBe("EditorTypeDefinition"); + expect(toCamelCase("script_list_column_width")).toBe("ScriptListColumnWidth"); + }); +}); diff --git a/src/pkg/utils/utils.ts b/src/pkg/utils/utils.ts index d4b62df4b..555f72011 100644 --- a/src/pkg/utils/utils.ts +++ b/src/pkg/utils/utils.ts @@ -334,7 +334,7 @@ export const obtainBlackList = (strBlacklist: string | null | undefined) => { // 将蛇形的 key 转换为驼峰的函数名 export function toCamelCase(key: SystemConfigKey) { - return key.replace(/_([a-z])/g, (g) => g[1].toUpperCase()).replace(/^([a-z])/, (g) => g.toUpperCase()); + return key.replace(/^[a-z]|_([a-z])/g, (_, c = _) => c.toUpperCase()); } export function cleanFileName(name: string): string {