1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee

实现 转小驼峰 Camel Case;转大驼峰 Pascal Case;transformText 函数通过测试用例

This commit is contained in:
2024-04-02 22:30:45 +08:00
parent 181d2b4c6f
commit c7a16581ab
5 changed files with 108 additions and 27 deletions

View File

@@ -8,13 +8,24 @@ import { transformText } from './text-transform';
* @since 2024-03-28
*/
export function toCamelCase(str: string): string {
// 切割文本
const result = transformText(str);
// Cut text 切割文本
let result = transformText(str);
console.log('result', result);
// TODO
// Post Process 后置处理
const words = result.trimResult.split('|');
const camelCaseWords = words.map((word, index) => {
if (index === 0) {
// 第一个单词保持不变
return word;
} else {
// 其他单词首字母大写
return word.charAt(0).toUpperCase() + word.slice(1);
}
});
return result.leadingSpace + camelCaseWords.join('') + result.trailingSpace;
return str.replace(/_([a-z])/g, (g) => g[1].toUpperCase());
// return str.replace(/_([a-z])/g, (g) => g[1].toUpperCase());
}
/**
@@ -25,7 +36,19 @@ export function toCamelCase(str: string): string {
* @since 2024-03-28
*/
export function toPascalCase(str: string): string {
return str.replace(/(^\w|_\w)/g, (g) => g.toUpperCase().replace('_', ''));
// Cut text 切割文本
let result = transformText(str);
console.log('result', result);
// Post Process 后置处理
const words = result.trimResult.split('|');
const pascalCaseWords = words.map((word, index) => {
// 首字母大写
return word.charAt(0).toUpperCase() + word.slice(1);
});
return result.leadingSpace + pascalCaseWords.join('') + result.trailingSpace;
// return str.replace(/(^\w|_\w)/g, (g) => g.toUpperCase().replace('_', ''));
}
/**

View File

@@ -1,3 +1,4 @@
import { TransformTextResult } from "../type-definition/text-transform-type";
const logDebugInfo = false;
@@ -7,19 +8,21 @@ const logDebugInfo = false;
* @param str
* @since 2024-04-02
*/
export function transformText(input: string): string {
export function transformText(input: string): TransformTextResult {
logDebugInfo && console.log('input ', '->' + input + '<-');
// 记录首尾空格
const leadingSpaces = input.match(/^ +/);
const trailingSpaces = input.match(/ +$/);
const trailingSpaces = /^[ ]+$/.test(input)
? '' // 字符串全为空格时,将尾空格置为空字符串
: input.match(/ +$/);
// 去除首尾空格
input = input.trim();
// 不可以使用 input = input.trim(); 否则换行会被替换掉
input = input.replace(/^ +| +$/g, '');
// 使用正则表达式匹配中英文字母、连字符、下划线空格
// 使用正则表达式匹配中英文字母、连字符、下划线空格
let result = input.replace(/([A-Za-z\-_ ]+)/g, (match: string) => {
// 替换连字符为 '|' (如有多个则合并)
match = match.replace(/[-_ ]+/g, '|');
@@ -48,9 +51,15 @@ export function transformText(input: string): string {
result = result.replace(/(^[\|]+|[\|]+$)/g, '');
// 还原首尾空格
// result = (leadingSpaces ? (leadingSpaces[0] + '|') : '') + result + (trailingSpaces ? ('|' + trailingSpaces[0]) : '');
result = (leadingSpaces ? leadingSpaces[0] : '') + result + (trailingSpaces ? trailingSpaces[0] : '');
const leadingSpaceStr = leadingSpaces ? leadingSpaces[0] : '';
const trailingSpaceStr = trailingSpaces ? trailingSpaces[0] : '';
let noTrimResult = leadingSpaceStr + result + trailingSpaceStr;
logDebugInfo && console.log('output ', '->' + result + '<-');
return result;
return {
leadingSpace: leadingSpaceStr,
trailingSpace: trailingSpaceStr,
result: noTrimResult,
trimResult: result,
};
}