实现 转小驼峰 Camel Case;转大驼峰 Pascal Case;transformText 函数通过测试用例
This commit is contained in:
parent
181d2b4c6f
commit
c7a16581ab
@ -8,13 +8,24 @@ import { transformText } from './text-transform';
|
|||||||
* @since 2024-03-28
|
* @since 2024-03-28
|
||||||
*/
|
*/
|
||||||
export function toCamelCase(str: string): string {
|
export function toCamelCase(str: string): string {
|
||||||
// 切割文本
|
// Cut text 切割文本
|
||||||
const result = transformText(str);
|
let result = transformText(str);
|
||||||
console.log('result', result);
|
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
|
* @since 2024-03-28
|
||||||
*/
|
*/
|
||||||
export function toPascalCase(str: string): string {
|
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('_', ''));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { TransformTextResult } from "../type-definition/text-transform-type";
|
||||||
|
|
||||||
const logDebugInfo = false;
|
const logDebugInfo = false;
|
||||||
|
|
||||||
@ -7,19 +8,21 @@ const logDebugInfo = false;
|
|||||||
* @param str
|
* @param str
|
||||||
* @since 2024-04-02
|
* @since 2024-04-02
|
||||||
*/
|
*/
|
||||||
export function transformText(input: string): string {
|
export function transformText(input: string): TransformTextResult {
|
||||||
logDebugInfo && console.log('input ', '->' + input + '<-');
|
logDebugInfo && console.log('input ', '->' + input + '<-');
|
||||||
|
|
||||||
// 记录首尾空格
|
// 记录首尾空格
|
||||||
const leadingSpaces = input.match(/^ +/);
|
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) => {
|
let result = input.replace(/([A-Za-z\-_ ]+)/g, (match: string) => {
|
||||||
|
|
||||||
// 替换连字符为 '|' (如有多个则合并)
|
// 替换连字符为 '|' (如有多个则合并)
|
||||||
match = match.replace(/[-_ ]+/g, '|');
|
match = match.replace(/[-_ ]+/g, '|');
|
||||||
|
|
||||||
@ -48,9 +51,15 @@ export function transformText(input: string): string {
|
|||||||
result = result.replace(/(^[\|]+|[\|]+$)/g, '');
|
result = result.replace(/(^[\|]+|[\|]+$)/g, '');
|
||||||
|
|
||||||
// 还原首尾空格
|
// 还原首尾空格
|
||||||
// result = (leadingSpaces ? (leadingSpaces[0] + '|') : '') + result + (trailingSpaces ? ('|' + trailingSpaces[0]) : '');
|
const leadingSpaceStr = leadingSpaces ? leadingSpaces[0] : '';
|
||||||
result = (leadingSpaces ? leadingSpaces[0] : '') + result + (trailingSpaces ? trailingSpaces[0] : '');
|
const trailingSpaceStr = trailingSpaces ? trailingSpaces[0] : '';
|
||||||
|
let noTrimResult = leadingSpaceStr + result + trailingSpaceStr;
|
||||||
|
|
||||||
logDebugInfo && console.log('output ', '->' + result + '<-');
|
logDebugInfo && console.log('output ', '->' + result + '<-');
|
||||||
return result;
|
return {
|
||||||
|
leadingSpace: leadingSpaceStr,
|
||||||
|
trailingSpace: trailingSpaceStr,
|
||||||
|
result: noTrimResult,
|
||||||
|
trimResult: result,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,8 @@ suite('Extension Test: run test case', () => {
|
|||||||
const inputList = Array.isArray(testCase.input) ? testCase.input : [testCase.input];
|
const inputList = Array.isArray(testCase.input) ? testCase.input : [testCase.input];
|
||||||
for (const input of inputList) {
|
for (const input of inputList) {
|
||||||
// 验证 transformText
|
// 验证 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.camelCase, toCamelCase(input));
|
||||||
assert.strictEqual(testCase.output.pascalCase, toPascalCase(input));
|
assert.strictEqual(testCase.output.pascalCase, toPascalCase(input));
|
||||||
|
@ -24,13 +24,43 @@ const testGroups: Array<TestCaseGroup> = [
|
|||||||
pascalCase: ' ',
|
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',
|
title: 'long text input',
|
||||||
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.`
|
`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: {
|
output: {
|
||||||
camelCase: '',
|
camelCase: '',
|
||||||
pascalCase: '',
|
pascalCase: '',
|
||||||
@ -41,10 +71,9 @@ const testGroups: Array<TestCaseGroup> = [
|
|||||||
title: 'enter input',
|
title: 'enter input',
|
||||||
input:
|
input:
|
||||||
`How do you\bdo?
|
`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: {
|
output: {
|
||||||
camelCase: '',
|
camelCase: '',
|
||||||
pascalCase: '',
|
pascalCase: '',
|
||||||
@ -56,7 +85,7 @@ const testGroups: Array<TestCaseGroup> = [
|
|||||||
input:
|
input:
|
||||||
'今天是星期日'
|
'今天是星期日'
|
||||||
,
|
,
|
||||||
transformText: '',
|
transformText: '今天是星期日',
|
||||||
output: {
|
output: {
|
||||||
camelCase: '今天是星期日',
|
camelCase: '今天是星期日',
|
||||||
pascalCase: '今天是星期日',
|
pascalCase: '今天是星期日',
|
||||||
@ -67,7 +96,7 @@ const testGroups: Array<TestCaseGroup> = [
|
|||||||
input:
|
input:
|
||||||
'🥰 a-cup/_of Coffee🍻,-_please!. '
|
'🥰 a-cup/_of Coffee🍻,-_please!. '
|
||||||
,
|
,
|
||||||
transformText: '',
|
transformText: '🥰|a|cup|/|of|coffee|🍻,|please|!. ',
|
||||||
output: {
|
output: {
|
||||||
camelCase: '',
|
camelCase: '',
|
||||||
pascalCase: '',
|
pascalCase: '',
|
||||||
@ -78,7 +107,7 @@ const testGroups: Array<TestCaseGroup> = [
|
|||||||
input:
|
input:
|
||||||
' NHDAs--+90-usz&* '
|
' NHDAs--+90-usz&* '
|
||||||
,
|
,
|
||||||
transformText: '',
|
transformText: ' n|h|d|as|+90|usz|&* ',
|
||||||
output: {
|
output: {
|
||||||
camelCase: '',
|
camelCase: '',
|
||||||
pascalCase: '',
|
pascalCase: '',
|
||||||
@ -89,7 +118,7 @@ const testGroups: Array<TestCaseGroup> = [
|
|||||||
input:
|
input:
|
||||||
'--担心你鸿dAf_=coffee—_— '
|
'--担心你鸿dAf_=coffee—_— '
|
||||||
,
|
,
|
||||||
transformText: '',
|
transformText: '担心你鸿|d|af|=|coffee|—|— ',
|
||||||
output: {
|
output: {
|
||||||
camelCase: '',
|
camelCase: '',
|
||||||
pascalCase: '',
|
pascalCase: '',
|
||||||
@ -100,7 +129,7 @@ const testGroups: Array<TestCaseGroup> = [
|
|||||||
input:
|
input:
|
||||||
'fsdi_sdacsaf+desd'
|
'fsdi_sdacsaf+desd'
|
||||||
,
|
,
|
||||||
transformText: '',
|
transformText: 'fsdi|sdacsaf|+|desd',
|
||||||
output: {
|
output: {
|
||||||
camelCase: '',
|
camelCase: '',
|
||||||
pascalCase: '',
|
pascalCase: '',
|
||||||
@ -181,9 +210,10 @@ const testGroups: Array<TestCaseGroup> = [
|
|||||||
' a_nice_day! ',
|
' a_nice_day! ',
|
||||||
' ANiceDay! ',
|
' 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_-_!-- ',
|
||||||
|
' A----NICE_Day_-_!-- ',
|
||||||
],
|
],
|
||||||
transformText: ' a|nice|day|! ',
|
transformText: ' a|nice|day|! ',
|
||||||
output: {
|
output: {
|
||||||
@ -194,7 +224,7 @@ const testGroups: Array<TestCaseGroup> = [
|
|||||||
{
|
{
|
||||||
title: 'Normal input (a-nice-day)',
|
title: 'Normal input (a-nice-day)',
|
||||||
input: [
|
input: [
|
||||||
' A niCe-Day-',
|
' A NICE-Day-',
|
||||||
' A niceDay',
|
' A niceDay',
|
||||||
],
|
],
|
||||||
transformText: ' a|nice|day',
|
transformText: ' a|nice|day',
|
||||||
@ -203,6 +233,18 @@ const testGroups: Array<TestCaseGroup> = [
|
|||||||
pascalCase: ' A NiceDay',
|
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, ...)',
|
title: 'Normal input (Julius-Caesar, William-Shakespeare, ...)',
|
||||||
input:
|
input:
|
||||||
|
6
src/type-definition/text-transform-type.ts
Normal file
6
src/type-definition/text-transform-type.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
export type TransformTextResult = {
|
||||||
|
leadingSpace: string
|
||||||
|
trailingSpace: string
|
||||||
|
result: string
|
||||||
|
trimResult: string
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user