Skip to content

Commit 88d8bdf

Browse files
cyfung1031CodFrm
andauthored
⚡️ toCamelCase 微优化 (#930)
* toCamelCase 微优化 * 添加单元测试 * concurrent * 调整代码 --------- Co-authored-by: 王一之 <yz@ggnb.top>
1 parent dbed242 commit 88d8bdf

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

src/pkg/utils/utils.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it, beforeAll } from "vitest";
2-
import { checkSilenceUpdate, cleanFileName, stringMatching } from "./utils";
2+
import { checkSilenceUpdate, cleanFileName, stringMatching, toCamelCase } from "./utils";
33
import { ltever, versionCompare } from "@App/pkg/utils/semver";
44
import { nextTime } from "./cron";
55
import dayjs from "dayjs";
@@ -331,3 +331,21 @@ describe.concurrent("stringMatching", () => {
331331
});
332332
});
333333
});
334+
335+
describe.concurrent("toCamelCase", () => {
336+
it.concurrent("应当将蛇形命名转换为驼峰命名", () => {
337+
expect(toCamelCase("cloud_sync")).toBe("CloudSync");
338+
expect(toCamelCase("cat_file_storage")).toBe("CatFileStorage");
339+
expect(toCamelCase("enable_eslint")).toBe("EnableEslint");
340+
expect(toCamelCase("eslint_config")).toBe("EslintConfig");
341+
});
342+
343+
it.concurrent("应当正确处理单词配置键", () => {
344+
expect(toCamelCase("language")).toBe("Language");
345+
});
346+
347+
it.concurrent("应当正确处理多下划线配置键", () => {
348+
expect(toCamelCase("editor_type_definition")).toBe("EditorTypeDefinition");
349+
expect(toCamelCase("script_list_column_width")).toBe("ScriptListColumnWidth");
350+
});
351+
});

src/pkg/utils/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ export const obtainBlackList = (strBlacklist: string | null | undefined) => {
334334

335335
// 将蛇形的 key 转换为驼峰的函数名
336336
export function toCamelCase(key: SystemConfigKey) {
337-
return key.replace(/_([a-z])/g, (g) => g[1].toUpperCase()).replace(/^([a-z])/, (g) => g.toUpperCase());
337+
return key.replace(/^[a-z]|_([a-z])/g, (_, c = _) => c.toUpperCase());
338338
}
339339

340340
export function cleanFileName(name: string): string {

0 commit comments

Comments
 (0)