多行内容逐行处理;更新测试用例;测试用例可以指定EOL换行符;transformMutliLineText 通过所有测试用例
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import { transformText } from './text-transform';
|
||||
import { ConvertFunction, EOL } from '../type-definition/convert-function-type';
|
||||
import { TransformTextResult } from '../type-definition/text-transform-type';
|
||||
import { transformMutliLineText, transformText } from './text-transform';
|
||||
|
||||
/**
|
||||
* 转小驼峰 to Camel Case
|
||||
@@ -7,26 +9,41 @@ import { transformText } from './text-transform';
|
||||
* @returns
|
||||
* @since 2024-03-28
|
||||
*/
|
||||
export function toCamelCase(str: string): string {
|
||||
export const toCamelCase: ConvertFunction = (str: string, eol: EOL): string => {
|
||||
// Cut text 切割文本
|
||||
let result = transformText(str);
|
||||
console.log('result', result);
|
||||
const results: Array<TransformTextResult> = transformMutliLineText(str);
|
||||
console.log('results', results);
|
||||
|
||||
// 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);
|
||||
const transformedLines: Array<string> = [];
|
||||
for (const result of results) {
|
||||
// Post Process 后置处理
|
||||
const words = result.trimResult.split('|');
|
||||
let isFirstWord: boolean = true;// 用于判断首词小写
|
||||
const camelCaseWords: Array<string> = [];
|
||||
for (let index = 0; index < words.length; index++) {
|
||||
const word = words[index];
|
||||
const firstLetter = word.charAt(0);
|
||||
const pascalCaseWord = firstLetter.toUpperCase() + word.slice(1);
|
||||
if (isFirstWord) {
|
||||
camelCaseWords.push(word);
|
||||
if (firstLetter !== firstLetter.toUpperCase()) {
|
||||
// 是大写 (A-Z), 再后面的词句不再是首词
|
||||
isFirstWord = false;
|
||||
}
|
||||
} else {
|
||||
camelCaseWords.push(pascalCaseWord);
|
||||
}
|
||||
|
||||
if (word === '\n' || word === '\r\n') {
|
||||
isFirstWord = true; // 换行后,重新计算首词
|
||||
}
|
||||
}
|
||||
});
|
||||
return result.leadingSpace + camelCaseWords.join('') + result.trailingSpace;
|
||||
|
||||
const transformedLine = result.leadingSpace + camelCaseWords.join('') + result.trailingSpace;
|
||||
transformedLines.push(transformedLine);
|
||||
}
|
||||
return transformedLines.join(eol);
|
||||
// return str.replace(/_([a-z])/g, (g) => g[1].toUpperCase());
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 转大驼峰 to Pascal Case
|
||||
@@ -35,7 +52,7 @@ export function toCamelCase(str: string): string {
|
||||
* @returns
|
||||
* @since 2024-03-28
|
||||
*/
|
||||
export function toPascalCase(str: string): string {
|
||||
export const toPascalCase: ConvertFunction = (str: string, eol: EOL): string => {
|
||||
// Cut text 切割文本
|
||||
let result = transformText(str);
|
||||
console.log('result', result);
|
||||
@@ -49,7 +66,7 @@ export function toPascalCase(str: string): string {
|
||||
return result.leadingSpace + pascalCaseWords.join('') + result.trailingSpace;
|
||||
|
||||
// return str.replace(/(^\w|_\w)/g, (g) => g.toUpperCase().replace('_', ''));
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 转大写 to Upper Case
|
||||
@@ -57,10 +74,10 @@ export function toPascalCase(str: string): string {
|
||||
* @param {string} str user selection
|
||||
* @returns
|
||||
* @since 2024-03-28
|
||||
*/
|
||||
export function toUpperCase(str: string): string {
|
||||
*/
|
||||
export const toUpperCase: ConvertFunction = (str: string, eol: EOL): string => {
|
||||
return str.toUpperCase();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 转小写 to Lower Case
|
||||
@@ -69,6 +86,6 @@ export function toUpperCase(str: string): string {
|
||||
* @returns
|
||||
* @since 2024-03-28
|
||||
*/
|
||||
export function toLowerCase(str: string): string {
|
||||
export const toLowerCase: ConvertFunction = (str: string, eol: EOL): string => {
|
||||
return str.toLowerCase();
|
||||
}
|
||||
};
|
||||
|
@@ -2,6 +2,15 @@ import { TransformTextResult } from "../type-definition/text-transform-type";
|
||||
|
||||
const logDebugInfo = false;
|
||||
|
||||
export function transformMutliLineText(multilineInput: string): Array<TransformTextResult> {
|
||||
const results: Array<TransformTextResult> = [];
|
||||
const lines = multilineInput.split(/\r?\n/);
|
||||
for (const line of lines) {
|
||||
// console.log('line', '->' + line + '<-');
|
||||
results.push(transformText(line));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
/**
|
||||
* 分词
|
||||
*
|
||||
@@ -17,6 +26,8 @@ export function transformText(input: string): TransformTextResult {
|
||||
? '' // 字符串全为空格时,将尾空格置为空字符串
|
||||
: input.match(/ +$/);
|
||||
|
||||
// const debug = { input, leadingSpaces, trailingSpaces };
|
||||
|
||||
// 去除首尾空格
|
||||
// 不可以使用 input = input.trim(); 否则换行会被替换掉
|
||||
input = input.replace(/^ +| +$/g, '');
|
||||
@@ -61,5 +72,6 @@ export function transformText(input: string): TransformTextResult {
|
||||
trailingSpace: trailingSpaceStr,
|
||||
result: noTrimResult,
|
||||
trimResult: result,
|
||||
// debug: debug
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user