From 34383439e0d6c0e5db15bf58741f7cd2330c97e8 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 17 Feb 2024 11:19:11 +0800 Subject: [PATCH] feat: add toCameCase to strings utils. --- apps/vue/src/utils/strings.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/apps/vue/src/utils/strings.ts b/apps/vue/src/utils/strings.ts index efec08194..bf7cfe6be 100644 --- a/apps/vue/src/utils/strings.ts +++ b/apps/vue/src/utils/strings.ts @@ -20,6 +20,18 @@ export function format(formatted: string, args: object | any[]) { } return formatted; } +/** + * 字符串转驼峰格式 + * @param str 字符串 + * @returns 字符串的驼峰表示法 + * @example toCamelCase('hello-world') => helloWorld + * @example toCamelCase('my_variable_name') => myVariableName + */ +export function toCamelCase(str: string) { + return str.replace(/[-_\s](\w)/g, function (_, c) { + return c ? c.toUpperCase() : ''; + }); +} /** * * @param str 字符串是否为空或空格