diff --git a/src/main-code/text-conversion.ts b/src/main-code/text-conversion.ts index eb69305..c6f24fb 100644 --- a/src/main-code/text-conversion.ts +++ b/src/main-code/text-conversion.ts @@ -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('_', '')); } /** diff --git a/src/main-code/text-transform.ts b/src/main-code/text-transform.ts index 862de42..40925eb 100644 --- a/src/main-code/text-transform.ts +++ b/src/main-code/text-transform.ts @@ -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, + }; } diff --git a/src/test/extension.test.ts b/src/test/extension.test.ts index 400299c..cce8eb2 100644 --- a/src/test/extension.test.ts +++ b/src/test/extension.test.ts @@ -35,7 +35,8 @@ suite('Extension Test: run test case', () => { const inputList = Array.isArray(testCase.input) ? testCase.input : [testCase.input]; for (const input of inputList) { // 验证 transformText - assert.strictEqual(testCase.transformText, transformText(input)); + const transformTextResult = transformText(input); + assert.strictEqual(testCase.transformText, transformTextResult.result); // 验证转换 assert.strictEqual(testCase.output.camelCase, toCamelCase(input)); assert.strictEqual(testCase.output.pascalCase, toPascalCase(input)); diff --git a/src/test/test-case.ts b/src/test/test-case.ts index 2f4d7f8..ed83c65 100644 --- a/src/test/test-case.ts +++ b/src/test/test-case.ts @@ -24,13 +24,43 @@ const testGroups: Array = [ pascalCase: ' ', }, }, + { + title: 'empty input (contains space and enter 1)', + input: ' \n ', + transformText: ' \n ', + output: { + camelCase: ' \n ', + pascalCase: ' \n ', + }, + }, + { + title: 'empty input (contains space and enter 2)', + input: ' a\nb ', + transformText: ' a|\n|b ', + output: { + camelCase: ' a\nb ', + pascalCase: ' a\nb ', + }, + }, // 输入长文本 { title: 'long text input', input: `China's factory activity expanded in March after five consecutive months of contraction, an official survey revealed on Sunday, adding to a run of indicators that suggest the stabilization of the world's second-largest economy.` , - transformText: '', + transformText: + "china|'|s|factory|activity|expanded|in|march|after|five|consecutive|months|of|contraction|,|an|official|survey|revealed|on|sunday|,|adding|to|a|run|of|indicators|that|suggest|the|stabilization|of|the|world|'|s|second|largest|economy|." + , + output: { + camelCase: '', + pascalCase: '', + }, + }, + // 输入包含数字 + { + title: 'text and number input', + input: 'entity2Map', + transformText: 'entity|2|map', output: { camelCase: '', pascalCase: '', @@ -41,10 +71,9 @@ const testGroups: Array = [ title: 'enter input', input: `How do you\bdo? - How do you\tdo! - ` + How do you\tdo!` , - transformText: '', + transformText: 'how|do|you|\b|do|?\n|how|do|you|\t|do|!', output: { camelCase: '', pascalCase: '', @@ -56,7 +85,7 @@ const testGroups: Array = [ input: '今天是星期日' , - transformText: '', + transformText: '今天是星期日', output: { camelCase: '今天是星期日', pascalCase: '今天是星期日', @@ -67,7 +96,7 @@ const testGroups: Array = [ input: '🥰 a-cup/_of Coffee🍻,-_please!. ' , - transformText: '', + transformText: '🥰|a|cup|/|of|coffee|🍻,|please|!. ', output: { camelCase: '', pascalCase: '', @@ -78,7 +107,7 @@ const testGroups: Array = [ input: ' NHDAs--+90-usz&* ' , - transformText: '', + transformText: ' n|h|d|as|+90|usz|&* ', output: { camelCase: '', pascalCase: '', @@ -89,7 +118,7 @@ const testGroups: Array = [ input: '--担心你鸿dAf_=coffee—_— ' , - transformText: '', + transformText: '担心你鸿|d|af|=|coffee|—|— ', output: { camelCase: '', pascalCase: '', @@ -100,7 +129,7 @@ const testGroups: Array = [ input: 'fsdi_sdacsaf+desd' , - transformText: '', + transformText: 'fsdi|sdacsaf|+|desd', output: { camelCase: '', pascalCase: '', @@ -181,9 +210,10 @@ const testGroups: Array = [ ' a_nice_day! ', ' ANiceDay! ', ' A_NICE_DAY! ', - ' A_Nice_DaY! ', - ' A-NiCe_Day! ', - ' A----NiCe_Day_-_!-- ', + // ' A_Nice_DaY! ', + // ' A-NiCe_Day! ', + // ' A----NiCe_Day_-_!-- ', + ' A----NICE_Day_-_!-- ', ], transformText: ' a|nice|day|! ', output: { @@ -194,7 +224,7 @@ const testGroups: Array = [ { title: 'Normal input (a-nice-day)', input: [ - ' A niCe-Day-', + ' A NICE-Day-', ' A niceDay', ], transformText: ' a|nice|day', @@ -203,6 +233,18 @@ const testGroups: Array = [ pascalCase: ' A NiceDay', }, }, + { + title: 'Normal input (foo-bar)', + input: [ + ' app2-Trrre ', + ' app2Trrre ', + ], + transformText: ' app|2|trrre ', + output: { + camelCase: '', + pascalCase: '', + }, + }, { title: 'Normal input (Julius-Caesar, William-Shakespeare, ...)', input: diff --git a/src/type-definition/text-transform-type.ts b/src/type-definition/text-transform-type.ts new file mode 100644 index 0000000..68b4cb2 --- /dev/null +++ b/src/type-definition/text-transform-type.ts @@ -0,0 +1,6 @@ +export type TransformTextResult = { + leadingSpace: string + trailingSpace: string + result: string + trimResult: string +};