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

转小驼峰,转大驼峰通过所有测试用例

This commit is contained in:
程序员小墨 2024-04-03 00:15:29 +08:00
parent 30224cfeb6
commit 31648a4805
2 changed files with 18 additions and 14 deletions

View File

@ -12,7 +12,7 @@ import { transformMutliLineText, transformText } from './text-transform';
export const toCamelCase: ConvertFunction = (str: string, eol: EOL): string => { export const toCamelCase: ConvertFunction = (str: string, eol: EOL): string => {
// Cut text 切割文本 // Cut text 切割文本
const results: Array<TransformTextResult> = transformMutliLineText(str); const results: Array<TransformTextResult> = transformMutliLineText(str);
console.log('results', results); // console.log('results', results);
const transformedLines: Array<string> = []; const transformedLines: Array<string> = [];
for (const result of results) { for (const result of results) {
@ -54,16 +54,20 @@ export const toCamelCase: ConvertFunction = (str: string, eol: EOL): string => {
*/ */
export const toPascalCase: ConvertFunction = (str: string, eol: EOL): string => { export const toPascalCase: ConvertFunction = (str: string, eol: EOL): string => {
// Cut text 切割文本 // Cut text 切割文本
let result = transformText(str); const results: Array<TransformTextResult> = transformMutliLineText(str);
console.log('result', result); // console.log('results', results);
// Post Process 后置处理 const transformedLines: Array<string> = [];
const words = result.trimResult.split('|'); for (const result of results) {
const pascalCaseWords = words.map((word, index) => { const words = result.trimResult.split('|');
// 首字母大写 const pascalCaseWords = words.map((word, index) => {
return word.charAt(0).toUpperCase() + word.slice(1); // 首字母大写
}); return word.charAt(0).toUpperCase() + word.slice(1);
return result.leadingSpace + pascalCaseWords.join('') + result.trailingSpace; });
const transformedLine = result.leadingSpace + pascalCaseWords.join('') + result.trailingSpace;
transformedLines.push(transformedLine);
}
return transformedLines.join(eol);
// return str.replace(/(^\w|_\w)/g, (g) => g.toUpperCase().replace('_', '')); // return str.replace(/(^\w|_\w)/g, (g) => g.toUpperCase().replace('_', ''));
}; };

View File

@ -123,13 +123,13 @@ const testGroups: Array<TestCaseGroup> = [
input: input:
'How do you\bdo?\n How do you\tdo!' 'How do you\bdo?\n How do you\tdo!'
, ,
eol: [LF, CRLF], eol: [LF],
transformText: [ transformText: [
'how|do|you|\b|do|?', 'how|do|you|\b|do|?',
' how|do|you|\t|do|!', ' how|do|you|\t|do|!',
], ],
output: { output: {
camelCase: 'howDoYou\bDo?\n HowDoYou\tDo!', camelCase: 'howDoYou\bDo?\n howDoYou\tDo!',
pascalCase: 'HowDoYou\bDo?\n HowDoYou\tDo!', pascalCase: 'HowDoYou\bDo?\n HowDoYou\tDo!',
}, },
}, },
@ -366,8 +366,8 @@ const testGroups: Array<TestCaseGroup> = [
' exercisingPermissionsGrantedByThisLicense.' ' exercisingPermissionsGrantedByThisLicense.'
, ,
pascalCase: pascalCase:
' &quot;You&quot; (or &quot;Your&quot;) shall mean an individual or Legal Entity\n' + ' &Quot;You&Quot;(Or&Quot;Your&Quot;)ShallMeanAnIndividualOrLegalEntity\n' +
' exercising permissions granted by this License.' ' ExercisingPermissionsGrantedByThisLicense.'
, ,
}, },
}, },