diff --git a/src/js/humpToUnderline.ts b/src/js/humpToUnderline.ts new file mode 100644 index 0000000..f1c301e --- /dev/null +++ b/src/js/humpToUnderline.ts @@ -0,0 +1,14 @@ +/** + * 驼峰命名改为下划线命名,数字不重新起一个下划线 + * @param str 需转换的字符串 + * @returns 转换的结果 + */ +export const humpToUnderline = (str: string) => { + let temp = str.replace(/[A-Z]/g, (match) => { + return `_${match.toLowerCase()}`; + }); + if (temp.slice(0, 1) === "_") { + temp = temp.slice(1); + } + return temp; +};